我想使用Cloudformation创建一个S3存储桶,只要发生S3事件(例如文件创建,文件删除等)就会触发Lambda函数。
根据我的研究,我有AWS::Lambda::Function和AWS::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事件?
答案 0 :(得分:3)
这是一个涵盖的例子,
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函数。
希望它有所帮助。
答案 1 :(得分:0)
我在使用Amazon Toolkit的Visual Studio示例项目之一中找到了答案:
(function f() { return 10; })()
答案 2 :(得分:0)
我已经创建了以下代码,它正在与CloudFormation一起使用 ################################################ ######################### 每当发生S3事件时触发Lambda函数
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub '${EnvironmentNameShorthand}.product'
NotificationConfiguration:
LambdaConfigurations:
-
Function: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:function name'
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
-
Name: suffix
Value: .json
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:function name '
Action: "lambda:InvokeFunction"
Principal: "s3.amazonaws.com"
SourceArn: !Sub 'arn:aws:s3:::${EnvironmentNameShorthand}.product'
答案 3 :(得分:-1)
这是一个详细的答案(来自https://medium.com/@windix/s3-bucket-notification-to-lambda-in-cloudformation-without-circular-reference-f8f56ec5342c)
AWSTemplateFormatVersion: '2010-09-09'
Description: Example Stack
Parameters:
BucketName:
Type: String
Default: unique-bucket-name
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
...
NotificationConfiguration:
LambdaConfigurations:
- Event: 's3:ObjectCreated:*'
Filter:
S3Key:
Rules:
- Name: prefix
Value: test/
- Name: suffix
Value: .txt
Function: !GetAtt Lambda.Arn
Lambda:
Type: AWS::Lambda::Function
...
S3InvokeLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref Lambda
Principal: s3.amazonaws.com
SourceArn: !Sub arn:aws:s3:::${BucketName}
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: '/'
Policies:
- PolicyName: s3
PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:Get*
Resource:
- !Sub arn:aws:s3:::${BucketName}
- !Sub arn:aws:s3:::${BucketName}/*