Skip to content

Instantly share code, notes, and snippets.

@deepak7093
Last active October 20, 2019 16:19
Show Gist options
  • Save deepak7093/7dde29ff02d69e2f5ca0ad0c86db1da7 to your computer and use it in GitHub Desktop.
Save deepak7093/7dde29ff02d69e2f5ca0ad0c86db1da7 to your computer and use it in GitHub Desktop.
Lambda Edge Sample Deployment Guide

Deploy Lambda@edge

Before deploy functions to lambda@edge, we have to meet some prerequisites as below:

  1. S3 bucket with public website hosting and set index.html as base path.
  2. Cloudfront distribution to server content for lambda@edge.
  3. Setup webserver on EC2 instance.
  4. Create lambda function.
  5. Setup lambda@edge trigger.
  6. Validation.

Create S3 Bucket with public website hosting

  1. Create S3 bucket and add enable static website hosting.
  2. Add below bucket policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<bucket_name>/*"
        }
    ]
}
  1. Now upload index.html and wordDoc.doc.html to s3.
  2. Update static website hosting base path to index.html

Create cloudfront distrubution

  1. Create cloudfront distrution with above create s3 as origin. keep other setting as default
  2. Check by entering distrubution url if it returns s3 index.html page

Setup webserver on EC2 instance

  1. Create EC2 instance with ubuntu 18.04 and public ip.
  2. Login to EC2 instance using public ip and private key.
  3. Install nginx using below command
# sudo -i
# apt-get update
# apt-get install -y nginx
# systemctl start nginx.service
# systemctl enable nginx.service
# touch /var/www/html/index.html
# echo "Sample Page from EC2 nginx webserver" >  /var/www/html/index.html
  1. Allow port 80 and 443 instance security group.

Create lambda function

  1. Create lambda function with runtime python 3.7 .
  2. Update code from file lambda_handler.py
  3. Create role as lambda-edge with below policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:*:*:*"
            ]
        }
    ]
}
  1. Attach above created role to lambda function.
  2. Update EC2 public ip inside lambda_handler.py.
  3. IAM Role
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "lambda.amazonaws.com",
          "edgelambda.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Setup lambda@edge trigger

  1. Publish new version of lambda.
  2. On new publish version Add trigger --> Choose cloud front --> Deploy to Lambda@Edge --> select distribution --> choose event viwer request --> confirm

Validate

  1. Open in browser <cloud_front_distrinution_url>/webPage.aspx it should open page from EC2.
  2. <cloud_front_distrinution_url>/wordDoc.doc it should open page from S3 bucket.
  3. <cloud_front_distrinution_url>/ it should get 503 Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment