Hello lad
Hello lad

Reputation: 18790

How to get endpoint/URI from AWS API Gateway?

How can I get the endpoint or URI from AWS API Gateway ? I see only arn from the management console

Upvotes: 4

Views: 4005

Answers (2)

bwv549
bwv549

Reputation: 5859

If you know the name of your rest-api endpoint (and it's been deployed as @pogul described), you can construct the URL. Here's a short python commandline app using boto3 that will return the URL given a name.

#!/usr/bin/env python

import argparse

import boto3

# for example:
# https://abcd123456.execute-api.us-east-2.amazonaws.com/mydeploystage

SUBDOMAIN = 'execute-api'
SECOND_LEVEL_DOMAIN = "amazonaws"
EXT = "com"

session = boto3.session.Session()
default_region = session.region_name

DEFAULT_PROTOCOL = 'https'

parser = argparse.ArgumentParser(description="guess the urls given a rest endpoint")
parser.add_argument("name", help="name of the rest-api endpoint")
parser.add_argument("--region", default=default_region, help=f"region (default: {default_region})")
parser.add_argument("--protocol", default=DEFAULT_PROTOCOL, help=f"protocol (default: {DEFAULT_PROTOCOL})")
args = parser.parse_args()

client = boto3.client('apigateway')

response = client.get_rest_apis()

name_to_result = {result.get('name'): result for result in response.get('items')}

api_endpoint = name_to_result[args.name]
api_id = api_endpoint['id']

response = client.get_stages(restApiId=api_id)
for stage in response['item']:
    stage_name = stage['stageName']
    domain_name = ".".join([api_id, SUBDOMAIN, args.region, SECOND_LEVEL_DOMAIN, EXT])
    url = f"{args.protocol}://{domain_name}/{stage_name}"
    print(url)

Upvotes: 0

pogul
pogul

Reputation: 458

You need to deploy an API in order to get an endpoint URL. The same API may be deployed under different guises — you might call it "dev" for a development deployment or "prod" for production purposes.

The API can only be accessed in this way once deployed, so:

  • Go to "APIs > resources"
  • Use the Actions button, "Actions > Deploy API"
  • Deploy it as, e.g. "dev"
  • Then, under "APIs > Stages", select the deployment and you will see the URL in a banner at the top, "Invoke URL: https://...amazonaws.com/dev"

Upvotes: 4

Related Questions