Enable Lambda function to an S3 bucket using cloudformation

后端 未结 5 640
醉话见心
醉话见心 2021-01-30 13:46

We are creating an S3 bucket using a CloudFormation template. I would like to associate (Add an event to S3 bucket) a Lambda function whenever a file is added to the S3 bucket.

5条回答
  •  执笔经年
    2021-01-30 14:40

    You need a NotificationConfiguration property in your CloudFormation template. Unfortunately, it seems to require the bucket to already exist. To get around this, you can create an initial stack, then update it with the NotificationConfiguration. For example:

        // template1.json
        {
          "AWSTemplateFormatVersion": "2010-09-09",
          "Parameters": {
            "mylambda": {
              "Type": "String"
            }
          },
          "Resources": {
            "bucketperm": {
              "Type": "AWS::Lambda::Permission",
              "Properties" : {
                "Action": "lambda:InvokeFunction",
                "FunctionName": {"Ref": "mylambda"},
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {"Ref": "AWS::AccountId"},
                "SourceArn": { "Fn::Join": [":", [
                    "arn", "aws", "s3", "" , "", {"Ref" : "mybucket"}]]
                }
              }
            },
            "mybucket": {
              "Type": "AWS::S3::Bucket"
            }
          }
        }
    
        // template2.json -- adds the NotificationConfiguration
        {
          "AWSTemplateFormatVersion": "2010-09-09",
          "Parameters": {
            "mylambda": {
              "Type": "String"
            }
          },
          "Resources": {
            "bucketperm": {
              "Type": "AWS::Lambda::Permission",
              "Properties" : {
                "Action": "lambda:InvokeFunction",
                "FunctionName": {"Ref": "mylambda"},
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {"Ref": "AWS::AccountId"},
                "SourceArn": { "Fn::Join": [":", [
                    "arn", "aws", "s3", "" , "", {"Ref" : "mybucket"}]]
                }
              }
            },
            "mybucket": {
              "Type": "AWS::S3::Bucket",
              "Properties": {
                "NotificationConfiguration": {
                  "LambdaConfigurations": [
                    {
                      "Event" : "s3:ObjectCreated:*",
                      "Function" : {"Ref": "mylambda"}
                    }
                  ]
                }
              }
            }
          }
        }
    

    You can use the AWS CLI tool to create the stack like this:

        $ aws cloudformation create-stack --stack-name mystack --template-body file://template1.json --parameters ParameterKey=mylambda,ParameterValue=
        # wait until stack is created
        $ aws cloudformation update-stack --stack-name mystack --template-body file://template2.json --parameters ParameterKey=mylambda,ParameterValue=
    

提交回复
热议问题