'AWS Cloudformation for building and deploying image from ECR. CodePipeline issue with artifact not found

I have a project which has this directory structure and I'm deploying it to AWS.

./frontend
./backend

I have two Cloudformation scripts: (1) to deploy the backend as a Fargate cluster, and (2) the other is for the GitHub/CodeBuild/CodePipelines.

Inside the backend directory is the buildspec.yml file which has these lines for the final steps for saving the artifact:

  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{"name":"cicd-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
      - cat imagedefinitions.json
artifacts:
  files:
    - ./backend/imagedefinitions.json

My cloudformation script for CodeBuild is essentially this:

- Name: BuildBackend
          Actions:
          - Name: Build
            ActionTypeId:
              Category: Build
              Owner: AWS
              Provider: CodeBuild
              Version: 1
            OutputArtifacts:
              - Name: be-build
            InputArtifacts:
              - Name: source-output
            Configuration:
                ProjectName: !Ref BackendCodeBuildProject
            RunOrder: 1

When I navigate to S3, I can see the artifacts being built and saved inside the zip file, i.e. I navigate to here and download it. The file is saved inside ./backend/imagedefinitions.json.

enter image description here

My problem is that I'm trying to use the portion of the Cloudformation script below to use the contents of imagedefinitions.json as input to another script updating the Fargate cluster.

      - Name: DeployBackend
          Actions:
          - Name: Deploy
            ActionTypeId:
              Category: Deploy
              Owner: AWS
              Version: 1
              Provider: CloudFormation
            InputArtifacts:
              - Name: source-output
              - Name: be-build
            Configuration:
                ActionMode: CREATE_UPDATE
                Capabilities: CAPABILITY_NAMED_IAM
                # Pass parameter values to Fargate-Cluster.yml for deployment
                ParameterOverrides: !Sub |
                  {
                    "ImageURI" : { 
                      "Fn::GetParam" : [
                        "be-build", 
                        "/backend/imagedefinitions.json", 
                        "ImageURI"
                      ]},
                    "Stage":  "${Stage}",
                    "ContainerPort": "${ContainerPort}"
                  }
                RoleArn: 
                  Fn::GetAtt: [ CloudformationExecutionRole, Arn ]
                StackName: !Join ['-', [!Ref Stage, !Ref 'AWS::AccountId', 'secure-cycle-fargate-cluster']]
                TemplatePath: source-output::Cloudformation/Fargate-Cluster.yml
            RunOrder: 1

The error I receive is:

Action execution failed File [/backend/imagedefinitions.json] does not exist in artifact [be-build]

I've tried different paths such as ./backend/imagedefinitions.json and backend/imagedefinitions.json. Nothing has worked. I'm trying to understand what I'm doing wrong here.



Solution 1:[1]

The approach I took was to set base-directory: backend in the buildspec.yml file.

Also, I realized my buildspec file had an error when saving the imagedefinitions.json. This created another error after I solved the first problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sdbol