【问题标题】:Is it possible to reference a CloudFormation resource name in an AppSync resolver?是否可以在 AppSync 解析器中引用 CloudFormation 资源名称?
【发布时间】:2019-02-26 04:30:04
【问题描述】:

我在我的 CloudFormation 模板(用 YAML 编写)中预置了一些 DynamoDB 表资源,我在模板的其他地方预置了一个 AppSync 端点。

我没有指定表的名称,而是让 CloudFormation 为它们生成名称。每当我需要指定表名时,我都会使用

!Ref TableName

我想在 AppSync 中使用 DynamoDB Batch 解析器,这需要我列出表的名称,但是当我更新堆栈时,“!Ref TableName”不会转换为表的名称。解析器以“!Ref TableName”作为表名结束。

有没有办法可以将 CloudFormation 生成的表的名称转换为 AppSync 模板语言?

以下是我的 CloudFormation 模板,已针对相关性进行了修剪:

conversationsTable:
Type: "AWS::DynamoDB::Table"
Properties:
  AttributeDefinitions:
    -
      AttributeName: "id"
      AttributeType: "S"
  KeySchema:
    -
      AttributeName: "id"
      KeyType: "HASH"
  ProvisionedThroughput:
    ReadCapacityUnits: "1"
    WriteCapacityUnits: "1"

userConversationsTable:
Type: "AWS::DynamoDB::Table"
Properties:
  AttributeDefinitions:
    -
      AttributeName: "userId"
      AttributeType: "S"
    -
      AttributeName: "conversationId"
      AttributeType: "S"
  KeySchema:
    -
      AttributeName: "userId"
      KeyType: "HASH"
    -
      AttributeName: "conversationId"
      KeyType: "RANGE"
  ProvisionedThroughput:
    ReadCapacityUnits: "1"
    WriteCapacityUnits: "1"

...

createConversationMutationResolver:
Type: "AWS::AppSync::Resolver"
Properties:
  ApiId: !GetAtt chatQLApi.ApiId
  TypeName: "Mutation"
  FieldName: "createConversation"
  DataSourceName: !GetAtt conversationsTableDataSource.Name
  RequestMappingTemplate: |
    {
      "version" : "2018-05-29",
      "operation" : "BatchPutItem",
      "tables": {
        !Ref conversationsTable : $util.toJson($convoList),
        !Ref userConversationsTable : $util.toJson($users)
      }
    }
  ResponseMappingTemplate: |
    #if($context.error)
      $util.appendError($context.error.message, $context.error.message)
    #end
    {
      "conversation": $util.toJson("${context.result.data}!Ref conversationTable")
      "userConversations": $util.toJson("${context.result.data}!Ref userConversationsTable")
    }

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-appsync


    【解决方案1】:

    阅读 intrinsic functions 上的 CloudFormation 文档后,我能够使用 SubJoin 函数实现我的目标。

    在 RequestMapping 模板中,我使用 Fn::Sub 将两个 VTL 变量设置为表的名称,然后使用 Fn::Join 将字符串连接到模板字符串的其余部分。

    在 ResponseMapping 模板中,我在模板代码中为需要表名的所有内容放置了占位符,并使用 Fn::Sub 对那些进行了子分类。然后为了将表名附加到上下文对象路径,我使用 Fn::Join 完全使用 CloudFormation 模板构建该路径,并将其放入 Fn::Sub 使用的替换映射中。

    下面是我上面的模板,有变化

    createConversationMutationResolver:
    Type: "AWS::AppSync::Resolver"
    Properties:
      ApiId: !GetAtt chatQLApi.ApiId
      TypeName: "Mutation"
      FieldName: "createConversation"
      DataSourceName: !GetAtt conversationsTableDataSource.Name
      RequestMappingTemplate:
        !Join
          - ''
          - - !Sub
               - |
                 #set($conversationTable = "${conversationsTable}")
                 #set($userConversationTable = "${userConversationsTable}")
               - { conversationTable: !Ref conversationsTable, userConversationTable: !Ref userConversationsTable }
            - |
              {
                "version" : "2018-05-29",
                "operation" : "BatchPutItem",
                "tables": {
                  "${conversationTable}" : $util.toJson($convoList),
                  "${userConversationTable}" : $util.toJson($users)
                }
              }
      ResponseMappingTemplate:
        !Sub
          - |
            #if($context.error)
              $util.appendError($context.error.message, $context.error.message)
            #end
            {
              "${conversation}": $util.toJson(${conversationContext})
              "${userConversations}": $util.toJson(${userConversationContext})
            }
          - {
              conversation: !Ref conversationsTable,
              userConversations: !Ref userConversationsTable,
              conversationContext: !Join [ '.', ["$context.result.data", !Ref conversationsTable]],
              userConversationContext: !Join [ '.', ["$context.result.data", !Ref userConversationsTable]]
            }
    

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2019-12-16
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 2021-01-07
      相关资源
      最近更新 更多