How to get AWS Lambda Function URL string in code

0

Hi,

Is there a way to get the AWS Lambda Function URL string (or the bits to construct it) programmatically from the running instance of the Lambda itself? I tried the below options and neither of them had the necessary URL:

  1. checked the input object in handleRequest(Object input, Context context)
  2. checked the items in System.getenv()

Thanks

已提问 1 年前1095 查看次数
2 回答
0
已接受的回答

I am not sure why would you need the URL from within the function itself, but you can call the Lambda.get_function_url_config API to get the string you need.

profile pictureAWS
专家
Uri
已回答 1 年前
  • As I mentioned in the above comment, the AWS Lambda is used with 2 events (periodic scheduled and Function URL request) and the periodic event need to send the Function URL endpoint to the external system, so the external system can send updates to the Function URL.

  • I'm accepting this as the answer since it allows me to gather the necessary details of a given AWS Lambda resource

  • I would probably create two functions. The first function is the one that is triggered on a schedule. The second function is the one withe function URL. The first function receives the function URL of the other function as an environment variable. You can create both functions in a single SAM template and there you can reference the URL in the env variables using something like 'Fn::GetAtt: HelloWorldFunctionUrl.FunctionUrl'.

0

The function URL domain and path are present as a part of the event object when the function is triggered from a function URL. You can see the event payload format here [1]. The function URL can be seen in full at event.requestContext.domainName, or just the UUID at event.requestContext.domainPrefix. You could also get it from event.headers.host.

[1] Invoking Lambda function URLs - Request payload format - https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-request-payload

AWS
Kevin_C
已回答 1 年前
profile pictureAWS
专家
已审核 1 年前
  • Yes, I know that these details are available from when triggered as a Function URL, however; that's too late for getting the domain or host details for me. I use the (same) AWS Lambda in 2 distinct ways:

    1. periodically triggered based on cron defined schedule as AWS Lambda. This may kick off an HTTP request to an external system to run some async background processing which will later send updates back to the AWS Lambda Function URL - thus I need to know the Function URL up front so the external system knows where to send updates to.
    2. triggered by incoming request as a Function URL. This will receive the external system generated update.

    Yes I can hardcode the Function URL host in code but since the AWS Lambda can be "configured" with a Function URL, I would have expected that this configuration is available through some sort of generic "event" object or at least as part of the system environment.

  • In that case you would just need to use the API to retrieve it. You can grab the function ARN from the context object or the function name from environment variables and use that to call the GetFunctionUrlConfig API which will return all of the information related to your function URL. In Boto3 for example: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda/client/get_function_url_config.html

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则