简体   繁体   English

如何从 AWS API Gateway 获取端点/URI?

[英]How to get endpoint/URI from AWS API Gateway?

How can I get the endpoint or URI from AWS API Gateway ?如何从 AWS API Gateway 获取端点或 URI? I see only arn from the management console我只看到管理控制台的 arn

You need to deploy an API in order to get an endpoint URL.您需要部署 API 才能获取端点 URL。 The same API may be deployed under different guises — you might call it "dev" for a development deployment or "prod" for production purposes.相同的 API 可能以不同的形式部署——您可以将其称为“开发”以用于开发部署或“生产”以用于生产目的。

The API can only be accessed in this way once deployed, so:该 API 只能在部署后以这种方式访问​​,因此:

  • Go to "APIs > resources"转到“API > 资源”
  • Use the Actions button, "Actions > Deploy API"使用操作按钮,“操作 > 部署 API”
  • Deploy it as, eg "dev"将其部署为例如“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 "然后,在“APIs > Stages”下,选择部署,您将在顶部的横幅中看到 URL,“调用 URL: https://...amazonaws.com/dev

If you know the name of your rest-api endpoint (and it's been deployed as @pogul described), you can construct the URL.如果您知道您的 rest-api 端点的名称(并且它已按照 @pogul 的描述进行部署),您就可以构建 URL。 Here's a short python commandline app using boto3 that will return the URL given a name.这是一个使用 boto3 的简短 python 命令行应用程序,它将返回给定名称的 URL。

#!/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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM