Bird
Raised Fist0
AWScloud~5 mins

Lambda with S3 event triggers in AWS - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you configure an AWS Lambda function with an S3 event trigger for object creation?
easy
A. The Lambda function runs manually and is not triggered by S3 events.
B. The Lambda function runs only when the S3 bucket is deleted.
C. The Lambda function runs automatically whenever a new file is uploaded to the S3 bucket.
D. The Lambda function runs only when an object is deleted from the S3 bucket.

Solution

  1. Step 1: Understand S3 event triggers

    S3 event triggers can invoke Lambda functions automatically on specific events like object creation.
  2. Step 2: Identify the event type for object creation

    When a new file is uploaded, the event type is 'ObjectCreated', which triggers the Lambda function.
  3. Final Answer:

    The Lambda function runs automatically whenever a new file is uploaded to the S3 bucket. -> Option C
  4. Quick Check:

    S3 ObjectCreated event triggers Lambda automatically [OK]
Hint: Remember: S3 triggers Lambda on file uploads automatically [OK]
Common Mistakes:
  • Confusing object deletion with creation events
  • Thinking Lambda runs manually without triggers
  • Assuming triggers only work on bucket deletion
2. Which of the following is the correct JSON snippet to configure an S3 event trigger for a Lambda function on object creation?
easy
A. { "Events": ["s3:ObjectCreated:*"], "LambdaFunctionArn": "arn:aws:lambda:region:account-id:function:myFunction" }
B. { "Events": ["s3:ObjectRemoved:*"], "LambdaFunctionArn": "arn:aws:lambda:region:account-id:function:myFunction" }
C. { "Events": ["lambda:InvokeFunction"], "LambdaFunctionArn": "arn:aws:s3:::myBucket" }
D. { "Events": ["s3:ObjectCreated:Delete"], "LambdaFunctionArn": "arn:aws:lambda:region:account-id:function:myFunction" }

Solution

  1. Step 1: Identify correct event type for object creation

    The event type for object creation is "s3:ObjectCreated:*" which covers all create events.
  2. Step 2: Check Lambda function ARN placement

    The LambdaFunctionArn must point to the Lambda function's ARN, not the bucket ARN.
  3. Final Answer:

    { "Events": ["s3:ObjectCreated:*"], "LambdaFunctionArn": "arn:aws:lambda:region:account-id:function:myFunction" } -> Option A
  4. Quick Check:

    Use "s3:ObjectCreated:*" for creation triggers [OK]
Hint: Use "s3:ObjectCreated:*" for all object creation events [OK]
Common Mistakes:
  • Using ObjectRemoved event instead of ObjectCreated
  • Putting bucket ARN instead of Lambda ARN
  • Using invalid event names like ObjectCreated:Delete
3. Given this Lambda function code triggered by S3 object creation:
def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    return f"File {key} uploaded to {bucket}"

What will be the output if a file named 'photo.jpg' is uploaded to bucket 'my-images'?
medium
A. "File uploaded to my-images"
B. "File photo.jpg uploaded to my-images"
C. "File my-images uploaded to photo.jpg"
D. Runtime error due to missing event keys

Solution

  1. Step 1: Extract bucket and key from event

    The code accesses event['Records'][0]['s3']['bucket']['name'] as bucket name and event['Records'][0]['s3']['object']['key'] as file key.
  2. Step 2: Substitute values from the upload event

    For file 'photo.jpg' uploaded to 'my-images', bucket='my-images' and key='photo.jpg'.
  3. Final Answer:

    "File photo.jpg uploaded to my-images" -> Option B
  4. Quick Check:

    Event keys map file and bucket correctly [OK]
Hint: Check event['Records'][0]['s3'] keys for bucket and object [OK]
Common Mistakes:
  • Mixing bucket name and object key positions
  • Assuming event keys are missing causing errors
  • Confusing string formatting output
4. You configured an S3 bucket to trigger a Lambda function on object creation, but the Lambda is not invoked. What is the most likely cause?
medium
A. The S3 bucket notification configuration is missing permission for S3 to invoke Lambda.
B. The S3 bucket is in a different AWS region than the Lambda function.
C. The Lambda function code has a syntax error.
D. The Lambda function's execution role lacks permission to read from S3.

Solution

  1. Step 1: Check permissions for S3 to invoke Lambda

    S3 must have permission to invoke the Lambda function via a resource-based policy on Lambda.
  2. Step 2: Understand common misconfigurations

    Missing this permission causes Lambda not to trigger even if bucket notifications are set.
  3. Final Answer:

    The S3 bucket notification configuration is missing permission for S3 to invoke Lambda. -> Option A
  4. Quick Check:

    S3 needs invoke permission on Lambda [OK]
Hint: Grant S3 permission to invoke Lambda in Lambda's resource policy [OK]
Common Mistakes:
  • Confusing Lambda execution role with S3 invoke permission
  • Assuming code errors prevent invocation
  • Thinking region mismatch blocks invocation
5. You want a Lambda function to process only PNG files uploaded to an S3 bucket. How should you configure the S3 event trigger to achieve this?
hard
A. Set the event type to 's3:ObjectCreated:Put' without any filters.
B. Set the event type to 's3:ObjectRemoved:*' and add a prefix filter '.png'.
C. Configure the Lambda function code to ignore non-PNG files without using filters.
D. Set the event type to 's3:ObjectCreated:*' and add a filter with suffix '.png' in the bucket notification configuration.

Solution

  1. Step 1: Use event type for object creation

    Use 's3:ObjectCreated:*' to capture all create events including uploads.
  2. Step 2: Apply suffix filter for '.png'

    Configure the bucket notification with a filter to trigger Lambda only for files ending with '.png'.
  3. Final Answer:

    Set the event type to 's3:ObjectCreated:*' and add a filter with suffix '.png' in the bucket notification configuration. -> Option D
  4. Quick Check:

    Use event filters to limit Lambda triggers by file type [OK]
Hint: Use suffix filter '.png' in S3 event trigger for file type filtering [OK]
Common Mistakes:
  • Using ObjectRemoved event instead of ObjectCreated
  • Relying only on Lambda code filtering without event filters
  • Using prefix filter with file extension instead of suffix
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard