Lamda@Edge + Cloudfront + ECS
AWS Fargate:
Fargate is a serverless compute engine for containers provided by AWS. It allows you to run containers without having to manage the underlying infrastructure, such as EC2 instances. With Fargate, you can deploy and manage containers at scale, without worrying about capacity planning, patching, or infrastructure management. Fargate is designed to integrate with other AWS services, such as Amazon ECS and Amazon EKS, to provide a complete container management solution.
Amazon CloudFront:
CloudFront is a global content delivery network (CDN) provided by AWS. It allows you to distribute your content to users around the world with low latency and high data transfer speeds. CloudFront works by caching content at edge locations, which are distributed globally, so that users can access the content from a nearby location. CloudFront supports a variety of content types, including static and dynamic content, streaming media, and APIs.
AWS Lambda@Edge:
Lambda@Edge is a serverless compute service provided by AWS that allows you to run Lambda functions at edge locations in the CloudFront CDN. This enables you to add custom logic to CloudFront, such as modifying requests and responses, adding security headers, or personalizing content for users. Lambda@Edge is designed to be integrated with other AWS services, such as API Gateway and DynamoDB, to provide a complete serverless solution for edge computing.
A sample web server container in Fargate with CloudFront and Lambda@Edge using the AWS Management Console.
First, let's set up the Fargate service:
- Open the Amazon ECS console and click on "Task Definitions" in the left-hand navigation panel.
- Click the "Create new Task Definition" button and select "Fargate" as the launch type.
- Enter a name for your task definition and specify the container image you want to use for your web server. For example, you can use the "nginx" container image from Docker Hub.
- Configure any additional settings for your container, such as port mappings, environment variables, or volumes.
- Click "Create" to create the task definition.
- Next, click on "Clusters" in the left-hand navigation panel and click the "Create Cluster" button.
- Choose "Networking Only" as the cluster type and click "Next step".
- Enter a name for your cluster and select "Fargate" as the launch type.
- Choose the VPC and subnets you want to use for your Fargate service.
- Click "Create" to create the cluster.
- Once your cluster is created, click on the "Services" tab and click the "Create" button to create a new service.
- Select your Fargate task definition and choose "FARGATE" as the launch type.
- Specify the number of tasks you want to run and the VPC and subnets you want to use.
- Under "Load balancing", choose "Application Load Balancer" and configure the load balancer settings as needed.
- Click "Create Service" to create the Fargate service.
Your web server container should now be running in Fargate, and you can access it using the DNS name of the load balancer.
Next, let's set up CloudFront to serve your web server content:
- Open the Amazon CloudFront console and click the "Create Distribution" button.
- Select "Web" as the delivery method.
- Configure the settings for your distribution, such as the origin domain name and cache behavior settings.
- Choose "Custom SSL Certificate" and select the SSL/TLS certificate you want to use.
- Click "Create Distribution" to create the CloudFront distribution.
Your CloudFront distribution is now ready to serve your web server content.
Finally, let's set up a Lambda@Edge function to customize the response from your web server:
- Open the AWS Lambda console and click the "Create Function" button.
- Choose "Author from scratch" and enter a name for your function.
- Choose "Node.js 14.x" as the runtime.
- Under "Permissions", choose "Create a new role with basic Lambda permissions".
- Click "Create function" to create your Lambda function.
- Copy and paste the following code into the function editor:
exports.handler = (event, context, callback) => {const response = event.Records[0].cf.response;const headers = response.headers;// Add a custom header to the responseheaders['x-custom-header'] = [{ key: 'X-Custom-Header', value: 'Hello from Lambda@Edge' }];// Modify the response bodyconst body = response.body;const modifiedBody = body.replace(/<h1>My Web Server<\/h1>/, '<h1>My Custom Web Server</h1>');response.body = modifiedBody;callback(null, response);};
- Click "Deploy" to deploy your Lambda@Edge function.
- Open the Amazon CloudFront console and
Some Basic References :
https://engineering.resolvergroup.com/2020/08/implementing-http-basic-auth-for-fargate-using-lambdaedge-and-cloudfront/
Comments
Post a Comment