Bird
Raised Fist0
AWScloud~5 mins

Event triggers for Lambda in AWS - Commands & Configuration

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

Practice

(1/5)
1. Which AWS service event can automatically start a Lambda function when a new file is uploaded?
easy
A. AWS CloudTrail log event
B. Amazon EC2 instance launch event
C. Amazon S3 object upload event
D. Amazon RDS backup event

Solution

  1. Step 1: Understand Lambda triggers

    Lambda functions can be triggered by events from various AWS services.
  2. Step 2: Identify common event sources

    Amazon S3 triggers Lambda when a new object is uploaded to a bucket.
  3. Final Answer:

    Amazon S3 object upload event -> Option C
  4. Quick Check:

    S3 upload triggers Lambda = A [OK]
Hint: Remember: S3 uploads often trigger Lambda automatically [OK]
Common Mistakes:
  • Confusing EC2 events as Lambda triggers
  • Thinking CloudTrail logs directly trigger Lambda
  • Assuming RDS backups trigger Lambda
2. Which of the following is the correct way to configure an SQS queue as an event source for a Lambda function?
easy
A. Enable CloudWatch Events on the SQS queue
B. Add an S3 trigger to the Lambda function
C. Attach an IAM role to the SQS queue
D. Create an event source mapping between the SQS queue and Lambda

Solution

  1. Step 1: Know how Lambda connects to SQS

    Lambda uses event source mappings to poll messages from SQS queues.
  2. Step 2: Identify correct configuration

    Creating an event source mapping links the SQS queue to Lambda for automatic invocation.
  3. Final Answer:

    Create an event source mapping between the SQS queue and Lambda -> Option D
  4. Quick Check:

    SQS triggers Lambda via event source mapping = D [OK]
Hint: Event source mapping links SQS to Lambda directly [OK]
Common Mistakes:
  • Confusing S3 triggers with SQS triggers
  • Attaching IAM roles to SQS instead of Lambda
  • Using CloudWatch Events incorrectly for SQS
3. Given this Lambda trigger configuration snippet:
{
  "EventSourceArn": "arn:aws:sqs:us-east-1:123456789012:MyQueue",
  "FunctionName": "ProcessQueueMessages",
  "Enabled": true
}

What happens when a new message arrives in the SQS queue?
medium
A. The Lambda function ProcessQueueMessages is invoked automatically
B. The message is deleted from the queue without processing
C. The Lambda function is not triggered until manually invoked
D. An error occurs because the configuration is incomplete

Solution

  1. Step 1: Understand event source mapping

    The snippet shows an event source mapping linking an SQS queue to a Lambda function.
  2. Step 2: Effect of Enabled=true

    With Enabled set to true, Lambda polls the queue and invokes the function on new messages.
  3. Final Answer:

    The Lambda function ProcessQueueMessages is invoked automatically -> Option A
  4. Quick Check:

    Enabled event source mapping triggers Lambda = C [OK]
Hint: Enabled event source mapping means automatic Lambda invocation [OK]
Common Mistakes:
  • Assuming manual invocation is needed
  • Thinking messages get deleted without processing
  • Believing configuration is incomplete without batch size
4. You configured a DynamoDB stream as a trigger for your Lambda function, but the function is not invoked when the table changes. What is the most likely cause?
medium
A. The DynamoDB stream is disabled or not enabled
B. The DynamoDB table has no data
C. The Lambda function code is empty
D. The Lambda execution role lacks permission to read the DynamoDB stream

Solution

  1. Step 1: Check DynamoDB stream status

    For Lambda to trigger, the DynamoDB stream must be enabled on the table.
  2. Step 2: Confirm stream is active

    If the stream is disabled or not enabled, no events are generated to invoke Lambda.
  3. Final Answer:

    The DynamoDB stream is disabled or not enabled -> Option A
  4. Quick Check:

    DynamoDB stream must be enabled for Lambda trigger = A [OK]
Hint: Ensure DynamoDB stream is enabled to trigger Lambda [OK]
Common Mistakes:
  • Ignoring stream enablement status
  • Assuming empty table stops triggers
  • Blaming Lambda code without checking stream
5. You want a Lambda function to run every day at 6 AM UTC without any external event. Which AWS service and configuration should you use to trigger the Lambda?
hard
A. Configure an S3 bucket event to trigger Lambda at 6 AM
B. Create a CloudWatch Events (EventBridge) rule with a cron expression to trigger Lambda
C. Set up an SQS queue with a message sent daily at 6 AM
D. Enable DynamoDB Streams to trigger Lambda daily

Solution

  1. Step 1: Identify scheduled triggers for Lambda

    To run Lambda on a schedule without external events, use CloudWatch Events (EventBridge) rules.
  2. Step 2: Use cron expression in EventBridge

    Create a rule with a cron expression for 6 AM UTC daily to trigger the Lambda function automatically.
  3. Final Answer:

    Create a CloudWatch Events (EventBridge) rule with a cron expression to trigger Lambda -> Option B
  4. Quick Check:

    Scheduled Lambda uses EventBridge cron rule = B [OK]
Hint: Use EventBridge cron rules for scheduled Lambda runs [OK]
Common Mistakes:
  • Trying to trigger Lambda via S3 or SQS for schedule
  • Assuming DynamoDB Streams can schedule events
  • Not using cron expressions for timing
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