Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the url that invoked my lambda function

I use the serverless framework to deploy my lambda functions. Serverless uses API Gateway to make my endpoints. I know that it's possible to pass the parameters in methods such as GET and use it in lambda by using the event object. The frontend is already deployed and they were using the following URL to call an endpoint:

example.com/api/EG43

`exports.myHandler = async function(event, context) {
        ...

        // somehow access the URL which was used by the frontend 
        // to grab that EG43 and then return the result based on that key.
        // return information to the caller.  
 }

which EG43 is a key that the previous backend was returning the result based on that key. My question is that if it's possible to somehow know what the URL is. the following doc from AWS shows the parameters that can be read by using the handler arguments, but it doesn't have URL.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

like image 827
Mason Avatar asked Apr 08 '18 07:04

Mason


People also ask

How do you call a Lambda function from a URL?

Function URLs are dual stack-enabled, supporting IPv4 and IPv6. After configuring your function URL, you can invoke your function through its HTTP(S) endpoint via a web browser, curl, Postman, or any HTTP client. To invoke a function URL, you must have lambda:InvokeFunctionUrl permissions.

What is function URL in AWS Lambda?

A function URL is a dedicated HTTP(S) endpoint for your Lambda function. You can create and configure a function URL through the Lambda console or the Lambda API. When you create a function URL, Lambda automatically generates a unique URL endpoint for you. Once you create a function URL, its URL endpoint never changes.


1 Answers

You should be able to access these details two ways:

a. Setup a mapping template, and pass in the details you need as part of the payload. The mapping template is a velocity template with certain variables accessible. See this for details of what you can access.

b. You can setup the lambda in Proxy Integration mode. With this you have access to the raw request. See this for more details.

Here's how you should setup the Lambda in Proxy Integration Mode:

Lambda Proxy Integration Setup

And when the Lambda is setup in Proxy Integration way here's what you get in the event:

{
    "resource": "/abc/version/test",
    "path": "/abc/version/test",
    "httpMethod": "GET",
    "headers": null,
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "path": "/abc/version/test",
        "accountId": "1234",
        "resourceId": "resourceId",
        "stage": "Stage",
        "requestId": "AWS Request ID",
        "identity": {
            "cognitoIdentityPoolId": null,
            "cognitoIdentityId": null,
            "apiKey": "API Key",
            "cognitoAuthenticationType": null,
            "userArn": "arn:aws:iam::<acc_Id>:user/yogesh",
            "apiKeyId": "api-key-id",
            "userAgent": "aws-internal/3",
            "accountId": "<AccId>",
            "caller": "<Some caller ID>",
            "sourceIp": "test-invoke-source-ip",
            "accessKey": "<Access Key>",
            "cognitoAuthenticationProvider": null,
            "user": "<Some User Id>"
        },
        "resourcePath": "/abc/version/test",
        "httpMethod": "GET",
        "extendedRequestId": "test-invoke-extendedRequestId",
        "apiId": "API ID, is typically the API GW ID"
    },
    "body": null,
    "isBase64Encoded": false
}
like image 190
Yogesh_D Avatar answered Oct 10 '22 13:10

Yogesh_D