AWS ECS Blue/Green CodePipeline: Exception while trying to read the image artifact

爷,独闯天下 提交于 2021-01-28 11:50:55

问题


I wanted to create a CodePipeline which builds a container image from CodeCommit source and afterwards deploys the new image in Blue/Green fashion to my ECS service (EC2 launchtype).

  • The source stage is CodeCommit, which already includes appspec.json
    as well as taskdef.json
  • The build stage is building the new container & pushing it to ECR successfully, the file imagedefinition.json is the BuildArtifact created at this step, containing the container and the recently created image with its tag corresponding to the CodeCommit commit-id.
  • The deploy stage is made of action "Amazon ECS (Blue/Green)" using the SourceArtifact and BuildArtifact as InputArtifacts, to take the appspec and taskdef from the SourceArtifact and the image description from the BuildArtifact, to finally deploy the new container in Blue/Green manner.

The problem is with the image definition from the BuildArtifact. The pipeline fails in the Deploy phase with error:

"" Invalid action configuration Exception while trying to read the image artifact file from the artifact: BuildArtifact. ""

How to properly configure the "Amazon ECS (Blue/Green)" deploy phase, so that it can use the recently created image and deploy it....by replacing placeholder IMAGE_NAME inside taskdef.json ?

Any hint highly appreciated :D


回答1:


answering my own question here, hopefully it helps others who facing the same situation.

  1. the file imagedefinitions.json is inappropriate for deploy action "Amazon ECS Blue/Green". For that you have to create file imageDetail.json within the build step and provide it as artifact to the deploy step. How ? This is how the bottom of my buildspec.yaml looks like:
      - printf '{"ImageURI":"%s"}' $REPOSITORY_URI:$IMAGE_TAG > imageDetail.json
artifacts:
  files: 
    - 'image*.json'
    - 'appspec.yaml'
    - 'taskdef.json'
  secondary-artifacts:
    DefinitionArtifact:
      files:
        - appspec.yaml
        - taskdef.json
    ImageArtifact:
      files:
        - imageDetail.json
  1. In the Deploy phase of CodePipeline, use DefinitionArtifact and ImageArtifact as Input Artifacts and configure them in the corresponding section "Amazon ECS task definition" and "AWS CodeDeploy AppSpec file".

Ensure that your appspec.yaml contains placeholder for the task definition. Here is my appspec.yaml:

version: 0.0

Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: <TASK_DEFINITION>
        LoadBalancerInfo:
          ContainerName: "my-test-container"
          ContainerPort: 8000

Also ensure that your taskdef.json contains placeholder for the final image, like

...
"image": <IMAGE1_NAME>,
...
  1. use that placeholder in the codepipeline config of your blue/green deploy phase in the section "Dynamically update task definition image - optional" by choosing the input artifact as "ImageArtifact" and the placeholder <IMAGE1_NAME>



回答2:


Amazon ECS Blue/Green (or CodeDeployToECS) CodePipeline action requires the TaskDefinitionTemplateArtifact parameter (see [1]).

In addition to the above file note an imageDetail.json is required for ECS Blue/Green deployments (not 'imagedefinition.json'). The file structure and details are available here [2]. Add this file to the root of your deployment artifact/version control. If you do not want to add this file manually you can use the ECR source action to the CodePipeline and configure this with the Image you are using in the ECS service/taskdef.json. This is all discussed at [2] for clarity.

To see how this is all brought together you can also follow the step by step instructions for ECS Blue/Green deployments here [3].

References:

[1] https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html#action-requirements : CodePipeline Pipeline Structure Reference - Action Structure Requirements in CodePipeline [2] https://docs.aws.amazon.com/codepipeline/latest/userguide/file-reference.html#file-reference-ecs-bluegreen : Image Definitions File Reference - imageDetail.json File for Amazon ECS Blue/Green Deployment Actions [3] https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-ecs-ecr-codedeploy.html : Tutorial: Create a Pipeline with an Amazon ECR Source and ECS-to-CodeDeploy Deployment




回答3:


Thanks, to all, this gives me some light into solving the issue.

I would like to add that when you use aws cli, cloudformation, or Terraform to configure codepipeline, some parameters and options are not available with the console and setting some variables in these tools like the empty string "" will cause an exception error.

Always check for codepipeline settings in the console when you deploy using these tools.

so the error occur when you defined Image Artifact but not define the placeholder

imageDetail.json can be passed into codedeploy using the following methods:

  • git source ( codecommit or github ) the file that exist in your app codebase
  • ECR source - the file will be autogenerated by ECR, but will use SHA256 instead of the image tag
  • CodeBuild source - you update the file using codebuild buildspec.yml and pass it down to codedeploy stage.


来源:https://stackoverflow.com/questions/62022787/aws-ecs-blue-green-codepipeline-exception-while-trying-to-read-the-image-artifa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!