要向API Gateway阶段变量添加权限,您可以使用AWS CLI的add-permission命令。下面是一个示例解决方法:
aws --version
aws lambda create-function \
--function-name my-function \
--runtime python3.8 \
--handler lambda_function.lambda_handler \
--role arn:aws:iam::123456789012:role/lambda-execution-role \
--code S3Bucket=my-bucket,S3Key=my-function.zip
请注意,您需要将--role
参数替换为您的Lambda执行角色的ARN,并将--code
参数替换为您的Lambda函数的代码位置。
aws apigateway create-rest-api --name my-api
aws apigateway get-resources --rest-api-id | jq '.items[] | select(.path == "/my-resource")' | jq '.id'
aws apigateway put-method --rest-api-id --resource-id --http-method ANY --authorization-type NONE
请注意,将
替换为您的API Gateway的ID,将
替换为资源的ID。
aws lambda add-permission \
--function-name my-function \
--statement-id apigateway-test-2 \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:::/*/*/*"
请注意,将
替换为您的AWS区域,将
替换为您的AWS账户ID,将
替换为您的API Gateway的ID。
aws apigateway put-integration \
--rest-api-id \
--resource-id \
--http-method ANY \
--type AWS_PROXY \
--integration-http-method POST \
--uri arn:aws:apigateway::lambda:path/2015-03-31/functions/arn:aws:lambda:::function:my-function/invocations
请注意,将
替换为您的AWS区域,将
替换为您的AWS账户ID。
现在,您已经成功地向API Gateway阶段变量添加了权限。您可以使用AWS CLI的其他命令来部署和管理您的API Gateway。