Make something that works with the minimum configuration when developing with Lambda @ Edge I want to develop while debugging using it Language is Python 3.7 (3.8 is not currently supported)
Request → CloudFront → S3 Insert Lamda @ Edge that outputs logs to CloudWatch Logs and does nothing else
Make a suitable bucket with S3 Put an appropriate index.html (this time it looks like this)
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="ja">
<head>
<title>Test homepage</title>
</head>
<body>
<h1>Test homepage</h1>
<p>It is under construction now.
</body>
</html>
Give your bucket public access
Check if index.html can be displayed in the browser from the "object URL" of the bucket
If you can display it, S3 setting is completed Turn on S3 static hosting settings and specify index.html
The more you look up, the more materials you will find, so I will omit it. Check if you can access S3 via CloudFront domain and display it in the browser Complete when displayed
Now you have Request → CloudFront → S3 This is the preparation Lambda @ Edge is sandwiched here
Lambda@Edge First make the ** region (the one next to the logged-in username) Virginia and then ** create a Lambda function
Make the contents of Lambda this
lambda_function.py
import json
def lambda_handler(event, context):
print('# lambda@edge start')
print(event)
#In the case of Viewer Response Origin Response, uncomment this
# cfResponse = event['Records'][0]['cf']['response']
# return cfResponse
#Uncomment this for Viewer Request Origin Request
# cfRequest = event['Records'][0]['cf']['request']
# return cfRequest
・ Lambda @ Edge has required parameters for request and response, otherwise an error will occur.
After confirming that the version is LATEST, select Action → Deploy to Lambda @ Edge Set the ID of CloudFront created in the distribution You can deploy by setting the Lambda @ Edge event you want to set in the CloudFront event and clicking Deploy
Since it is a CDN, there are areas where content distribution has not been completed for a while even after deploying Delivery is complete if the status changes from in Progress to deployed on the CloudFront console It seems that the completion itself was delivered in a few minutes to enough, or in 1 to 3 minutes to Japan.
Now if you look at the Lambda execution log of CloudWatch Logs, you should see the log written in the code All you have to do is debug what you want to do with Lambda @ Edge and implement it on Lambda.
Please note that Lambda @ Edge's CloudWatch Logs are written to different groups according to the following classification (I was most addicted to it) ** Log of tests run from the console ** Region: Virginia Output target: For each function ** Lambda log running on Lambda @ Edge ** Region; Region accessed (almost Tokyo in Japan) Output target: For each function ** Lambda @ Edge log running on Lambda @ Edge ** Region; Region accessed (almost Tokyo in Japan) Output target: For each output target associated with Lambda @ Edge
I hope this memo helps someone, including myself