Handling Lambda Proxy Integration

20 July, 2021
Back

Lambda Proxy Integration allows you to expose your Lambda function to the internet.

You can make a GET or POST request to insert values into the Lambda to process.

In this use case, Lambda will generate a short UID based on the length requested. For example 4 will return xtgh and 10 will return hjdyafkxwm.

Handling GET requests

Making a GET request to example endpoint https://ioskbr5q7c.execute-api.ap-southeast-1.amazonaws.com/prd/generate?gen=8 will return a UID kpuqtdxn.

If ?gen=8 is not specified in the query parameters, the default character length will be 4.

Lambda function:

exports.handler = async (e) => {

    const set = "abcdefghjkmnprstuvwxyz";
    const setlength = set.length;
    var code = "";
    var len = 0

    if (e.queryStringParameters && e.queryStringParameters.gen){
        var len = e.queryStringParameters.gen
    } else {
        var len = 4
    }

    
    for (var i = 0; i < len; i++) {
      const digit = Math.floor(Math.random() * Math.floor(setlength));
      code += set.substr(digit - 1, 1);
    }
    
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify(code),
    };
    return response;
};

Handling POST requests

Making a POST request to example endpoint https://ioskbr5q7c.execute-api.ap-southeast-1.amazonaws.com/prd/generate will return a UID thjx.

The body of the POST request must specify {"gen":4}.

Lambda function:

exports.handler = async (event) => {
    
    const set = "abcdefghjkmnprstuvwxyz";
    const setlength = set.length;
    var code = "";
    var len = JSON.parse(event.body).gen
    console.log(len)

    for (var i = 0; i < len; i++) {
      const digit = Math.floor(Math.random() * Math.floor(setlength));
      code += set.substr(digit - 1, 1);
    }
    
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify(code),
    };
    return response;
};

Setup

Setup steps for Lambda and API Gateway are in Be A Better Dev.

Reference docs for Lambda code.


Back