Cloudformation Creating A Trigger Event For Lamdba Function That Listens To File Uploads To S3 Bucket

Cloudformation Creating A Trigger Event For Lamdba Function That Listens To File Uploads To S3 Bucket

Ok that title felt like a mouthful.

I wanted to be able to listen to file uploads to an AWS S3 bucket and trigger a Lambda function to run some processing on the uploaded file.

Lots of ways you could use this.

Creating a trigger in a Lamdba function is pretty simple just navigate to your function in the AWS console and click configurations and then triggers.

You can then click add trigger and setup your trigger.

Easy enough now I wanted to move all this into a Cloudformation template here is the final code.

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "WatcherWorkflow",
    "Parameters": {
        "WatchBucket": {
            "Description": "Watch Bucket Name",
            "Type": "String",
            "Default": ""
        }
    },
    "Resources": {
        "CreateWatchBucket": {
            "Type": "AWS::S3::Bucket",
            "DependsOn": [
                "ProcessingLambdaPermission"
            ],
            "Properties": {
                "BucketName": {
                    "Ref": "WatchBucket"
                },
                "NotificationConfiguration": {
                    "LambdaConfigurations": [
                       {
                          "Event": "s3:ObjectCreated:*",
                          "Function":  {
                                "Fn::GetAtt": ["lambdaWatchFunction", "Arn"]
                            },
                          "Filter": {
                             "S3Key": {
                                "Rules": [
                                   {
                                      "Name": "suffix",
                                      "Value": ".mp4"
                                   }
                                ]
                             }
                          }
                       }
                    ]
                 },
                "CorsConfiguration": {
                    "CorsRules": [{
                        "AllowedHeaders": [
                            "*"
                        ],
                        "AllowedMethods": [
                            "HEAD",
                            "PUT",
                            "POST",
                            "GET"
                        ],
                        "AllowedOrigins": [
                            "*"
                        ],
                        "MaxAge": "3600"
                    }]
                }
            }
        },
        "ProcessingLambdaPermission": {
            "Type": "AWS::Lambda::Permission",
            "DependsOn": "lambdaWatchFunction",
            "Properties": {
                "Action": "lambda:InvokeFunction",
                "FunctionName": {
                    "Fn::GetAtt": [
                       "lambdaWatchFunction",
                       "Arn"
                    ]
                 },
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {
                    "Ref": "AWS::AccountId"
                },
                "SourceArn": {
                    "Fn::Sub": "arn:aws:s3:::${WatchBucket}"
                }
            }
        },
        "lambdaWatchFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Code": {
                    "S3Bucket": "s3b-lamdba-exec-tests",
                    "S3Key": "index.js.zip"
                },
                "Handler": "index.handler",
                "Role": "arn:aws:iam::00000000:role/BasicLambdaExecRole",
                "Runtime": "nodejs10.x",
                "FunctionName": "WatchWorkflow",
                "MemorySize": 128,
                "Timeout": 5,
                "Description": "Texting Lambda",
                "Environment": {
                    "Variables": {
                        "WatchBucket": {
                            "Ref": "WatchBucket"
                        }
                    }
                }
            }
        }
    },
    "Outputs": {
        "WatchBucket": {
            "Description": "Watch Bucket Name",
            "Value": {
                "Ref": "WatchBucket"
            }
        }
    }
}

The main thing I learnt with this setup is utilising the DependsOn field.

"DependsOn": "lambdaWatchFunction",

These need to be setup in a specific order for everything to work smoothly.

Leave a comment

Your email address will not be published. Required fields are marked *