Last active
January 3, 2024 16:55
-
-
Save maguzzi/396a32defa3b4cf66d4286fa71203264 to your computer and use it in GitHub Desktop.
Template for lambda that invalidates the cloudfront cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: "2010-09-09" | |
Description: Lambda action cloudfront cache invalidation stack | |
Parameters: | |
Stage: | |
Type: String | |
AllowedValues: | |
- dev | |
- prod | |
CloudformationExportVar: | |
Type: String | |
ZipDate: | |
Type: String | |
Resources: | |
LambdaActionInvalidateCloudfrontDistribution: | |
Properties: | |
Runtime: python3.8 | |
Handler: index.lambda_handler | |
Code: | |
S3Bucket: "lambda-artifacts-bucket-maguzzi" | |
S3Key: !Join [ "", [ "lambda-cloudfront-invalidate-", !Ref Stage, "-", !Ref ZipDate ,".zip"] ] | |
Role: !GetAtt LambdaActionInvalidateCloudfrontDistributionRole.Arn | |
Environment: | |
Variables: | |
CLOUDFRONT_DISTRIBUTION_ID: | |
Fn::ImportValue: | |
!Ref CloudformationExportVar | |
Type: "AWS::Lambda::Function" | |
LambdaActionInvalidateCloudfrontDistributionRole: | |
Type: "AWS::IAM::Role" | |
Properties: | |
RoleName: !Join [ "-", [ "InvalidateCloudfrontDistributionRole", !Ref Stage ] ] | |
AssumeRolePolicyDocument: | |
Version: "2012-10-17" | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
Action: "sts:AssumeRole" | |
Policies: | |
- PolicyName: InvalidateCloudfrontDistributionPolicy | |
PolicyDocument: | |
Version: "2012-10-17" | |
Statement: | |
- Effect: Allow | |
Action: | |
- cloudfront:CreateInvalidation | |
- codepipeline:PutJobFailureResult | |
- codepipeline:PutJobSuccessResult | |
Resource: "*" | |
ManagedPolicyArns: | |
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | |
Outputs: | |
LambdaActionInvalidateCloudfrontDistributionOutput: | |
Value: !Ref LambdaActionInvalidateCloudfrontDistribution | |
Description: "Name of the created Lambda Action Invalidate Cloudfront Distribution" | |
Export: | |
Name: !Join ["-",[!Sub "${AWS::StackName}-LambdaName",!Ref Stage]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment