gpt4 book ai didi

amazon-web-services - 用于在 S3 事件上触发 Lambda 的 Cloudformation 模板

转载 作者:行者123 更新时间:2023-12-04 08:17:39 25 4
gpt4 key购买 nike

我想使用 Cloudformation 创建一个 S3 存储桶,每当发生 S3 事件(例如文件创建、文件删除等)时,该存储桶都会触发 Lambda 函数。

根据我的研究,我有我的 AWS::Lambda::FunctionAWS::S3::Bucket设置,

AWSTemplateFormatVersion: '2010-09-09'
Resources:
HandleFileCreation:
Type: "AWS::Lambda::Function"
Properties:
...

LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/AWSLambdaFullAccess
AssumeRolePolicyDocument:
...

ReportsBucket:
Type: AWS::S3::Bucket

BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ReportsBucket
PolicyDocument:
...

我正在查看AWS::Events::Rule ,但该示例仅适用于 EC2,我找不到 S3 的示例

  EventRule: 
Type: "AWS::Events::Rule"
Properties:
Description: "EventRule"
EventPattern:
source:
- "aws.ec2"
detail-type:
- "EC2 Instance State-change Notification"
detail:
state:
- "stopping"
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- HandleFileCreation
- Arn
Id: TargetFunctionV1
PermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: HandleFileCreation
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "EventRule"
- "Arn"

如何编写模板来触发 S3 事件?

最佳答案

这是一个示例,

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-notificationconfig-lambdaconfig.html

EncryptionServiceBucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub ${User}-encryption-service
NotificationConfiguration:
LambdaConfigurations:
-
Function: !Ref LambdaDeploymentArn
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: suffix
Value: zip

我注意到的一个问题是,您需要先创建该函数,然后再为其分配触发器。如果您使用 CF,请确保在为其创建触发器之前创建 lambda 函数。

希望有帮助。

关于amazon-web-services - 用于在 S3 事件上触发 Lambda 的 Cloudformation 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46854316/

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