如何找到lambda函数的API端点?

时间:2016-04-11 22:36:05

标签: aws-lambda aws-api-gateway

我有一个Lambda函数,它有一个公开的API网关端点,我可以通过AWS控制台获取该URL。但是,我想通过API调用获取该URL。 Lambda API documentationAPI Gateway documentation似乎都没有这些信息(或者我可能错过了它),所以这首先是可能的吗?

7 个答案:

答案 0 :(得分:24)

我真的不明白上述答案(也许它已经过时了?)。

绝对最简单的方法:

  1. 选择" API网关"在"服务"在AWS。
  2. 点击您的API。
  3. 点击"阶段"。
  4. 选择您要使用的舞台
  5. 现在,您可以在顶部的蓝色框内看到整个网址,标题为“#34;调用网址"

答案 1 :(得分:7)

您的API网关端点URL不会通过API调用公开。但是,由于API的URL遵循某种结构,您可以获得所有必需的部分并在代码中创建URI。

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

您可以使用apigateway:rest-apis获取API-ID,restapi:stages获取舞台相应的标识符。

答案 2 :(得分:1)

我没有看到OP问题的直接答案(使用API​​获取端点URL)。这是一小段Python代码,即使您使用的是其他语言绑定或CLI,我也希望能以此为指导。请注意,获取内部端点与获取任何关联的自定义域端点的方法有所不同。

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

答案 3 :(得分:0)

跟进@larschanders注释,如果使用CloudFormation创建网关,则端点URL将作为堆栈输出之一浮出水面。

答案 4 :(得分:0)

要获取API端点,

  

第一步是将API部署在阶段(dev / test / prod),然后您   将获取调用网址

enter image description here

enter image description here

答案 5 :(得分:0)

如果您使用CloudFormation,则可以通过Python和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')

这来自我在GitHub上使用Chalice的REST服务的工作示例。以下是上下文中相关代码的链接:Container image

答案 6 :(得分:-1)

非常精确地转到 AWS 控制台搜索:

  1. 服务下的 Lambda
  2. 搜索正在查找的函数名称。
  3. 在函数概览中,您会找到 API GATEWAY(点击此处)
  4. 在 API GATEWAY 下,点击详细信息(向下箭头)
  5. 在详细信息下,您将找到所有详细信息,例如 API 端点: API 类型: 授权 : 方法 : 资源路径: 阶段:
相关问题