In this task, we’ll create the first Lambda function for our application. This function serves as the entry point, receiving information about new posts that need to be converted into audio files.
AWS Lambda allows us to run code without provisioning or managing servers. It’s ideal for our use case because:
Access Lambda in AWS Console
Create a New Function
PostReader_NewPostLab-Lambda-RoleAdd Function Code
import boto3
import os
import uuid
def lambda_handler(event, context):
recordId = str(uuid.uuid4())
voice = event["voice"]
text = event["text"]
print('Generating new DynamoDB record, with ID: ' + recordId)
print('Input Text: ' + text)
print('Selected voice: ' + voice)
# Creating new record in DynamoDB table
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['DB_TABLE_NAME'])
table.put_item(
Item={
'id' : recordId,
'text' : text,
'voice' : voice,
'status' : 'PROCESSING'
}
)
# Sending notification about new post to SNS
client = boto3.client('sns')
client.publish(
TopicArn = os.environ['SNS_TOPIC'],
Message = recordId
)
return recordId
Examine the Code The Lambda function performs the following tasks:
Deploy the Function
Configure Environment Variables
SNS_TOPIC, Value: [Paste your SNS topic ARN]DB_TABLE_NAME, Value: postsUpdate Function Configuration
Create a Test Event
{
"voice": "Joanna",
"text": "This is working!"
}
Run the Test
Congratulations! You’ve successfully created and tested the New Post Lambda function. This function will serve as the entry point for your application, handling new post submissions and initiating the audio conversion process.