Skip to content

Latest commit

 

History

History

README.md

Building and Deploying a Docker Image With AWS CodePipeline

Stability: Stable

This is a stable example. It should successfully build out of the box

This example is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.


Overview

This AWS Cloud Development Kit (CDK) Python example demonstrates how to configure AWS CodePipeline with CodeCommit, CodeBuild, and CodeDeploy to build and deploy a Docker image to an Elastic Container Service (ECS) cluster running AWS Fargate (serverless compute for containers).

Real-world Example

When working in fast-paced development environments, CI/CD (Continuous Integration and Continuous Delivery) pipelines are used to automatically build, test, and deploy application changes across multiple accounts and environments. This allows new features and bug fixes to be tested and deployed quickly to continuously improve the application.

Requirements

AWS Services Utilized

  • CodePipeline
  • CodeCommit
  • CodeBuild
  • CodeDeploy
  • Elastic Container Service (ECS)
  • Fargate
  • Elastic Container Registry (ECR)
  • Lambda

Deploying

  • Authenticate to an AWS account via a Command Line Interface (CLI).
  • Navigate to this codepipeline-build-deploy directory.
  • pip install -r requirements.txt to install the required dependencies.
  • cdk synth to generate and review the CloudFormation template.
  • cdk diff to compare local changes with what is currently deployed.
  • pytest to run the unit tests we specify in tests/unit/test_codepipeline_build_deploy_stack.py.
  • cdk deploy to deploy the stack to the AWS account you're authenticated to.

Output

After a successful deployment, CDK will output a public endpoint for:

  • Application Load Balancer (ALB)

Testing

  • To test that the Docker image was built and deployed successfully to ECS, we can use the Application Load Balancer (ALB) public endpoint, e.g. http://xyz123.us-east-1.elb.amazonaws.com.
  • The simple containerized application contains multiple pages for testing:
    • /index.html
    • /about.html
    • /contact.html
    • /error.html
  • To run the Python unit tests, execute the following commands:
    • pip install -r requirements-dev.txt
    • pytest
  • Navigate to the AWS CodePipeline console and select ImageBuildDeployPipeline. Then click on Release change to trigger the pipeline and observe the workflow in action end-to-end.
  • Navigate to the AWS Console to view the services that were deployed:
    • CodePipeline pipeline
    • CodeCommit repository
    • CodeBuild project
    • CodeDeploy application
    • ECS cluster
    • ECS service on Fargate
    • ECR image repository
    • Lambda functions

Further Improvements