# Setting API Gateway Method/Integration Response/Request with Lambda Integration

**URL:** <https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772>\
**Category:** Serverless Framework\
**Tags:** lambda, api-gateway\
**Created:** [December 20, 2018, 2:18pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772 "2018-12-20T14:18:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![GazEdge](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/gazedge/32/2673_2.png) [@GazEdge](https://forum.serverless.com/u/GazEdge)\
**Post date:** [December 20, 2018, 2:18pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/1 "2018-12-20T14:18:11Z")

</div>

Im trying to create and API Gateway Lambda Integration (NOT lambda proxy) so I can use some of the more advanced features of API Gateway (namely the Method Request, Integration Request, Integration Response, Method Response features).

I’ve found this guide [https://serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-integration](https://serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-integration) for serverless, but looking at this and the masses of AWS documentation around these features, I’m totally lost on how to setup what I need as its not obvious to me how the serverless.yml examples in the link above relate to the instructions/documentation provide by AWS.

Can anyone recommend a good worked example of using Lambda Integration with the advance API Gateway features?

---

<div class="post-metadata">

**Author:** ![corydorning](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/corydorning/32/4350_2.png) [@corydorning](https://forum.serverless.com/u/corydorning)\
**Post date:** [June 22, 2020, 4:05pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/2 "2020-06-22T16:05:15Z")

</div>

i’m running into the EXACT same issue. I realize this is 18 months old, but did you ever figure this out?

---

<div class="post-metadata">

**Author:** ![bearclawstu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bearclawstu/32/4301_2.png) [@bearclawstu](https://forum.serverless.com/u/bearclawstu)\
**Post date:** [June 25, 2020, 7:56am UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/3 "2020-06-25T07:56:01Z")

</div>

Here is some sample code that I am using to set up lambda integration. This populates the request and response by using velocity templates.

```auto
functions:
  getProfile:
    handler: functions/get/getProfile.getProfile
    package:
      include:
        - functions/get/getProfile.js
      exclude:
        - functions/**
    events:
      - http:
          path: profile
          method: get
          authorizer:
            type: COGNITO_USER_POOLS
            authorizerId:
              "Fn::ImportValue": ${self:custom.serviceName}:cognito-authorizer
          cors:
            origin: '*'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: false
            cacheControl: 'max-age=600, s-maxage=600, proxy-revalidate' # Caches on browser and proxy for 10 minutes and doesnt allow proxy to serve out of date content
          integration: lambda
          request:
            passThrough: WHEN_NO_TEMPLATES
            template:
              application/json: ${file(../gateway/templates/genericMappingTemplate.vtl)}
          response:
            statusCodes:
              200:
                pattern:''
              400:
                pattern: '.*"customLambdaError".*'
                template: $input.path('$.errorMessage')

```

Let me know if this doesn’t cover everything you need or want more info and I will try to help further

---

<div class="post-metadata">

**Author:** ![corydorning](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/corydorning/32/4350_2.png) [@corydorning](https://forum.serverless.com/u/corydorning)\
**Post date:** [June 25, 2020, 2:27pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/4 "2020-06-25T14:27:45Z")

</div>

what do you have your lambda returning? My serverless response looks like this:

```auto
response:
  400:
    pattern: '.*"error".*'
    template: $input.path('$.errorMessage')

```

and my lambda returns:

```auto
const response = {
    error: 'some error',
    name: event.name
  };
...
context.done(JSON.stringify(response));

```

Unfortunately, it is still being returned as a 200 to the client and not a 400:

```auto
{
    "errorType": "string",
    "errorMessage": "{\"error\":\"error\",\"name\":\"my-event\"}",
    "trace": []
}

```

---

<div class="post-metadata">

**Author:** ![bearclawstu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bearclawstu/32/4301_2.png) [@bearclawstu](https://forum.serverless.com/u/bearclawstu)\
**Post date:** [June 25, 2020, 4:45pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/5 "2020-06-25T16:45:40Z")

</div>

my lambda returns this

```auto
catch (error) {
        console.log('original error', error);
        const customError = {
            code: error.code,
            type: 'customLambdaError',
            message: "Unable to get group"
        };
        context.fail(JSON.stringify(customError));
    }

```

I think setting the context.fail is the key to making it realise it has failed

---

<div class="post-metadata">

**Author:** ![corydorning](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/corydorning/32/4350_2.png) [@corydorning](https://forum.serverless.com/u/corydorning)\
**Post date:** [June 25, 2020, 5:05pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/6 "2020-06-25T17:05:57Z")

</div>

no luck. still getting the same thing. do you have error.code set to 400?

---

<div class="post-metadata">

**Author:** ![bearclawstu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bearclawstu/32/4301_2.png) [@bearclawstu](https://forum.serverless.com/u/bearclawstu)\
**Post date:** [June 25, 2020, 5:24pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/7 "2020-06-25T17:24:06Z")

</div>

No, error.code is just the type of error returned from aws. “AccessDenied” etc.

It’s not because you are missing the statusCodes in your yml is it? Are the response templates showing up in the Aws console?

---

<div class="post-metadata">

**Author:** ![corydorning](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/corydorning/32/4350_2.png) [@corydorning](https://forum.serverless.com/u/corydorning)\
**Post date:** [June 25, 2020, 5:35pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/8 "2020-06-25T17:35:56Z")

</div>

I’m not sure what you mean by ‘it’s not because you are missing the statusCodes in your yml is it?’ but i did look at the cloudformation-template-update-stack.json and see this so i think you are onto something:

```auto
{
              "StatusCode": 400,
              "SelectionPattern": "[\\s\\S]*\\[400\\][\\s\\S]*",
              "ResponseParameters": {
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              },
              "ResponseTemplates": {}
            },

```

Looks like my response templates are empty. but i have the status codes in my serverless.yml as listed above:

```auto
response:
  400:
    pattern: '.*"error".*'
    template: $input.path('$.errorMessage')

```

---

<div class="post-metadata">

**Author:** ![bearclawstu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bearclawstu/32/4301_2.png) [@bearclawstu](https://forum.serverless.com/u/bearclawstu)\
**Post date:** [June 25, 2020, 5:41pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/9 "2020-06-25T17:41:16Z")

</div>

If you look at my example above, after response: I have a line saying statusCodes but you don’t?

---

<div class="post-metadata">

**Author:** ![corydorning](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/corydorning/32/4350_2.png) [@corydorning](https://forum.serverless.com/u/corydorning)\
**Post date:** [June 25, 2020, 5:43pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/10 "2020-06-25T17:43:32Z")

</div>

Doh! lol. That was it. Just fixed. Thanks!!!

---

<div class="post-metadata">

**Author:** ![bearclawstu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bearclawstu/32/4301_2.png) [@bearclawstu](https://forum.serverless.com/u/bearclawstu)\
**Post date:** [June 25, 2020, 5:44pm UTC](https://forum.serverless.com/t/setting-api-gateway-method-integration-response-request-with-lambda-integration/6772/11 "2020-06-25T17:44:59Z")

</div>

Awesome. Easily missed!
