代码之家  ›  专栏  ›  技术社区  ›  Paul D'Ambra

如何解决AWS SAM模板中的循环依赖关系

  •  1
  • Paul D'Ambra  · 技术社区  · 6 年前

    我有一个sam模板

    AWSTemplateFormatVersion : '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    
    Description: |
      Some infrastructure
    
    Resources:
      S3HomeBucket:
        Type: 'AWS::S3::Bucket'
        Properties:
          AccessControl: PublicRead
          BucketName: the-site-home
        DeletionPolicy: Retain
      BucketPolicy:
        Type: 'AWS::S3::BucketPolicy'
        Properties:
          PolicyDocument:
            Id: S3HomeBucketPolicy
            Version: 2012-10-17
            Statement:
              - Sid: PublicReadForGetBucketObjects
                Effect: Allow
                Principal: '*'
                Action: 's3:GetObject'
                Resource: !Join
                  - ''
                  - - 'arn:aws:s3:::'
                    - !Ref S3HomeBucket
                    - /*
          Bucket: !Ref S3HomeBucket
      homePageDistribution:
        Type: AWS::CloudFront::Distribution
        Properties:
          DistributionConfig:
            Origins:
            - DomainName: !Join [ "", [!Ref S3HomeBucket, ".s3.amazonaws.com"]]
              Id: myS3Origin
              S3OriginConfig:
                OriginAccessIdentity: origin-access-identity/cloudfront/my-id
            Enabled: 'true'
            Comment: the static home page cdn
            DefaultRootObject: index.html
            Aliases:
            - the.info
            DefaultCacheBehavior:
              AllowedMethods:
              - GET
              - HEAD
              - OPTIONS
              TargetOriginId: myS3Origin
              ForwardedValues:
                QueryString: 'false'
                Cookies:
                  Forward: none
              ViewerProtocolPolicy: allow-all
            PriceClass: PriceClass_100
            ViewerCertificate:
              CloudFrontDefaultCertificate: 'true'
      CloudfrontInvalidatingFunction:
        Type: AWS::Serverless::Function
        Properties:
          Runtime: nodejs8.10
          Handler: invalidateStaticFiles.handler
          Timeout: 60
          Policies:
            - AWSLambdaExecute
            - Statement:
                - Effect: Allow
                  Action:
                    - 'cloudfront:CreateInvalidation'
                  Resource: !Join
                  - ''
                  - - 'arn:aws:cloudfront:'
                    - !Ref AWS::Region
                    - ':'
                    - !Ref AWS::AccountId
                    - ':'
                    - !Ref homePageDistribution
          Environment:
            Variables:
              DISTRIBUTION_ID: !Ref homePageDistribution
          Events:
            AnyChange:
              Type: S3
              Properties:
                Bucket: !Ref S3HomeBucket
                Events: s3:*
    Outputs:
      SiteBucketName:
        Description: the name of the s3 bucket referenced by cloudfront
        Value: !Ref S3HomeBucket
        Export:
          Name: the-site-home-bucket-name
      CloudFrontId:
        Description: the id of the cloudfront distribution for the
        Value: !Ref homePageDistribution
        Export:
          Name: the-site-cloudfront-distribution-id
    

    运行我得到的:

    未能创建变更集:等待程序ChangeSetCreateComplete失败:等待程序遇到终端故障状态:失败。原因:资源之间存在循环依赖关系:[CloudFrontInvalidTingFunction、BucketPolicy、CloudFrontInvalidTingFunctionAnyChangePermission、S3homeBucket、HomePageDistribution、CloudFrontInvalidTingFunctionRole]

    别想了 那个 this other question 应用

    我真的不明白 this documentation . 情况不一样,但我不明白它到底在提议什么。

    我想做的是:

    • 有一个拥有静态HTML的bucket,
    • 位于其前面的CloudFront分布
    • 以及一个lambda,当文件更改时,该lambda将监视bucket并使缓存失效。

    有可能吗?

    (the serverless application model github project issue template 向这里的人提供帮助,而不是Github问题)

    1 回复  |  直到 6 年前
        1
  •  0
  •   Milan Cermak    6 年前

    我想在这种情况下,如果你只是硬编码 DomainName homePageDistribution 资源——基本上删除 !Ref S3HomeBucket

    您可以使用bucket名称定义一个模板参数,并在整个模板中使用它。

    Parameters:
      ImagesBucketName:
        Default: the-site-home
        Type: String
    
    推荐文章