Skip to content

Instantly share code, notes, and snippets.

@chrismckinnel
Created June 12, 2019 09:22
Show Gist options
  • Save chrismckinnel/97196d41d0b639abbd7e9d433b2ae7b4 to your computer and use it in GitHub Desktop.
Save chrismckinnel/97196d41d0b639abbd7e9d433b2ae7b4 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Full stack to demo Lambda@Edge for CloudFront redirects
Parameters:
RedirectLambdaName:
Type: String
Default: redirect-lambda
Resources:
RedirectLambdaFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'lambda.amazonaws.com'
- 'edgelambda.amazonaws.com'
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
RedirectLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambdas/
FunctionName: !Ref RedirectLambdaName
Handler: RedirectLambda.handler
Role: !GetAtt RedirectLambdaFunctionRole.Arn
Runtime: nodejs10.x
AutoPublishAlias: live
CloudFront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: true
TargetOriginId: google-origin
ViewerProtocolPolicy: redirect-to-https
DefaultTTL: 0
MaxTTL: 0
MinTTL: 0
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !Ref RedirectLambdaFunction.Version
Enabled: true
HttpVersion: http2
PriceClass: PriceClass_All
Origins:
- DomainName: www.google.com
Id: google-origin
CustomOriginConfig:
OriginProtocolPolicy: https-only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment