gpt4 book ai didi

amazon-web-services - 在我的 IaC 堆栈中收到自动存储桶策略的处理程序错误

转载 作者:行者123 更新时间:2023-12-03 07:24:11 25 4
gpt4 key购买 nike

AWSTemplateFormatVersion: '2010-09-09'
Resources:
LambdaFunction:
Type: 'AWS::Lambda::Function'
Properties:
Code:
ZipFile: |
import json
import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):
# Get bucket name from the S3 event
print(event)

bucket_name = event['detail']['requestParameters']['bucketName']

# Create a bucket policy
bucket_policy =json.dumps({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "MustBeEncryptedAtRest",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"Resource": [
"arn:aws:s3:::{}".format(bucket_name),
"arn:aws:s3:::{}/*".format(bucket_name)
],
"Condition": {
"StringNotEquals": {
"s3:x-amz-server-side-encryption": [
AES256
"aws:kms"
]
}
}
},
{
"Sid": "MustBeEncryptedInTransit",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::{}".format(bucket_name),
"arn:aws:s3:::{}/*".format(bucket_name)
],
"Condition": {
"Bool": {
"aws:SecureTransport": "false"
}
}
} ] })


# Set the new policy
s3.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy),
Handler: lambda_handler.py
Role: 'arn:aws:iam::myrole'
Runtime: python3.7
EventRule:
Type: 'AWS::Events::Rule'
Properties:
EventPattern:
source:
- aws.s3
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- s3.amazonaws.com
eventName:
- CreateBucket

我检查了 lambda 控制台,看看我的函数是否写在其中,并收到如下错误消息:“Lambda 找不到文件 lambda_handler.py。请确保您的处理程序支持以下格式:文件名.方法。”我的处理程序为 lambda_handler.py,那么为什么现在它不起作用?

最佳答案

创建的文件将命名为index.py

因此,您的 Handler 属性将为 index.lambda_handler。这是因为处理程序定义了与 Lambda 服务调用的函数相结合的文件名。

如上所述,文件是 index.py 但由于 Lambda 知道您的运行时,它只需要文件名的 index 部分,那么代码中定义的函数是lambda_handler

更新处理程序值,这应该可以工作。

关于amazon-web-services - 在我的 IaC 堆栈中收到自动存储桶策略的处理程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63060540/

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