首页 文章

阻止CloudFormation从CloudFront中删除Lambda Edge关联

提问于
浏览
4

我正在使用 CloudFormation 来管理 CloudFront distribution .

在这个 CloudFront distribution 我已经关联 Lambda Edge function (不使用CloudFormation) .

问题是稍后,当我使用相同的CloudFormation堆栈更新CloudFront分配时,它 removes all Lambda Edge associations .

怎么预防?

那真的很不幸......

PS:有时CloudFormation会删除Lambda关联(例如更新证书ARN时),有时则不会 .

编辑:我可以尝试使用https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html,但有一个不那么hacky的方式?不..

编辑:AWS论坛上的相同问题https://forums.aws.amazon.com/thread.jspa?threadID=274111(需要登录)

1 回答

  • 0

    可以使用CloudFormation为CloudFront“部署”新的Lambda @ Edge功能 .

    CloudFront和Lambda @ Edge需要版本化的Lambda函数 . 因此,您需要确保CloudFront模板发布Lambda代码更改的新版本,并确保您的Distribution使用版本别名 .

    有关工作示例,请参阅我的boilerplate Go/Lambda应用中的CloudFormation template

    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
    

相关问题