[英]How do I call an Lambda authorized API Gateway endpoint from AWS Lambda
[英]How do I find the API endpoint of a lambda function?
我有一個 Lambda function 有一個暴露的 API 網關端點,我可以通過 AWS 控制台獲得 URL。 但是,我想通過 API 電話獲得 URL。 Lambda API 文檔和API 網關文檔似乎都沒有該信息(或者我可能錯過了),那么這是否可能首先出現?
我不太明白上面的答案(也許它已經過時了?)。
最簡單的方法:
您的 API 網關端點 URL 不會通過 API 調用公開。 但是,由於 API 的 URL 遵循某種結構,因此您可以獲得所有必要的部分並在您的代碼中創建 URI。
https://API-ID.execute-api.REGION.amazonaws.com/STAGE
你可以使用apigateway:rest-apis來獲取你的 API-ID 和restapi:stages來獲取階段對應的標識符。
我沒有看到 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
跟進@larschanders 評論,如果您使用 CloudFormation 創建網關,端點 URL 將作為堆棧輸出之一出現。
如果您使用 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 服務的工作示例。 這是上下文中相關代碼的鏈接: aws-doc-sdk-examples 。
轉到您的 lambda 主頁 -> 單擊“應用程序” [功能概述部分] -> 資源部分 -> LambdaAPIDefinition -> 階段選項卡 -> [選擇所需階段] -> [調用 URL] 您可以看到 api 端點可見。
如果您知道 function 名稱,則可以通過獲取策略來獲取 apigw 端點:
aws lambda get-policy --function-name <name> \
| jq -r '.Policy | fromjson | .Statement[0].Condition.ArnLike."AWS:SourceArn"'
那么這只是提取 api_id 並像Jurgen建議的那樣構建端點的問題
腳本是目前唯一的答案但是如果有人想純粹在 CLI 上使用命令來做這里是很長的路要走(雖然我是 AWS 的一個非常初學者並且我在學習 Lambda 基礎知識期間嘗試過這個),也許有更好的方法...
端點的 Invoke-URL 的格式是這樣的
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
請注意,您必須安裝 jq 解析器(如果您還沒有) https://stedolan.github.io/jq/download/我在 Windows 之上使用 Linux,所以我需要以這種方式進行
curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
非常精確地轉到 AWS 控制台搜索:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.