Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Describe AWS API Gateway Body Mapping Templates in CloudFormation

I looked though the documentation but didn't find a way to do this. I have a API Gateway method that has a Body Mapping Template, as in the picture attached. Method Screenshot

How do I map this template in CloudFormation? (I'm using JSON). I added "PassthroughBehavior": "WHEN_NO_TEMPLATES", but haven't found a way to add the Content-Type mapping.

Thank you.

like image 498
June Avatar asked Dec 23 '22 14:12

June


2 Answers

You can do something like this:

GETMethodRequest:

Type: "AWS::ApiGateway::Method"
DependsOn: ePlanningLambdaPermission
Properties: 
  RestApiId: !Ref YourAPI
  AuthorizationType: NONE
  HttpMethod: GET
  RequestParameters:
    method.request.querystring.name: true
  MethodResponses:
     - StatusCode: 200

  ResourceId: !Ref ePlanningGISLocalitymapResource
  Integration:
    Type: AWS
    IntegrationHttpMethod: POST
    RequestTemplates:
      "application/json": "{
          \"body\" : $input.json('$'),
          \"headers\": {
            #foreach($header in $input.params().header.keySet())
            \"$header\": \"$util.escapeJavaScript($input.params().header.get($header))\" #if($foreach.hasNext),#end

            #end
          },
          \"method\": \"$context.httpMethod\",
          \"params\": {
            #foreach($param in $input.params().path.keySet())
            \"$param\": \"$util.escapeJavaScript($input.params().path.get($param))\" #if($foreach.hasNext),#end

            #end
          },
          \"query\": {
            #foreach($queryParam in $input.params().querystring.keySet())
            \"$queryParam\": \"$util.escapeJavaScript($input.params().querystring.get($queryParam))\" #if($foreach.hasNext),#end

            #end
          }  
        }"
    IntegrationResponses:
     - StatusCode: 302
like image 79
Anoop Avatar answered Jan 04 '23 17:01

Anoop


You can do this as part of the RequestTemplates property described here.

It should look something like this:

"APIMethodGet": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
    "RequestTemplates": {
        "application/json": {
            "Fn::Join": [
                "",
                [
                    "{\n    \"StreamName\": \"my-kinesis\"\n",
                    "\n    \"Data\": \"$util.base64encode($input.body)\"\n",
                    "\n    \"PartitionKey\": \"1\"\n}"
                ]
            ]
        }
    },
    "PassthroughBehavior": "WHEN_NO_TEMPLATES"
}
}
like image 42
EFeit Avatar answered Jan 04 '23 15:01

EFeit