我需要我的 lambda 来调用 API 网关,并在我的云形成模板中将以下代码作为 lambda 的内联代码。
from requests_aws4auth import AWS4Auth
def handler(event,context):
client = boto3.client('sts')
responseAssumeRole = client.assume_role(
DurationSeconds=3600,
RoleArn='arn',// real arn of the api gateway invocation role
RoleSessionName='Bob',
)
credentials = responseAssumeRole['Credentials']
auth = AWS4Auth(aws_access_key=responseAssumeRole['Credentials']['AccessKeyId'],
aws_secret_access_key=responseAssumeRole['Credentials']['SecretAccessKey'],
aws_host='host.execute-api.us-east-1.amazonaws.com',
aws_region='us-east-1',
aws_service='execute-api')
headers= {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
response = requests.get('https://host.execute-api.us-east-1.amazonaws.com/test',
auth=auth, headers=headers)
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误
No module named 'requests_aws4auth'
Run Code Online (Sandbox Code Playgroud)
任何使用 aws 凭据创建身份验证的解决方案或替代方法也将受到欢迎。
我正在研究 KMS 密钥的云形成模板。在政策文件中,我想根据阶段(无论是生产还是测试)设置委托人。Fn:If如果两个阶段只有一个主体,我可以轻松使用。但是我每个阶段都有不止一个principal和Fn:If只允许你赋值,根据我的理解不是集合(如果我错了,请纠正我)。
我曾尝试为该值分配一个集合,但在使用 AWS 帐户中的 CloudFormation 设计器验证模板时,它给了我“映射键必须是字符串;收到一个集合”错误。
"MyEncryptionKey": {
"DeletionPolicy": "Retain",
"Properties": {
"Description": "MyEncryptionKey",
"EnableKeyRotation": true,
"Enabled": true,
"KeyPolicy": {
"Statement": [
{
"Action": "kms:*",
"Effect": "Allow",
"Principal": {
"AWS": "root"
},
"Resource": "*"
},
{
"Action": "kms:Decrypt",
"Effect": "Allow",
"Principal": {
"AWS": [
{
"Fn::If": [
"IsProd",
{["arn1","arn2"]},
"arn2"
]
}
]
},
"Resource": "*"
}
]
}
},
"Version": "2012-10-17",
"Type": "AWS::KMS::Key"
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,密钥策略中的第二个语句应该有两个 arn 值 if prod 和一个 arn value if not prod。 …