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:
  1. Open the Amazon ECS console and click on "Task Definitions" in the left-hand navigation panel.
  2. Click the "Create new Task Definition" button and select "Fargate" as the launch type.
  3. 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.
  4. Configure any additional settings for your container, such as port mappings, environment variables, or volumes.
  5. Click "Create" to create the task definition.
  6. Next, click on "Clusters" in the left-hand navigation panel and click the "Create Cluster" button.
  7. Choose "Networking Only" as the cluster type and click "Next step".
  8. Enter a name for your cluster and select "Fargate" as the launch type.
  9. Choose the VPC and subnets you want to use for your Fargate service.
  10. Click "Create" to create the cluster.
  11. Once your cluster is created, click on the "Services" tab and click the "Create" button to create a new service.
  12. Select your Fargate task definition and choose "FARGATE" as the launch type.
  13. Specify the number of tasks you want to run and the VPC and subnets you want to use.
  14. Under "Load balancing", choose "Application Load Balancer" and configure the load balancer settings as needed.
  15. 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:
  1. Open the Amazon CloudFront console and click the "Create Distribution" button.
  2. Select "Web" as the delivery method.
  3. Configure the settings for your distribution, such as the origin domain name and cache behavior settings.
  4. Choose "Custom SSL Certificate" and select the SSL/TLS certificate you want to use.
  5. 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:
  1. Open the AWS Lambda console and click the "Create Function" button.
  2. Choose "Author from scratch" and enter a name for your function.
  3. Choose "Node.js 14.x" as the runtime.
  4. Under "Permissions", choose "Create a new role with basic Lambda permissions".
  5. Click "Create function" to create your Lambda function.
  6. 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 response
    headers['x-custom-header'] = [{ key: 'X-Custom-Header', value: 'Hello from Lambda@Edge' }];

    // Modify the response body
    const body = response.body;
    const modifiedBody = body.replace(/<h1>My Web Server<\/h1>/, '<h1>My Custom Web Server</h1>');
    response.body = modifiedBody;

    callback(null, response);
};
  1. Click "Deploy" to deploy your Lambda@Edge function.
  2. Open the Amazon CloudFront console and


Some Basic References :
  



Comments

Popular posts from this blog

Remote Friendly Companies

GitHub Actions: A Comprehensive Guide to Automation from Scratch

Introduction to Istio, Kiali, Jaeger, Grafana, and Prometheus