0

我想用Cloudformation每當S3事件如文件創建,文件刪除等Cloudformation模板觸發LAMBDA上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事件?

回答

1

這裏有個例子,

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函數。

希望它有幫助。

+0

我收到一個錯誤:AWS :: S3 :: Bucket \t ReportsBucket \t ARN的格式不正確物理ID:some-prefix-us-west-2-test-43a8dcf',它引用了我的桶名稱BucketName: !加入[' - ',['some-prefix',!Ref Region,!Ref Stage]]' –

相關問題