2

I have a lambda function which has its api-gateway URL. Also, I have a custom domain URL associated with it in CloudFront (https://abc.def.com/ghi). I can access my lambda using the custom domain. Now as a requirement I am supposed to return the custom domain URL from the lambda function as part of the JSON response. So my response body from the lambda should look like:

    {
    "response": "hi i am coming from lambda",
    "myurl": "https://abc.def.com/ghi"
    }

How do I catch hold of the URL inside lambda function? PS: I am using node 8.10 environment.

1 Answer 1

3

First, I would challenge the requirement. IMHO it does not make sense to return the domain url to the client as part of the response, since it already knows it (otherwise it would not be able to make the request in the first place).

That said, one way of solving your problem is to pass the custom domain url as a Lambda environment variable. If you have setup your application using AWS SAM / CloudFormation you pass the value as part of the Environment property, e.g. when using SAM:

ExampleLambda:
  Type: AWS::Serverless::Function
  Properties:
    Runtime: nodejs8.10
    CodeUri: .
    Handler: index.handler
    Environment:
      Variables:
        CUSTOM_DOMAIN_URL: !Sub CustomDomainUrl

Then CUSTOM_DOMAIN_URL will be available as an environmental variable in your Lambda runtime:

const CUSTOM_DOMAIN_URL = process.env.CUSTOM_DOMAIN_URL;

And then you you just add it to your response object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this was my last option. The requirement is like in the response I have to send the links to other resources appended to the custom domain url. So it would not be just plain URL. It may be something like "myurl": "abc.def.com/ghi?parameter=hello" or "myurl": "abc.def.com/ghi/someresource"
You can still use client side logic. For example, you can save the host part of the URL in client before the request and return the path part of the URL in the response from the server. Then it is the client's job to stitch the two together before making the next request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.