When an index* file is created/updated on a S3 bucket, this NodeJS based AWS lambda function finds a distribution that uses the bucket as a origin and creates a wildcard parametrized CloudFront Invalidation.
If you are not using apex or serverless to manage AWS Lambda functions you can install it by:
-
Installing the dependencies
npm install
-
Zip it and upload the whole zip to AWS Lambda.
-
Create the role for the AWS lambda. For CloudFront the next role policy:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "cloudfront:CreateInvalidation", "cloudfront:ListDistributions", "cloudfront:ListDistributionsByWebACLId", "cloudfront:ListInvalidations" ], "Resource": [ "*" ] } ] }
And for S3 and logging the role policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::*"
]
}
]
}
-
The handler
s3-bucket-cf-invalidation.handler
-
For the event source, select the S3 bucket(s) related.