How do I find the API endpoint of a lambda function?

后端 未结 6 1573
有刺的猬
有刺的猬 2021-02-06 22:37

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. Neithe

相关标签:
6条回答
  • 2021-02-06 22:43

    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"
    0 讨论(0)
  • 2021-02-06 22:45

    To get the API endpoint ,

    first step is to deploy the API on a stage(dev/test/prod) then you will get the invoke url

    0 讨论(0)
  • 2021-02-06 22:46

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

    0 讨论(0)
  • 2021-02-06 22:51

    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.

    0 讨论(0)
  • 2021-02-06 22:55

    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.

    0 讨论(0)
  • 2021-02-06 22:57

    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
    
    0 讨论(0)
提交回复
热议问题