54

I have a Lambda function that has an exposed API Gateway endpoint, and I can get the URL for that via the AWS console. However, I would like to get that URL via API call. Neither the Lambda API documentation nor the API Gateway documentation seem to have that information (or perhaps I've missed it), so is this even possible in the first place?

howcheng
  • 2,211
  • 2
  • 17
  • 24

9 Answers9

99

I don't really understand the above answer (maybe it's outdated?).

The absolute easiest way:

  1. Choose "API Gateway" under "Services" in AWS.
  2. Click on your API.
  3. Click on "Stages".
  4. Choose the stage you want to use
  5. Now you can see the entire URL very visible inside a blue box on the top with the heading "Invoke URL"
larschanders
  • 1,951
  • 3
  • 16
  • 21
39

Your API Gateway endpoint URL doesn't get exposed via an API call. However, since the URL of the API follows a certain structure, you could get all the necessary pieces and create the URI within your code.

https://API-ID.execute-api.REGION.amazonaws.com/STAGE

You could use apigateway:rest-apis to get your API-ID and restapi:stages to get the stage corresponding identifier.

Jurgen
  • 1,243
  • 8
  • 9
  • When I get back to that task, I will definitely try that out. Thanks. – howcheng Apr 15 '16 at 00:13
  • What do you do if you have a CDN for your gateway - is that exposed to the lambda? – Gordon Bean Dec 12 '19 at 22:44
  • Not to the lambda, but you can find it from the API gateway custom domains. See my example below. – Peter Halverson Apr 16 '20 at 15:52
  • You can actually retrieve everything from the request context parameter passed into the Lambda (ie. `context`), no need to issue any separate requests to the API Gateway. The API domain is stored in `context['requestContext']['domainName']` and the API stage in `context['requestContext']['stage']`. – schiavuzzi Apr 03 '23 at 12:30
4

I'm not seeing a direct answer to the OP's question (get endpoint URL using API). Here's a snippet of Python code that I hope will guide the way, even if you're using other language bindings or the CLI. Note the difference in approach for getting the internal endpoint vs getting any associated custom domain endpoints.

import boto3

apigw = boto3.client('apigateway')

def get_rest_api_internal_endpoint(api_id, stage_name, region=None):
    if region is None:
        region = apigw.meta.region_name
    return f"https://{api_id}.execute-api.{region}.amazonaws.com/{stage_name}"

def get_rest_api_public_endpoints(api_id, stage_name):
    endpoints = []
    for item in apigw.get_domain_names().get('items',[]):
        domain_name = item['domainName']
        for mapping in apigw.get_base_path_mappings(domainName=domain_name).get('items', []):
            if mapping['restApiId'] == api_id and mapping['stage'] == stage_name:
                path = mapping['basePath']
                endpoint = f"https://{domain_name}"
                if path != "(none)":
                    endpoint += path
                endpoints.append(endpoint)
    return endpoints
Peter Halverson
  • 380
  • 2
  • 12
2

Go to you lambda main page -> Click on "Application" [Function Overview section] -> Resource section -> LambdaAPIDefinition -> Stages tab -> [Select required stage] -> [Invoke URL] You can see the api endpoint visible.

1

If you use CloudFormation you can get this with Python and Boto3:

import boto3

cloudformation = boto3.resource('cloudformation')
stack = cloudformation.Stack(name=stack_name)
api_url = next(
    output['OutputValue'] for output in stack.outputs
    if output['OutputKey'] == 'EndpointURL')

This is from a working example of a REST service using Chalice that I put on GitHub. Here's a link to the pertinent code, in context: aws-doc-sdk-examples.

Laren Crawford
  • 561
  • 4
  • 6
0

Following up on @larschanders comment, if you create the gateway using CloudFormation, the endpoint URL is surfaced as one of the stack outputs.

Peter Halverson
  • 380
  • 2
  • 12
  • it would be helpful if you described where in the json this info is located. i quick grep of 'http' didn't return any results. – David Schumann Apr 15 '20 at 11:14
  • Sorry, my mistake – I haven't written a CF-based API deployment in a while, been using other tools for managing AWS resources. This obviously only works if the CF template author has included the stage endpoint URL as an output. – Peter Halverson Apr 16 '20 at 14:46
  • The better answer is, as mentioned earlier you don't need an API call, the internal stage endpoint URL is always 'https://.execute-api..amazonaws.com/'; see https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html If the API has been mapped to a custom domain, that's a bit more complicated: you have to look through all the custom domains (apigateway:get-domain-names) and their corresponding base path mappings (apigateway:get-base-path-mappings) to find the mapping(s) associated with the API. – Peter Halverson Apr 16 '20 at 15:14
0

If you know the function name, you can get the apigw endpoint by fetching the policy:

aws lambda get-policy --function-name <name> \
  | jq -r '.Policy | fromjson | .Statement[0].Condition.ArnLike."AWS:SourceArn"'

THen it's just a matter of extracting the api_id and construct the endpoint like Jurgen suggested

Karl Pokus
  • 772
  • 1
  • 7
  • 14
0

A Script is the only answer as of now But if someone wants to do purely on CLI using commands here is the long way (though I am a very beginner in AWS and I tried this during learning Lambda basics), maybe there is a much better way...

The format of Invoke-URL of the endpoint is something like this

https://YOUR-REST-API-ID.execute-api.REGION.amazonaws.com/STAGE/RESOURCE

// get YOUR-REST-API-ID            e.g.,    0w12zl28di
aws apigateway get-rest-apis | jq -r '.items[] | [.id, .name] | @tsv' 


// get REGION  using Lambda_function_name e.g.,      ap-east-1
aws lambda get-function-configuration --function-name YOUR_LAMBDA_FUNCTION_NAME | jq '.FunctionArn | split(":")[3]'   

// get STAGE e.g.,         dev
aws apigateway get-stages --rest-api-id YOUR-REST-API-ID | jq -r '.item[] | [.stageName] | @tsv'


// get RESOURCE e.g.,       users
aws apigateway get-resources --rest-api-id YOUR-REST-API-ID | jq '.items[] | [.id, .path]'


 // finally put your pieces in this way

 https://0w12zl28di.execute-api.ap-east-1.amazonaws.com/dev/users

Note you have to install jq parser (if you don't have it already) https://stedolan.github.io/jq/download/ I was using Linux on top of Windows, so I need to do it in this way

curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
ajay_full_stack
  • 494
  • 7
  • 13
-2

To very precise Go to AWS Console search :

  1. Lambda under services
  2. Search the Function name which are looking for.
  3. In Function overview, you will find API GATEWAY (Click on this)
  4. Under API GATEWAY, click on Details (down arrow)
  5. Under Details, you will find all the details like API endpoint : API type : Authorization : Method : Resource path : Stage :
Ak_Singh
  • 1
  • 5