sno*_*der 2 python-requests boto3 aws-api-gateway
我正在尝试从具有 IAM 角色的 EC2 实例之一调用 AWS API Gateway 端点。我在 EC2 实例上安装了 boto3 库,并尝试使用下面的代码执行简单的网关 API,但仍然出现身份验证丢失错误。
import boto3
import json
import requests
from aws_requests_auth.aws_auth import AWSRequestsAuth
session = boto3.Session()
credentials = session.get_credentials()
headers = {'params': 'ABC'}
response = requests.get('https://restapiid.execute-api.us-east-1.amazonaws.com/stage/resource_path',
auth=credentials, headers=headers)
Run Code Online (Sandbox Code Playgroud)
对于具有 IAM 角色的 EC2 实例,这应该非常简单。请任何建议。
由于您的问题缺乏详细信息(缺少实例角色详细信息、API 网关策略、未知headers或iam_auth已启用天气),我只能提供并评论给定的 python 代码。
使用角色的python代码应该是(这是我用来验证代码的示例):
import boto3
import requests
from aws_requests_auth.aws_auth import AWSRequestsAuth
session = boto3.Session()
credentials = session.get_credentials()
auth = AWSRequestsAuth(aws_access_key=credentials.access_key,
aws_secret_access_key=credentials.secret_key,
aws_token=credentials.token,
aws_host='fzoskzctgd.execute-api.us-east-1.amazonaws.com',
aws_region='us-east-1',
aws_service='execute-api')
response = requests.get('https://fzoskzctgd.execute-api.us-east-1.amazonaws.com/test', auth=auth)
print(response.content)
Run Code Online (Sandbox Code Playgroud)
我用authorizationTypeset to测试了AWS_IAM所测试的资源。
API资源政策
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456:role/instance-role"
},
"Action": "execute-api:Invoke",
"Resource": "arn:aws:execute-api:us-east-1:170576413884:fzoskzctgd/test/*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
实例角色
不需要任何 API 调用权限,因为它们是通过 API 资源策略提供的。实例角色必须仅存在并附加到实例。
| 归档时间: |
|
| 查看次数: |
13886 次 |
| 最近记录: |