Serverless architectures are a cloud computing paradigm that allows developers to build and deploy applications without the need to manage servers directly (PaaS). In this model, the cloud provider takes care of server provisioning, scaling, and maintenance, enabling developers to focus solely on writing code and paying only for the actual resources consumed during execution.
This approach offers reduced operational overhead, improved cost efficiency, and faster time-to-market for various types of applications and services.
Let's say Netflix wants to send an email to a group of customers when a new movie is released.
In this tutorial we configure a Lambda function to be triggered whenever a new movie is added in the movies DynamoDB table. The Lambda function would send an email to those who have subscribed to receive notifications new movies.
-
Sign in to the Amazon SNS console.
-
In the left navigation pane, choose Topics.
-
On the Topics page, choose Create topic.
-
By default, the console creates a FIFO topic. Choose Standard.
-
In the Details section, enter a Name for the topic.
-
Scroll to the end of the form and choose Create topic. The console opens the new topic's Details page.
-
In the left navigation pane, choose Subscriptions.
-
On the Subscriptions page, choose Create subscription.
-
On the Create subscription page, choose the Topic ARN field to see a list of the topics in your AWS account.
-
Choose the topic that you created in the previous step.
-
For Protocol, choose Email.
-
For Endpoint, enter an email address that can receive notifications.
-
Choose Create subscription.
-
The console opens the new subscription's Details page.
-
Check your email inbox and choose Confirm subscription in the email from AWS Notifications. The sender ID is usually
no-reply@sns.amazonaws.com
. -
Amazon SNS opens your web browser and displays a subscription confirmation with your subscription ID.
- In the DynamoDB navigation pane on the left side, choose Tables.
- Choose your table from the table list.
- Choose the Exports and streams tab for your table.
- Under DynamoDB stream details choose Enable.
- Choose New and old images and click Enable stream.
-
Open the Functions page of the Lambda console.
-
Choose Create function.
-
Under Basic information, do the following:
- Enter Function name.
- For Runtime, confirm that Python 3.x is selected.
-
Choose Create function.
-
Enter your function, copy the content of
new_movie_lambda/app.py
and paste it in the Code source. -
Click the Deploy button.
-
On the same page, click Add trigger and choose your Dynamo table as a source trigger.
-
Configure an environment variable named
TOPIC_ARN
with the ARN of your topic. The lambda will send a notification to this topic. -
In your Lambda IAM role, attach the
AWSLambdaInvocation-DynamoDB
andAmazonSNSFullAccess
permissions to allow your Lambda read items from DynamoDB and publish messages to your SNS topic.
Test your Lambda function by creating a new movie item in the Dynamo table and watch for new email in your inbox.
- Access your Grafana instance (it must be running within an EC2 instance).
- Add CloudWatch as a data source under Grafana's data source settings. To allow Grafana access CloudWatch, create a role with permission on CloudWatch and attach it to your EC2 instance.
- Create panels in Grafana to display the following Lambda metrics: invocation count, errors and running duration.
As DevOps engineers, we prefer deploying the Lambda function using Docker containers instead of directly copying the source code.
- Under
new_movie_lambda/
create aDockerfile
to containerize your code:
FROM public.ecr.aws/lambda/python:3.10
# TODO your instructions here....
CMD ["app.lambda_handler"]
-
Build the image and push it to an ECR repo:
- Open the Amazon ECR console at https://console.aws.amazon.com/ecr/repositories.
- In the navigation pane, choose Repositories.
- On the Repositories page, choose Create repository.
- For Repository name, enter a unique name for your repository. E.g.
john_new_movie_notify
- Choose Create repository.
- Select the repository that you created and choose View push commands to view the steps to build and push an image to your new repository.
-
Create a new Lambda function based on your Docker image.
-
Test it.