Example: Modify an Amazon ECS taskdef - Amazon CodeCatalyst

Example: Modify an Amazon ECS taskdef

The following is an example of a full workflow that includes the Render Amazon ECS task definition action, along with build and deploy actions. The workflow’s purpose is to build and deploy a Docker image into your Amazon ECS cluster. The workflow consists of the following building blocks that run sequentially:

  • A trigger – This trigger starts the workflow run automatically when you push a change to your source repository. For more information about triggers, see Starting a workflow run automatically using triggers.

  • A build action (BuildDocker) – On trigger, the action builds the Docker image using the Dockerfile, tags it with a commit ID, and pushes the image to Amazon ECR. For more information about the build action, see Building with workflows.

  • A Render Amazon ECS task definition action (RenderTaskDef) – On completion of the build action, this action updates an existing taskdef.json located in the root of your source repository with an image field value that includes the correct commit ID. It saves the updated file with a new file name (task-definition-random-string.json) and then creates an output artifact that contains this file. The render action also generates a variable called task-definition and sets it to the name of the new task definition file. The artifact and variable will be used the deploy action, which is next.

  • A Deploy to Amazon ECS action (DeployToECS) – On completion of the Render Amazon ECS task definition action, the Deploy to Amazon ECS action looks for the output artifact generated by the render action (TaskDefArtifact), finds the task-definition-random-string.json file inside of it, and registers it with your Amazon ECS service. The Amazon ECS service then follows the instructions in the task-definition-random-string.json file to run Amazon ECS tasks—and associated Docker image containers—inside your Amazon ECS cluster.

Name: codecatalyst-ecs-workflow SchemaVersion: 1.0 Triggers: - Type: PUSH Branches: - main Actions: BuildDocker: Identifier: aws/build@v1 Environment: Name: codecatalyst-ecs-environment Connections: - Name: codecatalyst-account-connection Role: codecatalyst-ecs-build-role Inputs: Variables: - Name: REPOSITORY_URI Value: 111122223333.dkr.ecr.us-east-2.amazonaws.com/codecatalyst-ecs-image-repo - Name: IMAGE_TAG Value: ${WorkflowSource.CommitId} Configuration: Steps: #pre_build: - Run: echo Logging in to Amazon ECR... - Run: aws --version - Run: aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-2.amazonaws.com #build: - Run: echo Build started on `date` - Run: echo Building the Docker image... - Run: docker build -t $REPOSITORY_URI:latest . - Run: docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG #post_build: - Run: echo Build completed on `date` - Run: echo Pushing the Docker images... - Run: docker push $REPOSITORY_URI:latest - Run: docker push $REPOSITORY_URI:$IMAGE_TAG RenderTaskDef: DependsOn: - BuildDocker Identifier: aws/ecs-render-task-definition@v1 Inputs: Variables: - Name: REPOSITORY_URI Value: 111122223333.dkr.ecr.us-east-2.amazonaws.com/codecatalyst-ecs-image-repo - Name: IMAGE_TAG Value: ${WorkflowSource.CommitId} Configuration: task-definition: taskdef.json container-definition-name: codecatalyst-ecs-container image: $REPOSITORY_URI:$IMAGE_TAG # The output artifact contains the updated task definition file. # The new file is prefixed with 'task-definition'. # The output variable is set to the name of the updated task definition file. Outputs: Artifacts: - Name: TaskDefArtifact Files: - "task-definition*" Variables: - task-definition DeployToECS: Identifier: aws/ecs-deploy@v1 Environment: Name: codecatalyst-ecs-environment Connections: - Name: codecatalyst-account-connection Role: codecatalyst-ecs-deploy-role #Input artifact contains the updated task definition file. Inputs: Sources: [] Artifacts: - TaskDefArtifact Configuration: region: us-east-2 cluster: codecatalyst-ecs-cluster service: codecatalyst-ecs-service task-definition: ${RenderTaskDef.task-definition}