Cloudfront can be simply defined as a CDN (Content Delivery Network), caching your static assets in a datacenter nearer to your viewers. But Cloudfront is a lot more complex and versatile than this simple definition. Cloudfront is a “pull” CDN, which means that you don’t push your content to the CDN. The content is pulled into the CDN Edge from the origin at the first request of any piece of content.
In addition to the traditional pull and cache usage, Cloudfront can also be used as:
A Networking Router
A Firewall
A Web Server
An Application Server
Why is using a CDN relevant?
The main reason is to improve the speed of delivery of static content. By caching the content on the CDN edge, you not only reduce the download time from a few seconds to a few milliseconds, but you also reduce the load and amount of requests on your backend (Network, IO, CPU, Memory, …).
Static content can be defined as content not changing between two identical requests done in the same time frame.
Identical can be as simple as the same URI, or as fine grained as down to the authentication header. The time frame can range between 1 second to 1 year. The most common case is caching resources like Javascript or CSS and serving the same file to all users forever. But caching a JSON response tailored to a user (Authentication header) for a few seconds reduces the backend calls when the user has the well-known “frenetic browser reload syndrome”.
Edges, Mid-Tier Caches, and Origins
Cloudfront isn’t “just” some servers in datacenters around the world. The service is a layered network of Edge Locations and Regional Edge Caches (or Mid-Tier Caches).
Edge Locations are distributed around the globe with more than 400 points of presence in over 90 cities across 48 countries. Each Edge Location is connected to one of the 13 Regional Edge Caches.
Regional Edge Caches are transparent to you and your visitors, you can’t configure them or access them directly. Your visitors will interact with the nearest Edge Location, which will connect to the attached Regional Edge Cache and finally to your origin. Therefore, in this article, we will refer to Cloudfront as the combination of Edge Locations and Region Edge Caches.
What Have We Learned?
Cloudfront is more than just a simple “pull-cache-serve” service
You improve delivery speed to your visitors
You can increase resilience by always using a healthy backend
You improve overall speed to your backend by leveraging AWS’s backbone
You can modify any request to tailor the response to your visitor’s device or region
You don’t always need a backend
You protect your backend by reducing the number of calls reaching it
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
copilot init
Release - Easily Setup CI/CD pipeline to automatically deploy your containerized application
In local try to access http://localhost:3000, It should return below response if all good.
{
"message": "NodeJs App Running on Amazon ECS Fargate"
}
Deploy to Amazon ECS Fargate with AWS Copilot
copilot init
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.
{
"message": "NodeJs App Running on Amazon ECS Fargate"
}
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
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
copilot pipeline init
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
copilot app delete
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.