API Gateway with Lambda Examples

This is the third article in the API Gateway and Lambda series. For a beginner level introduction, refer the ‘Exposing AWS Lambda functions with the API Gateway‘ and for a description about Lambda proxy integration refer to the intermediate article ‘What is Lambda Proxy Integration

In this article, we will discuss common use cases of API Gateway and Lambda with actual examples. You can refer the full source code in our GitHub repository at https://github.com/asankha/aws-lambda-samples

How to access the request path and method from Lambda?

curl -sD - 'https://vuwibphjt1.execute-api.us-east-1.amazonaws.com/dev/03-lambda-query-params'

REST APIs

exports.handler = async (event) => {
    console.log("Method : " + event.httpMethod);
    console.log("Path  : " + event.path);
    return {
        "body": JSON.stringify({"message": "Successfully executed"})
    };
};

HTTP APIs

exports.handler = async (event) => {
    console.log("Method : " + event.requestContext.http.method);
    console.log("Path : " + event.rawPath);
    return {"message": "Successfully executed"};
};

How to access HTTP Query Parameters from Lambda?

Assuming your request had two query parameters name and age, you can access these from within your Lambda function as follows, irrespective of the APIs being REST or HTTP.

curl -sD - 'https://vuwibphjt1.execute-api.us-east-1.amazonaws.com/dev/03-lambda-query-params?name=John&age=34'
exports.handler = async (event) => {
    console.log("Name : " + event.queryStringParameters.name);
    console.log("Age  : " + event.queryStringParameters.age);
    return {
        "body": JSON.stringify({"message": "Successfully executed"})
    };
};

How can I access a custom request HTTP header from my Lambda function?

curl -sD - -H ‘X-Token: 3456’  'https://vuwibphjt1.execute-api.us-east-1.amazonaws.com/dev/03-lambda-query-params'
exports.handler = async (event) => {
    console.log("X-Token : " + event.headers['X-Token']);
    return {
        "body": JSON.stringify({"message": "Successfully executed"})
    };
};

HTTP APIs

Note that event header names are always converted to lower case for HTTP APIs.

How do I access the payload in a Lambda function with API Gateway?

This depends on the type of API being used. Let us consider a request with a JSON payload as follows.

{ "name":"John", "age":31, "city":"New York" }

REST API without Proxy Integration

exports.handler = async (event) => {
    console.log("Name: " + JSON.stringify(event.name));
    console.log("Age : " + JSON.stringify(event.age));
    console.log("City: " + JSON.stringify(event.city));
    return {"message": "Successfully executed"};
};

REST API with Proxy Integration

exports.handler = async (event) => {
    let data = JSON.parse(event.body);
    console.log("Name: " + JSON.stringify(data.name));
    console.log("Age : " + JSON.stringify(data.age));
    console.log("City: " + JSON.stringify(data.city));

    return {
        "body": JSON.stringify({"message": "Successfully executed"})
    };
};

HTTP API

exports.handler = async (event) => {
    let data = JSON.parse(new Buffer(event.body, 'base64').toString('utf-8'));
    console.log("Name: " + JSON.stringify(data.name));
    console.log("Age : " + JSON.stringify(data.age));
    console.log("City: " + JSON.stringify(data.city));

    return {
        "body": JSON.stringify({"message": "Successfully executed"})
    };
};

How do I enable CORS for an API?

When your API’s resources receive requests from a domain other than the API’s own domain, you must enable cross-origin resource sharing (CORS) for selected methods on the resource. This amounts to having your API respond to the OPTIONS pre-flight request with at least the following CORS-required response headers:
• Access-Control-Allow-Methods
• Access-Control-Allow-Headers
• Access-Control-Allow-Origin

Simply enabling CORS from the API Gateway trigger configuration of the Sigma IDE does what’s required. However, when Lambda proxy integration is also enabled, you would need to set at least the ‘Access-Control-Allow-Origin’ on the response manually. See this thread on StackOverflow for reference.