Passing serverless api url as parameter for Lambda function in same stack

I started creating a JAM application using AWS Lambda, AWS API Gateway and without a server, as well as with a different vendor API.

This provider API is called by the Lambda function and requires that the callback URL be passed in to retrieve some data after it has completed its work.

Since I am creating everything without a server, going to the console and extracting the API URL for manual installation, since the env variable is useless to me, and I need a way so that the server server can pass the API open endpoint to the lambda function URL.

How to get a Lambda function of a URI event URI as env or something passing with another Lambda function in the same stack?

Can someone provide some server snippet on how to achieve this? Thank!

+4
source share
1 answer

If you want to find the URL of the API gateway that called the Lambda function, you need to check the variable eventthat your lambda function receives.

event.headers.Host -> abcdefghij.execute-api.us-east-1.amazonaws.com
event.requestContext.stage -> dev
event.requestContext.resourcePath -> my-service/resource

If you want to create an API gateway URL (example: https://abcdefghij.execute-api.us-east-1.amazonaws.com/dev/my-service/resource ), use:

const url = `https://${event.headers.Host}/${event.requestContext.stage}/${event.requestContext.resourcePath}`;

Full test case:

module.exports.hello = (event, context, callback) => {

  const url = `https://${event.headers.Host}/${event.requestContext.stage}/${event.requestContext.resourcePath}`;

  const response = {
    statusCode: 200,  
    headers: {
      'Access-Control-Allow-Origin': '*'
    },  
    body: JSON.stringify({
        message: url
    })
  };

  callback(null, response);
};

. - AWS, , event headers requestContext. , API Gateway URL.

+5

Source: https://habr.com/ru/post/1668489/


All Articles