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

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 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?

1 Like

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

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

    handler: functions/get/getProfile.getProfile
        - functions/get/getProfile.js
        - functions/**
      - http:
          path: profile
          method: get
            type: COGNITO_USER_POOLS
              "Fn::ImportValue": ${self:custom.serviceName}:cognito-authorizer
            origin: '*'
              - 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
            passThrough: WHEN_NO_TEMPLATES
              application/json: ${file(../gateway/templates/genericMappingTemplate.vtl)}
                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

1 Like

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

    pattern: '.*"error".*'
    template: $input.path('$.errorMessage')

and my lambda returns:

const response = {
    error: 'some error',

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

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

my lambda returns this

catch (error) {
        console.log('original error', error);
        const customError = {
            code: error.code,
            type: 'customLambdaError',
            message: "Unable to get group"

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

1 Like

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

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?

1 Like

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:

              "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:

    pattern: '.*"error".*'
    template: $input.path('$.errorMessage')

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

1 Like

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

Awesome. Easily missed!

1 Like