Amazon Lambda - Hands On Demo
In this Lab, we will invoke an API configured through API Gateway.
API will in turn invoke the Lambda function, which will trigger an SNS Email Notification.
API Gateway
Lambda Function
Simple Notification Service (SNS)
Create a topic and a subscription for Email Notification
Please follow this tutorial first to create a SNS Topic and a Subscription first
Create a Lambda Function
- Paste the following code to create the Lambda function named "PythonHelloWorld".
- Runtime: Python 3.12
- Keep all others values as default.
import boto3
def lambda_handler(event, context):
# Initialize SNS client
sns = boto3.client('sns')
# Specify your SNS topic ARN
topic_arn = 'arn:aws:sns:us-east-1:211125437318:MyTopic'
# Publish a message to the SNS topic
sns.publish(
TopicArn=topic_arn,
Message='Hello from Lambda! This is the message that will be sent in the email.',
Subject='Email Subject from Lambda'
)
return {
'statusCode': 200,
'body': 'Message published to SNS topic successfully!'
}
Add an API Gateway Trigger
- Click "Add Trigger" and select "API Gateway".
- Select "Create a new API".
- API Type: HTTP API.
- Security: Open.
- API Name (default): PythonHelloWorld-API.
- Select "Cross-origin resource sharing (CORS)".
- Click Add.
Add a Destination
- Source: Asynchronous invocation
- Condition: On success
- Destination type: SNS topic
- Destination: arn:aws:sns:us-east-1:<ACCOUNT-ID>:MyTopic
- Permissions: Add required permissions
Test
- Open a new browser window.
- Paste the API URL like.
- Check Email.
Thanks
For
Watching
Amazon Lambda - Hands On Demo
By Deepak Dubey
Amazon Lambda - Hands On Demo
Amazon Lambda - Hands On Demo
- 61