gpt4 book ai didi

amazon-web-services - 尝试使用 cloudFormation 模板创建堆栈时获取 'Error occurred while GetObject. S3 Error Code: NoSuchKey'

转载 作者:行者123 更新时间:2023-12-03 07:22:50 28 4
gpt4 key购买 nike

我正在尝试使用 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,

enter image description here

我在我的 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")

最佳答案

“S3Key”:“S3Bucket”不正确。 S3Bucket 应该是 S3 中 lambda zip 文件的名称。因此,在将带有源代码的 zip 上传到 S3 后,您必须为此提供有效的名称。例如:

`"S3Key": "myfunction.zip"`

关于amazon-web-services - 尝试使用 cloudFormation 模板创建堆栈时获取 'Error occurred while GetObject. S3 Error Code: NoSuchKey',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70826908/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com