簡體   English   中英

是否可以在 AppSync 解析器中引用 CloudFormation 資源名稱?

[英]Is it possible to reference a CloudFormation resource name in an AppSync resolver?

我在 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")
    }

閱讀有關內在函數的 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]]
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM