0
0
AWScloud~5 mins

Event triggers for Lambda in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Event triggers start your Lambda function automatically when something happens. This helps your code run only when needed, saving time and resources.
When you want to run code after a file is uploaded to storage.
When a new message arrives in a queue and you want to process it.
When a database record changes and you want to update something.
When a scheduled time arrives and you want to run a task automatically.
When an HTTP request comes in and you want to respond with code.
Config File - lambda-event.json
lambda-event.json
{
  "Type": "AWS::Lambda::EventSourceMapping",
  "Properties": {
    "EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:my-queue",
    "FunctionName": "my-lambda-function",
    "Enabled": true,
    "BatchSize": 5
  }
}

This JSON configures an event trigger for a Lambda function.

EventSourceArn is the source of events, here an SQS queue.

FunctionName is the Lambda function to run.

Enabled turns the trigger on.

BatchSize controls how many events Lambda processes at once.

Commands
This command creates an event trigger that runs 'my-lambda-function' when messages arrive in the SQS queue 'my-queue'. Batch size 5 means it processes up to 5 messages at once.
Terminal
aws lambda create-event-source-mapping --event-source-arn arn:aws:sqs:us-east-1:123456789012:my-queue --function-name my-lambda-function --batch-size 5 --enabled
Expected OutputExpected
{ "UUID": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StartingPosition": "LATEST", "BatchSize": 5, "EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:my-queue", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function", "State": "Enabled", "StateTransitionReason": "User action" }
--event-source-arn - Specifies the event source to trigger the Lambda.
--function-name - Specifies which Lambda function to run.
--batch-size - Sets how many events Lambda processes at once.
This command lists all event triggers connected to 'my-lambda-function' so you can verify the trigger was created.
Terminal
aws lambda list-event-source-mappings --function-name my-lambda-function
Expected OutputExpected
[ { "UUID": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StartingPosition": "LATEST", "BatchSize": 5, "EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:my-queue", "FunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function", "State": "Enabled", "StateTransitionReason": "User action" } ]
--function-name - Filters triggers for the specified Lambda function.
Key Concept

If you remember nothing else from this pattern, remember: event triggers automatically run your Lambda when something important happens.

Common Mistakes
Using the wrong ARN for the event source.
Lambda won't trigger because it can't find the event source.
Double-check and copy the exact ARN of the event source like SQS queue or S3 bucket.
Not enabling the event source mapping after creation.
The trigger exists but is disabled, so Lambda never runs.
Use the --enabled flag when creating or update the mapping to enable it.
Setting batch size too high for the event source.
It can cause delays or errors if Lambda can't process many events at once.
Start with a small batch size like 5 and adjust based on performance.
Summary
Create an event source mapping to connect an event source like SQS to your Lambda function.
Use the AWS CLI to create and verify event triggers with commands.
Enable the trigger and set batch size to control how Lambda processes events.