0

我有以下爲我apigateway模板GET方法配置cloudformation如何自定義效應初探在cloudformation爲apigateway

 "paths": { 
     "/customer/{customerid}": { 
      "get": { 
      "description": "Returns JSON customer objects from DynamoDB.", 
      "parameters": [ 
       { 
       "required": true, 
       "type": "string", 
       "name": "customerid", 
       "in": "path" 
       } 
      ], 
      "produces": [ 
       "application/json" 
      ], 
      "x-amazon-apigateway-integration": { 
       "passthroughBehavior": "never", 
       "responses": { 
       "default": { 
        "statusCode": "200" 
       } 
       }, 
       "uri": { 
       "Fn::Join": [ 
        ":", 
        [ 
        "arn", 
        "aws", 
        "apigateway", 
        { 
         "Ref": "AWS::Region" 
        }, 
        "dynamodb", 
        "action/GetItem" 
        ] 
       ] 
       }, 
       "httpMethod": "POST", 
       "requestTemplates": { 
       "application/json": "{\n \"TableName\": \"customer\",\n \"Key\": {\n \"customerid\": {\n  \"S\": \"$input.params('customerid')\"\n }\n }\n}\n" 
       }, 
       "credentials": { 
       "Fn::GetAtt": [ 
        "TableAccessRole", 
        "Arn" 
       ] 
       }, 
       "type": "aws" 
      }, 
      "consumes": [ 
       "application/json" 
      ], 
      "responses": { 
       "200": { 
       "description": "200 response" 
       } 
      } 
      } 
     } 

被完美創建的API,但是,該API的響應

{ 
    "Item": { 
    "Name": { 
     "S": "Alex" 
    }, 
    "CustomerId": { 
     "S": "123" 
    } 
    } 
} 

但我想這是一個簡單的JSON像

{ 
    "Name":"Alex", 
    "CustomerId":"123" 
} 

我正在查看aws文檔,但我無法弄清楚我的配置的哪個部分需要更改。我知道我有input變量,我可以用它來獲取數據,但在何處以及如何,我這樣失去

回答

0

由於現有的模板使用x-amazon-apigateway-integrationSwagger extension,你可以添加一個responseTemplates對象包含您迴應映射模板到現有的defaultresponse您已經定義:

"x-amazon-apigateway-integration": { 
    "passthroughBehavior": "never", 
    "responses": { 
    "default": { 
     "statusCode": "200", 
     "responseTemplates": { 
     "application/json": "{\"Name\": \"$input.path('$.Item.Name.S')\", \"CustomerId\": \"$input.path('$.Item.CustomerId.S')\"}" 
     } 
    } 
    }, 
    [...] 
相關問題