我如何从 AWS Lambda 调用一个 AWS_IAM 授权的 API 网关端点,谁的执行角色有权这样做?
How do I call an AWS_IAM authorized API Gateway endpoint from AWS Lambda who's execution role has permission to do so?
我想从执行角色有权执行该 API 网关的正在执行的 AWS Lambda 调用授权设置为 AWS_IAM 的 AWS API 网关端点。
我是否可以使用我的 Lambda 的当前执行角色将 HTTPS 请求签署到 API 网关?
编辑:
查看http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html,我可能可以使用SDK调用AssumeRole来承担角色,该角色具有执行Api网关的权限,然后提取可用于的密钥签署请求。
可以从环境变量中检索 lambda 执行角色的凭据 - AWS_SESSION_TOKEN、AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY.
您可以使用上述凭据对请求进行签名。签名文档在此处 http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html。您还可以查看执行此操作的第三方库。
请注意,由于您拥有会话令牌,因此您需要按照此处所述进行处理 http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html#RequestWithSTS
如, you can retrieve the credentials for lambda's execution with the environment variables.
如果您正在使用 Python,您可以使用 aws_requests_auth library to sign the request. You can also check the documentation for a complete example on how to sign the request。
以下是有关如何签署来自 lambda 的请求的片段:
import json
import requests
import os
from aws_requests_auth.aws_auth import AWSRequestsAuth
def lambda_handler(event, context):
api_id = 'XXXXXXXXX' # Replace with your API Gateway Id
api_url = 'https://{}.execute-api.eu-west-1.amazonaws.com/dev/hello'.format(api_id)
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID']
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
aws_session_token = os.environ['AWS_SESSION_TOKEN']
auth = AWSRequestsAuth(aws_access_key=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_token=aws_session_token,
aws_host='{}.execute-api.eu-west-1.amazonaws.com'.format(api_id),
aws_region='eu-west-1',
aws_service='execute-api')
response = requests.get(api_url, auth=auth)
return {
'statusCode': response.status_code,
'body': json.dumps(response.json())
}
希望对您有所帮助。
我想从执行角色有权执行该 API 网关的正在执行的 AWS Lambda 调用授权设置为 AWS_IAM 的 AWS API 网关端点。
我是否可以使用我的 Lambda 的当前执行角色将 HTTPS 请求签署到 API 网关?
编辑:
查看http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html,我可能可以使用SDK调用AssumeRole来承担角色,该角色具有执行Api网关的权限,然后提取可用于的密钥签署请求。
可以从环境变量中检索 lambda 执行角色的凭据 - AWS_SESSION_TOKEN、AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY.
您可以使用上述凭据对请求进行签名。签名文档在此处 http://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html。您还可以查看执行此操作的第三方库。
请注意,由于您拥有会话令牌,因此您需要按照此处所述进行处理 http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html#RequestWithSTS
如
如果您正在使用 Python,您可以使用 aws_requests_auth library to sign the request. You can also check the documentation for a complete example on how to sign the request。
以下是有关如何签署来自 lambda 的请求的片段:
import json
import requests
import os
from aws_requests_auth.aws_auth import AWSRequestsAuth
def lambda_handler(event, context):
api_id = 'XXXXXXXXX' # Replace with your API Gateway Id
api_url = 'https://{}.execute-api.eu-west-1.amazonaws.com/dev/hello'.format(api_id)
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID']
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
aws_session_token = os.environ['AWS_SESSION_TOKEN']
auth = AWSRequestsAuth(aws_access_key=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_token=aws_session_token,
aws_host='{}.execute-api.eu-west-1.amazonaws.com'.format(api_id),
aws_region='eu-west-1',
aws_service='execute-api')
response = requests.get(api_url, auth=auth)
return {
'statusCode': response.status_code,
'body': json.dumps(response.json())
}
希望对您有所帮助。