【发布时间】:2021-05-01 00:26:47
【问题描述】:
我一直在互联网上寻找解决方案。根据this tutorial,我一直在尝试设置一个 AWS Lambda 函数,以便在每次将文件上传到特定的 S3 存储桶时向 SNS 发送一条消息。至此,我已经完成了函数设置,并且可以成功调用它。但是,当我尝试将该函数连接到 S3 时,我收到一条错误消息,指出 An error occurred (InvalidArgument) when calling the PutBucketNotification operation: Unable to validate the following destination configurations。根据this article,我应该能够添加一个允许 S3 调用 Lambda 函数的权限,如下所示:
aws lambda add-permission \
--function-name my-file-upload \
--principal s3.amazonaws.com \
--statement-id AcceptFromImport \
--action "lambda:InvokeFunction" \
--source-arn arn:aws:s3:::file-import \
--source-account my_account_id
我这样做了,并注意到与 Lambda 函数关联的策略已更新并且似乎是正确的。但是,错误仍然存在。我看过一个类似的问题,here,但这里的解决方案都不起作用。
执行角色 ARN:arn:aws:iam::my_account_id:role/lambda-upload-stream
执行角色(lambda-upload-stream)信任关系:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
执行角色策略(my-file-upload):
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AccessObject",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::file-import/*"
},
{
"Sid": "SendUpdate",
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "arn:aws:sns:ap-northeast-1:my_account_id:comm-in"
}
]
}
Lambda 函数 ARN:arn:aws:lambda:ap-northeast-1:my_account_id:function:my-file-upload
Lambda 函数角色文档
{
"roleName": "lambda-upload-stream",
"policies": [
{
"name": "my-file-upload",
"id": "AWS_ACCESS_KEY_ID",
"type": "managed",
"arn": "arn:aws:iam::my_account_id:policy/my-file-upload",
"document": {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AccessObject",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::file-import/*"
},
{
"Sid": "SendUpdate",
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "arn:aws:sns:ap-northeast-1:my_account_id:comm-in"
}
]
}
}
],
"resources": {
"s3": {
"service": {
"name": "Amazon S3",
"icon": "data:image/svg+xml;base64,very_long_base64_string1"
},
"statements": [
{
"resource": "arn:aws:s3:::file-import/*",
"service": "s3",
"effect": "Allow",
"action": "s3:GetObject",
"source": {
"index": "AccessObject",
"policyName": "my-file-upload",
"policyType": "managed"
}
}
]
},
"sns": {
"service": {
"name": "Amazon SNS",
"icon": "data:image/svg+xml;base64,very_long_base64_string2"
},
"statements": [
{
"resource": "arn:aws:sns:ap-northeast-1:my_account_id:comm-in",
"service": "sns",
"effect": "Allow",
"action": "sns:Publish",
"source": {
"index": "SendUpdate",
"policyName": "my-file-upload",
"policyType": "managed"
}
}
]
}
},
"trustedEntities": [
"lambda.amazonaws.com"
]
}
Lambda 函数资源策略:
{
"Version": "2012-10-17",
"Id": "default",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:ap-northeast-1:my_account_id:function:my-file-upload",
"Condition": {
"StringEquals": {
"AWS:SourceAccount": "my_account_id"
},
"ArnLike": {
"AWS:SourceArn": "arn:aws:s3:::file-import"
}
}
}
]
}
我的问题是:我在这里做错了什么以及如何解决?
【问题讨论】:
-
为什么会有三个角色?这些用于3种不同的功能?什么是“角色文件”?它来自哪里?其 IAM 角色或策略无效。
-
@Marcin 角色文档是我在 AWS 控制台上查找 Lambda 函数时得到的,转到权限并单击
Role Document
标签: amazon-web-services amazon-s3 aws-lambda