Looking for Senior AWS Serverless Architects & Engineers?
Let's TalkIntroduction
In this article, we will take a look at a use-case that represents how to deploy a Serverless Containerized NodeJs Application on Amazon ECS Fargate with AWS Copilot.
We will look at both the use case of AWS Copilot normal deploy and AWS CI/CD pipeline-based deployment workflow for serverless containerized applications.
AWS Architecture
Above AWS Architecture Diagram showing how AWS Copilot CLI is used to deploy Serverless Containerized Application on Amazon ECS Fargate both the way normal and CI/CD pipeline workflow-based deployment.
What is Amazon ECS?
Amazon ECS is a fully managed container orchestration service that makes it easy for you to deploy, manage, and scale containerized applications. [1]
What is AWS Fargate?
AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. AWS Fargate is compatible with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS). [2]
Recently AWS announced that AWS Fargate now delivers faster scaling of applications. Now AWS Fargate enables customers to scale applications faster, improving performance and reducing wait time. AWS has made several improvements over the last year that enable you to scale applications up to 16X faster, making it easier to build and run applications at a larger scale on Fargate, along with that AWS Fargate increases task launch rates.
What is AWS Copilot?
The AWS Copilot CLI is a tool for developers to build, release, and operate production-ready containerized applications on Amazon ECS, AWS Fargate and AWS App Runner. From getting started, pushing to staging, and releasing to production, Copilot can help manage the entire lifecycle of your application development. [3]
AWS Copilot helps customers focus on building their applications rather than setting up their infrastructure. With one command, AWS Copilot will provision all the infrastructure customers need to run production-ready containerized services on Amazon ECS and AWS Fargate. [4]
AWS Copilot will create ECS clusters, tasks, services for Fargate launch types along with that also create load balancers, VPC, ECR registries, and more.
AWS Copilot Concepts
Main three concepts: Applications, Environments, Services [5]
Application - Collection of services and environmentsEnvironment - Deployment environment test/staging/productionService - Service is code/application runs in the container
AWS Copilot FeaturesDevelop - Quick Setup infrastructure for your containerized application
Release - Easily Setup CI/CD pipeline to automatically deploy your containerized application
Operate - Check container service's status
AWS Copilot CLI Commands
Setup Environments
Make sure you have the following installed:
- AWS CLI - Install Instruction
- AWS Copilot CLI - Install Instruction
- Docker Engine - Install Instruction
- NodeJs - Install Instruction
Setup AWS CLI Default Configuration - AWS Configure
Make sure you created a relevant IAM User with programmatic access and used relevant credentials with this aws configuration step.
Create a NodeJs Application
Create a node project folder and initialize npm with express to run the server
Create index.js file with this code
It will create a server that listens on port 3000 using node express
Update package.json Scripts with below:
It will allow running nodejs index.js file using npm run start, which will run the express server.
Dockerize the NodeJs Application
Create Dockerfile file in the root of the same project
Build and Run Docker image
In local try to access http://localhost:3000, It should return below response if all good.
Deploy to Amazon ECS Fargate with AWS Copilot
This will help you to initialize and deploy, during this process you will be required to answer a few questions, below are mentioned answers for your reference:
Application Name: sg-fargate-ecs
Workload Type: Load Balanced Web Services
Service Name: api
Dockerfile: ./Dockerfile
Deploying to test environment for first time
Once you answer all the questions, the process will build an environment with IAM Role, Subnet, VPC, and more, you can wait to get that finish.
When done with building the environment, the Docker image will be pushed to Amazon ECR.In the end, deployment to Amazon ECS will start.
Access URL display in ‘Recommended follow-up action’ section from above command response, It should return below response if all good, it means you are done with Amazon ECS Fargate Deployment.
NodeJs Application Docker Image Pushed to Amazon ECR
Amazon ECS Cluster Auto Created with Launch Type Fargate
Amazon ECS Cluster Details Which Auto Created
Amazon ECS Cluster Running 1 Service & 1 Task Using AWS Fargate
Add Project within Git & Set AWS CodeCommit Repository as Remote
Initialize git into the project and do an initial commit
Create AWS CodeCommit Repository
Services → Developer Tools → CodeCommit → Repositories → Create repository
Add AWS CodeCommit Repository as remote origin in a local git repository
After git push to origin, you will see project files into AWS CodeCommit Repository
AWS CI/CD Pipeline Workflow with AWS Copilot
Initialize copilot pipeline
Commit and push the buildspec.yml, pipeline.yml, and .workspace files of your copilot directory to your repository.
Run copilot pipeline deploy to create your pipeline
Copilot pipeline deploy will auto-create AWS CodePipeline CI/CD workflow along with AWS CodeBuild project, Relevant IAM Roles, and more that help to Deploy Serverless Containerized NodeJs Application on Amazon ECS Fargate based on git commit and push triggers on specific git branch.
AWS CodePipeline Source Step
Pipeline source step auto using that same AWS CodeCommit repository which we created and added as remote for project in which initialize copilot pipeline.
AWS CodePipeline Build Step
Copilot pipeline deploy auto created new Build Project and used same as part of this CI/CD Pipeline.
During running the CI/CD pipeline the first time, at the Build Stage, it was failed.
Resolved this by updating the Build Project IAM Role Permissions policies by attaching this additional permission: AmazonEC2ContainerRegistryFullAccess
AWS CodePipeline Deploy Step
Copilot pipeline using AWS CloudFormation for deployment steps.
After the AWS Copilot CI/CD pipeline is created, As soon as the developer commits and pushes changes to a specific branch it will auto-trigger the relevant deployment pipeline to the auto-deploy container application.
Remove Copilot Application
This will help you to clean up your resources created before within the application.
Conclusion
Amazon ECS with Fargate allows you to run Serverless Containers. AWS Copilot CLI is a great tool for easily launching and managing containerized applications on AWS.
AWS Copilot supports quick develop operations for Containerized Amazon ECS applications, environments, services, jobs, tasks through commands, which also allows for a quick release process using deploy & pipeline commands to quicken up the CI/CD workflow for containers.
The scope of this article covers the basic features of AWS Copilot CLI with Amazon ECS Fargate, but there is always more to learn since they have more supported features. That being said, you should be able to easily explore those features after reading this article.
Sources
[1]https://aws.amazon.com/ecs/
[2]https://aws.amazon.com/fargate/
[3]https://aws.github.io/copilot-cli/en/docs/overview/
[4]https://aws.amazon.com/containers/copilot/faqs/
[5]https://aws.github.io/copilot-cli/docs/concepts/environments/