In this blog, we’ll explore how to automate the deployment of a Node.js application using AWS CodePipeline and deploy it to Amazon ECS (Elastic Container Service). By setting up a continuous integration and continuous deployment (CI/CD) pipeline, we can ensure a seamless and automated process from code changes to a running application in ECS.
Prerequisites
- AWS Account: Ensure you have an AWS account with appropriate permissions to create and manage AWS services.
- GitHub Repository: Have your Node.js application code hosted on a GitHub repository. In this tutorial we will be using github repo available here.
- ECS Service: We will be using a service created during previous tutorial.
- ECR Container Registry: We will utilize ECR registry created during previous tutorial.
Setting up AWS CodePipeline
Let’s start by creating an AWS CodePipeline to automate the deployment process.
1. Create a New Pipeline:
- Go to AWS CodePipeline and create a new pipeline named
ecs-pipeline
. - Choose a new service role and click next.
2. Configure the Source Stage:
- Source Provider: GitHub (Version 2)
- Click on “Connect to GitHub”.
- Follow the prompts to install a new app and sign in to your GitHub account.
- Once connected, select your repository and the respective branch to trigger the CodePipeline on new code pushes.
3. Add the Build Stage:
- Build Provider: CodeBuild
- Region: US EAST (N. Virginia)
- Click on “Create Project for CodeBuild”.
4. Configure CodeBuild Project:
- Project Name: my-ecs-codebuild
- Environment Image: Managed Image
- Runtime(s): standard
- Image: aws/codebuild/amazonlinux2-x86_64-standard:4.0
- Image version: Always use the latest image for this runtime version
- Environment type: Linux EC2
- Check the privilege field to allow CodeBuild to build Docker images.
- Service Role: Create a new service role and grant it ECRFullAccess to perform ECR operations.
- Build specifications: Use a buildspec file
- Buildspec name: build-spec.yml (This file contains build commands and is present in our Code Repository)
- Click “Connect to codepipeline” and select the CodeBuild project in the CodePipeline build stage.
5. Add the Deploy Stage:
- Deploy Provider: Amazon ECS
- Region: US-East (N. Virginia)
- Cluster name: myCluster
- Service name: my-ecs-service
- Image definitions file: imagedefinitions.json
6. Review and Create Pipeline:
- Click next, review the pipeline configuration, and then create the pipeline.
Testing the Deployment
To validate our setup:
- Make changes in your GitHub code repository.
- This will automatically trigger the CodePipeline, which will build the Docker image, push it to Amazon ECR, and update the ECS service with the new image.
If you encounter any issues, ensure you check the CodeBuild’s IAM role permissions and verify that the codebuild OS architecture matches the OS architecture mentioned in your task definition.
Conclusion
By setting up an AWS CodePipeline to automate the deployment of your Node.js application to Amazon ECS, you’ve established a reliable and efficient CI/CD process. This automation ensures that your application is always up-to-date with the latest changes and deployed seamlessly.