嵌套堆栈不使用 RequestMappingTemplateS3Loc​​ation 检测模板中的更改

Nested stack does not detect changes in the template with RequestMappingTemplateS3Location

我有一个带有嵌套堆栈的无服务器规范,我想使用 RequestMappingTemplateS3Loc​​ation 和 ResponseMappingTemplateS3Loc​​ation 定义类型:AWS :: AppSync :: Resolver,模板位于 s3 中。当我更新模板时,堆栈不会更新 cloudformation。

 Resource:
    AppSyncResolverTestStack:
      Type: AWS::CloudFormation::Stack
      DependsOn:
        - GraphQlApi
        - GraphQlSchema
      Properties:
        Parameters:
          MappingTemplatesURL:
            Fn::Join:
              - "/"
              - - "s3:/"
                - ${self:provider.deploymentBucket}
                - 'etc'
                - ${opt:stage}
                - 'mapping_templates_extra'
          GraphQlApiId:
            Fn::GetAtt:
              - GraphQlApi
              - ApiId
        TemplateURL:
          Fn::Join:
            - "/"
            - - "https://s3.amazonaws.com"
              - ${self:provider.deploymentBucket}
              - 'etc'
              - ${opt:stage}
              - 'cf-resolvers-2.yml'

嵌套

Parameters:
  MappingTemplatesURL:
    Type: String
  GraphQlApiId:
    Type: String
Resources:
  FCSYSAPIGraphQlResolverFinancialRequest:
    Type: AWS::AppSync::Resolver
    Properties:
      ApiId:
        Ref: GraphQlApiId
      TypeName: Mutation
      FieldName: FinanceDocumentsApi
      DataSourceName: "FCFinanceApi"
      RequestMappingTemplateS3Location:
        Fn::Join:
          - "/"
          - - Ref: MappingTemplatesURL
            - "fc-finance"
            - "FinanceDocuments.request.vm"
      ResponseMappingTemplateS3Location:
        Fn::Join:
          - "/"
          - - Ref: MappingTemplatesURL
            - "fc-finance"
            - "FinanceDocuments.response.vm"

我希望当我更新 s3 中的模板并部署我的项目时,cloudformation 会更新,但会使用以前的代码进行维护。

这是 CloudFormation 的正常行为。 CloudFormation 仅在其属性更改时更新资源

由于属性 RequestMappingTemplateS3LocationResponseMappingTemplateS3Location 没有改变,CloudFormation 不会更新您的 AppSync 解析器(即使这些 S3 位置指向 "new" 内容)。

解决问题的一种方法是使用 AWS CLI 的 aws cloudformation package 命令。它允许您使用本地文件定义模板:

Type: 'AWS::AppSync::Resolver'
Properties:
  ...
  RequestMappingTemplateS3Location: './path/to/local/template/file'
  ...

运行

aws cloudformation package --template-file mytemplate.yml --s3-bucket mybucket --output-template-file packaged.template

returns 模板副本 (packaged.template),将对本地工件的引用替换为命令上传工件的 S3 位置。 S3 位置名称(密钥)取决于内容(使用 MD5)。因此,使用此策略,如果 S3 位置引用的内容发生变化,属性 RequestMappingTemplateS3Location 也会发生变化。

之后,您可以使用 aws cloudformation deploy 部署您的模板。

注意:这与使用AWS SAM CLI相同,sam packageaws cloudformation package

的别名

如果使用无服务器框架,另一种解决方案是使用 serverless-appsync-plugin,它允许内联或在文件中指定映射模板。