【问题标题】:Nested stack does not detect changes in the template with RequestMappingTemplateS3Location嵌套堆栈未使用 RequestMappingTemplateS3Loc​​ation 检测模板中的更改
【发布时间】:2019-09-22 11:59:04
【问题描述】:

我有一个带有嵌套堆栈的无服务器规范,我想使用 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 会更新,但它会使用之前的代码进行维护。

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation serverless-framework


    【解决方案1】:

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

    由于属性 RequestMappingTemplateS3LocationResponseMappingTemplateS3Location 不会更改,CloudFormation 不会更新您的 AppSync Resolver(即使这些 S3 位置指向“新”内容)。

    解决问题的一种方法是使用 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
    

    返回您的模板副本 (packaged.template),用命令上传工件的 S3 位置替换对本地工件的引用。 S3 位置名称(键)取决于内容(使用 MD5)。因此,使用此策略,如果 S3 位置引用的内容发生更改,属性 RequestMappingTemplateS3Location 也会更改。

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

    注意:这和使用AWS SAM CLI是一样的,sam packageaws cloudformation package的别名

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

    【讨论】:

    • 感谢您的回复。这似乎解决了我的问题,但是在运行命令时他告诉我一切都是正确的,但他没有将工件上传到 s3 并且他保留了相同的模板(我只有一个 AWS :: AppSync :: Resolver 资源)
    • 您是否指定了--output-template-file,您在RequestMappingTemplateS3Location 的输出文件中看到了什么?
    • 我得到了相同的地址(RequestMappingTemplateS3Loc​​ation:/home/oreste/Documents/bacardi/training/mapping-templates/getPost.request.vm),当我运行命令时我得到了这个(```成功打包工件并将输出模板写入文件 serverless_result 执行以下命令部署打包模板 aws cloudformation deploy --template-file /home/oreste/Documents/bacardi/training/serverless_result --stack-name ```)
    • 这很奇怪......它应该可以工作。您可以在要点中分享您的输入模板吗?您在serverless_result 文件中看到了什么?
    • 我看到你正在使用无服务器框架和serverless-appsync-plugin,你为什么不使用这个插件的内置功能?您可以指定mappingTemplates 内联或通过文件...更新了我的答案以提及该解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2021-04-30
    • 2021-12-29
    • 2019-03-09
    • 2021-09-16
    相关资源
    最近更新 更多