0
0
AWScloud~5 mins

Lambda with S3 event triggers in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your program to run automatically when a file is added to storage. AWS Lambda with S3 event triggers lets you do this by running code whenever a new file appears in an S3 bucket.
When you want to resize images automatically after uploading them to storage.
When you need to process data files as soon as they are saved in a storage bucket.
When you want to send notifications or logs every time a new file is added.
When you want to automate backups or data transformations triggered by file uploads.
When you want to scan files for viruses immediately after upload.
Config File - lambda_s3_event.json
lambda_s3_event.json
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "MyLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Handler": "index.handler",
        "Role": "arn:aws:iam::123456789012:role/lambda-execution-role",
        "Code": {
          "ZipFile": "exports.handler = async (event) => { console.log('File uploaded:', event.Records[0].s3.object.key); return; };"
        },
        "Runtime": "nodejs18.x"
      }
    },
    "S3Bucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "example-lambda-trigger-bucket"
      }
    },
    "BucketNotificationPermission": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": { "Ref": "MyLambdaFunction" },
        "Action": "lambda:InvokeFunction",
        "Principal": "s3.amazonaws.com",
        "SourceArn": { "Fn::GetAtt": ["S3Bucket", "Arn"] }
      }
    },
    "BucketNotificationConfig": {
      "Type": "AWS::S3::BucketNotification",
      "Properties": {
        "Bucket": { "Ref": "S3Bucket" },
        "NotificationConfiguration": {
          "LambdaConfigurations": [
            {
              "Event": "s3:ObjectCreated:Put",
              "Function": { "Ref": "MyLambdaFunction" }
            }
          ]
        }
      },
      "DependsOn": ["BucketNotificationPermission"]
    }
  }
}

This file is an AWS CloudFormation template that creates three main things:

  • MyLambdaFunction: A Lambda function that runs Node.js code logging the uploaded file name.
  • S3Bucket: An S3 bucket named 'example-lambda-trigger-bucket' where files are uploaded.
  • BucketNotificationPermission and BucketNotificationConfig: Permissions and configuration to let the S3 bucket trigger the Lambda function when a new file is uploaded.

This setup automates running code on file uploads without manual intervention.

Commands
This command creates the Lambda function, S3 bucket, and sets up the trigger using the CloudFormation template. The capability flag allows creating roles.
Terminal
aws cloudformation deploy --template-file lambda_s3_event.json --stack-name lambda-s3-trigger-stack --capabilities CAPABILITY_NAMED_IAM
Expected OutputExpected
Waiting for stack create/update to complete... Successfully created/updated stack - lambda-s3-trigger-stack
--capabilities CAPABILITY_NAMED_IAM - Allows CloudFormation to create IAM roles needed for Lambda execution.
Uploads a file named testfile.txt to the S3 bucket to trigger the Lambda function.
Terminal
aws s3 cp testfile.txt s3://example-lambda-trigger-bucket/
Expected OutputExpected
upload: ./testfile.txt to s3://example-lambda-trigger-bucket/testfile.txt
Checks the latest logs from the Lambda function to confirm it ran after the file upload.
Terminal
aws logs filter-log-events --log-group-name /aws/lambda/MyLambdaFunction --limit 5
Expected OutputExpected
{ "events": [ { "message": "File uploaded: testfile.txt", "timestamp": 1680000000000 } ] }
--limit 5 - Limits the output to the 5 most recent log events.
Key Concept

If you remember nothing else from this pattern, remember: S3 can automatically run your Lambda code whenever a new file is added, making automation easy.

Common Mistakes
Not giving Lambda permission to be triggered by S3.
Without permission, S3 cannot invoke the Lambda function, so the trigger won't work.
Add a Lambda permission resource allowing S3 to invoke the function, as shown in the CloudFormation template.
Uploading files to the wrong bucket name.
The Lambda trigger is set only on the specific bucket; uploading elsewhere won't trigger the function.
Always upload files to the exact bucket configured for the trigger, here 'example-lambda-trigger-bucket'.
Not checking Lambda logs to verify the function ran.
Without checking logs, you can't confirm if the trigger worked or debug issues.
Use AWS CLI or console to view Lambda logs after uploading files.
Summary
Use a CloudFormation template to create a Lambda function, S3 bucket, and set up the event trigger.
Deploy the stack with AWS CLI including permissions for Lambda to be triggered by S3.
Upload a file to the bucket to automatically run the Lambda function.
Check Lambda logs to confirm the function executed and processed the event.