Intermediate 25 min

Step 3: Connect Trigger and Function

A trigger is the link between your storage bucket and your function. When a file is uploaded, the trigger automatically invokes your function.

What is a Trigger?

A trigger is an automatic connection:

  • Event source: Storage bucket
  • Event type: File uploaded, deleted, etc.
  • Target: Your serverless function

Think of it like this: “When X happens in the bucket, automatically run Y function.”

How Triggers Work

Here’s the flow:

1. Upload File 2. Storage Emits Event 3. Trigger Routes Event 4. Function Runs

Event Types

You can trigger on different events:

Object Created

  • File uploaded
  • File copied
  • Most common for notifications

Object Deleted

  • File removed
  • Useful for cleanup workflows

Object Updated

  • File modified
  • Metadata changed

For this tutorial, we’ll use Object Created.

Configure Trigger: AWS

Using AWS Console

  1. Go to S3 → Your bucket
  2. Click Properties tab
  3. Scroll to Event notifications
  4. Click Create event notification
  5. Configure:
    • Event name: notify-on-upload
    • Event types: Select All object create events
    • Destination: Lambda function
    • Function: Select your function
  6. Click Save changes

Using AWS CLI

# First, create the Lambda function (we'll do this in next step)
# Then add permission for S3 to invoke it

# Add S3 invoke permission to Lambda
aws lambda add-permission \
--function-name notify-on-upload \
--principal s3.amazonaws.com \
--statement-id s3-trigger \
--action "lambda:InvokeFunction" \
--source-arn arn:aws:s3:::my-notifications-bucket-2025

# Create S3 event notification configuration
aws s3api put-bucket-notification-configuration \
--bucket my-notifications-bucket-2025 \
--notification-configuration '{
  "LambdaFunctionConfigurations": [{
    "Id": "notify-on-upload",
    "LambdaFunctionArn": "arn:aws:lambda:us-east-1:123456789:function:notify-on-upload",
    "Events": ["s3:ObjectCreated:*"]
  }]
}'

Filter Events

You can filter which files trigger the function:

By file extension:

  • Only .pdf files
  • Only .jpg or .png images
  • Exclude certain types

By path prefix:

  • Only files in documents/ folder
  • Only files in incoming/ folder
  • Ignore files in archive/
{
"LambdaFunctionConfigurations": [{
  "Id": "notify-pdf-only",
  "LambdaFunctionArn": "arn:aws:lambda:...",
  "Events": ["s3:ObjectCreated:*"],
  "Filter": {
    "Key": {
      "FilterRules": [
        {"Name": "suffix", "Value": ".pdf"},
        {"Name": "prefix", "Value": "documents/"}
      ]
    }
  }
}]
}

Common Pitfalls

Pitfall 1: Wrong event type

// ❌ Wrong: This fires on ALL events
Events: ["s3:*"]

// ✅ Correct: Only on creation
Events: ["s3:ObjectCreated:*"]

Pitfall 2: Trigger in different region

  • Function must be in same region as bucket (usually)
  • Or configure cross-region permissions
  • Check your provider’s requirements

Pitfall 3: Trigger not enabled

  • Verify trigger is active in console
  • Check function permissions
  • Review function logs for errors

Pitfall 4: Missing permissions

  • Function needs permission to be invoked by storage
  • Storage needs permission to invoke function
  • Check IAM roles and policies

Verify Trigger Configuration

Test that your trigger works:

  1. Upload a test file to your bucket
  2. Check function logs - Should see function execution
  3. Verify event received - Function should log the event
# Upload test file
echo "Test content" > test.txt
aws s3 cp test.txt s3://my-notifications-bucket-2025/

# Check Lambda logs
aws logs tail /aws/lambda/notify-on-upload --follow

# Or check in CloudWatch Logs console

Knowledge Check

What’s Next?

Now that the trigger is connected, let’s add email sending functionality. In the next page, we’ll integrate an email service to send notifications.