Hamed Minaee
Hamed Minaee

Reputation: 2560

How to add template to body Mapping Templates of APIgateway(in integrationResponse)in cloudformation

I am using api gateway and in the body mapping teplate of my integration response I have :

#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))
#set ($bodyObj = $util.parseJson($input.body))

 {
  "searchObjects":"$input.body",
  "statusCode":"$errorMessageObj.statusCode",
  "message":"$errorMessageObj.message"

 }

Now in the api gateway section of my code I have :

GetMethod:
  Type: AWS::ApiGateway::Method
  DependsOn: APIGatewayToLambdaPermission
  Properties:
    AuthorizationType: NONE
    HttpMethod: GET
    Integration:
      Type: AWS
      IntegrationHttpMethod: POST
      Uri:
        Fn::Join:
        - ''
        - - 'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/'
          - Fn::ImportValue: !Sub ${project}-${EnvironmentApp}-lambda-es-
            search
          - "/invocations"
      IntegrationResponses:
      - StatusCode: 200
        ResponseParameters:
          method.response.header.Access-Control-Allow-Origin: "'*'"

I have no idea how to add this to my cloud formation:

    #set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))
#set ($bodyObj = $util.parseJson($input.body))

 {
  "searchObjects":"$input.body",
  "statusCode":"$errorMessageObj.statusCode",
  "message":"$errorMessageObj.message"

 }

Here is my api gateway set up:

enter image description here

Any idea?

Upvotes: 2

Views: 1887

Answers (1)

Dmitry Grinko
Dmitry Grinko

Reputation: 15212

If you worked with AWS console you can go to

API Gateway > api > stage > tab Export > choose Export as Swagger + API Gateway Extensions > click on YAML

Than you can see

httpMethod: "POST"
        requestTemplates:
          application/json: "#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))\n\
            #set ($bodyObj = $util.parseJson($input.body))\n\n {\n  \"searchObjects\"\
            :\"$input.body\",\n  \"statusCode\":\"$errorMessageObj.statusCode\",\n\
            \  \"message\":\"$errorMessageObj.message\"\n }"
        contentHandling: "CONVERT_TO_TEXT"
        type: "aws"

Hope it helps

Upvotes: 5

Related Questions