可以使用 CloudFormation 为 CloudFront“部署”新的 Lambda@Edge 函数。
CloudFront 和 Lambda@Edge 需要版本化的 Lambda 函数。因此,您需要确保您的 CloudFront 模板发布有关 Lambda 代码更改的新版本,并且您的分配使用版本别名。
有关工作示例,请参阅我的样板 Go/Lambda应用程序中的CloudFormation 模板:
WebAuthFunction:
Properties:
AutoPublishAlias: Live
CodeUri: ./web/handlers/auth/index.zip
Environment: !Ref AWS::NoValue
FunctionName: !Sub ${AWS::StackName}-WebAuthFunction
Handler: index.handler
Role: !GetAtt WebAuthFunctionRole.Arn
Runtime: nodejs6.10
Type: AWS::Serverless::Function
WebDistribution:
Condition: WebDomainNameSpecified
Properties:
DistributionConfig:
Aliases:
- !Ref WebDomainName
Comment: !Sub Distribution for ${WebBucket}
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
Compress: true
ForwardedValues:
Cookies:
Forward: none
QueryString: true
LambdaFunctionAssociations:
- !If
- OAuthClientIdSpecified
- EventType: viewer-request
LambdaFunctionARN: !Ref WebAuthFunction.Version
- !Ref AWS::NoValue
TargetOriginId: !Ref WebBucket
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
Enabled: true
HttpVersion: http2
Origins:
- DomainName: !Sub ${WebBucket}.s3.amazonaws.com
Id: !Ref WebBucket
S3OriginConfig:
OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${WebOriginAccessIdentity}
PriceClass: PriceClass_All
ViewerCertificate:
AcmCertificateArn: !Ref WebCertificate
SslSupportMethod: sni-only
Type: AWS::CloudFront::Distribution