我正在嘗試使用 Lambda 函式將事件從 SQS 佇列獲取到 S3 存盤桶中。嘗試使用 cloudFormation 模板進行部署時出現以下錯誤。我的 Lambda 執行角色缺少什么?
錯誤:
- ERROR - Stack shows a rollback status ROLLBACK_IN_PROGRESS.
- INFO - The following root cause failure event was found in the stack for resource 'EventLambda':
- INFO - Resource handler returned message: "Error occurred while GetObject. S3 Error Code: NoSuchKey.
S3 Error Message: The specified key does not exist. (Service: Lambda, Status Code: 400, Request ID: 6b19aec2-6b0e-437a-8f19-7d699f3b3c52,

我在我的 cloudFormation 模板中使用以下 Lambda 函式和 Lambda 執行角色。
"EventLambda": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "S3Bucket"
},
"S3Key": "S3Bucket"
},
"Description": "Copy events from SQS Queue into s3 bucket",
"Environment": {
"Variables": {
"FinalBucket": {
"Ref": "EventDeployS3Bucket"
}
}
},
"Handler": "sqs_to_s3_lambda.lambda_handler",
"Layers": [
{
"Ref": "LambdaLayerVersion"
}
],
"MemorySize": 128,
"Role": {
"Fn::GetAtt": [
"LambdaExecutionRole",
"Arn"
]
},
"Runtime": "python3.7",
"Timeout": 300
}
},
"LambdaExecutionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Policies": [
{
"PolicyName": "LambdaPolicy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes",
"sqs:ChangeMessageVisibility"
],
"Resource": "*"
}
]
}
}
]
}
}
而 Lambda 函式是:
import json
import logging
import os
import boto3
logger = logging.getLogger()
logger.info("init")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
def copy_files_to_s3(event, context):
# Setup the client
s3_bucket = boto3.resource("s3")
logger.info(f"lambda_handler -- event: {json.dumps(event)}")
events = json.loads(event["Records"][0]["body"])
print(events)
s3_bucket.put_object(Bucket=os.environ['S3BucketEvents'], key="data.json", Body=json.dumps(events))
logger.info("done")
def lambda_handler(event, context):
logger.info("copied events data")
copy_files_to_s3(event, context)
logger.info("done")
uj5u.com熱心網友回復:
"S3Key": "S3Bucket"是不正確的。S3Bucket應該是 S3 中 lambda zip 檔案的名稱。因此,在將帶有源代碼的 zip 上傳到 S3 之后,您必須為此提供有效名稱。例如:
`"S3Key": "myfunction.zip"`
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/421262.html
標籤:
