Continuous Deployment to Amazon ECS Using AWS CodePipeline, AWS CodeBuild, and AWS CloudFormation
The ECS Continuous Deployment reference architecture demonstrates how to achieve continuous deployment of an application to Amazon EC2 Container Service (Amazon ECS) using AWS CodePipeline, AWS CodeBuild, and AWS CloudFormation. With continuous deployment, software revisions are deployed to a production environment without explicit approval from a developer, automating the entire software release process.
Launching this AWS CloudFormation stack provisions the following
- A continuous deployment process that uses AWS CodePipeline to monitor a GitHub repository for new commits
- AWS CodeBuild to create a new Docker container image and to push it into Amazon EC2 Container Registry (Amazon ECR),
- AWS CloudFormation to deploy the new container image to production on Amazon ECS.
This getting started guide uses Git to clone and push files to and from GitHub repositories. You can install the Git command line tools by following the instructions at https://git-scm.com/downloads, or by using a package manager like NuGet or Homebrew.
- Browse to the Amazon ECS sample app.
- Choose fork from the upper right corner of the screen.
From your terminal application, run the following command (replace <your_github_username> with your GitHub username):
git clone https://github.com/<your_github_username>/ecs-demo-php-simple-app
This creates a directory named ecs-demo-php-simple-app in your current directory, which contains the code for the Amazon ECS sample app.
The personal access token is used by AWS CodePipeline to access the contents of your GitHub repository.
- Browse to https://github.com/settings/tokens.
- Choose Generate new token.
- In Token description, type a description.
- Under Select scopes, choose repo.
- Choose Generate token.
- Copy the generated token to the clipboard.
Choose Launch Stack to launch the template in the US East (N.Virginia) Region in your account. The CloudFormation template requires the following parameters:
- GitHub configuration
- Repo: The repo name of the sample service; for example, <github_username>/ecs-demo-php-simple-app
- Branch: The branch of the repo to deploy continuously; for example, master
- User: Your user name on GitHub
- Personal Access Token: The token for the user specified in the preceding procedure
The CloudFormation stack provides the following output:
- ServiceUrl: The sample service that is being continuously deployed.
- PipelineUrl: The continuous deployment pipeline in the AWS Management Console.
After the CloudFormation stack is created, the latest commit to the GitHub repository is run through the pipeline and deployed to Amazon ECS. Open the PipelineUrl to watch the first revision run through the AWS CodePipeline pipeline. After the deploy step turns green, open the URL from ServiceUrl, which loads a page similar to this:
To test continuous deployment, make a change to src/index.php in the ecs-demo-php-simple-app repository and push it to GitHub.
From your terminal application, change the working directory to your local copy of the repository, and then run the following commands to push the changes to your remote repository:
git add . git commit –m “changes to index.php” git push origin master
AWS CodePipeline detects the change, builds the new application, and deploys it to your cluster automatically. After the pipeline finishes deploying the revision, refresh the page to see your changes.
This pipeline is now in production, listening for new code in the source code repository, and ready to ship future changes that your team pushes into production. The pipeline is extensible, meaning that you can add stages to include additional steps. For example, add a test stage to run unit and acceptance tests. This helps ensure new code revisions are safe to deploy to production. You can also include a notification step that alerts your team, through email or a Slack channel, that a new version is live. The notification can include details about the change that was deployed to production.