Project Working With AWS Lambda
Project Working With AWS Lambda
Lab overview
In this lab, you deploy and configure an AWS Lambda based serverless
computing solution. The Lambda function generates a sales analysis
report by pulling data from a database and emailing the results daily. The
database connection information is stored in Parameter Store, a capability
of AWS Systems Manager. The database itself runs on an Amazon Elastic
Compute Cloud (Amazon EC2) Linux, Apache, MySQL, and PHP (LAMP)
instance.
The following diagram shows the architecture of the sales analysis report
solution and illustrates the order in which actions occur.
Step Details
1 An Amazon CloudWatch Events event calls the salesAnalysisReport Lambda function at 8 PM eve
The salesAnalysisReport Lambda function invokes another Lambda function, salesAnalysisReport
2
data.
3 The salesAnalysisReportDataExtractor function runs an analytical query against the café database
4 The query result is returned to the salesAnalysisReport function.
The salesAnalysisReport function formats the report into a message and publishes it to the salesAn
5
Notification Service (Amazon SNS) topic.
Step Details
6 The salesAnalysisReportTopic SNS topic sends the message by email to the administrator.
In this lab, the Python code for each Lambda function is provided to you
so that you can focus on the SysOps tasks of deploying, configuring, and
testing the serverless solution components.
Objectives
After completing this lab, you will be able to do the following:
Duration
This lab requires approximately 60 minutes to complete.
2. Wait until the message "Lab status: ready" appears, and then
choose X to close the Start Lab panel.
3. At the top of these instructions, choose AWS to open the AWS
Management Console on a new browser tab. The system
automatically signs you in.
Tip If a new browser tab does not open, a banner or icon at the top
of your browser will indicate that your browser is preventing the site
from opening pop-up windows. Choose the banner or icon, and
choose Allow pop-ups.
Leave this browser tab open. You return to it later in this lab.
16. To download the lab files required by this task to your local
machine, choose the following links:
pymysql-v3.zip
salesAnalysisReportDataExtractor-v3.zip
Note: The salesAnalysisReportDataExtractor-v3.zip file is a Python
implementation of a Lambda function that makes use of the
PyMySQL open-source client library to access the MySQL café
database. This library has been packaged into the pymysql-v3.zip
which is uploaded to Lambda layer next.
Tip: The Lambda layers feature requires that the .zip file containing
the code or library conform to a specific folder structure. The
pymysqlLibary.zip file used in this lab was packaged using the
following folder structure:
Note: If the code does not yet display in the function code editor,
refresh the console so that it displays.
Tip: You can ignore the warning (if any) that recommends
choosing at least two subnets to run in high availability mode
because it is not applicable to the function.
Notice that the security group’s inbound and outbound rules are
automatically displayed following the field.
{
"dbUrl": "<value of /cafe/dbUrl parameter>",
"dbName": "<value of /cafe/dbName parameter>",
"dbUser": "<value of /cafe/dbUser parameter>",
"dbPassword": "<value of /cafe/dbPassword parameter>"
}
{
"errorMessage": "2019-02-14T04:14:15.282Z ff0c3e8f-1985-44a3-8022-
519f883c8412 Task timed out after 3.00 seconds"
}
This message indicates that the function timed out after 3 seconds.
The Log output section includes lines starting with the following
keywords:
o One of the first things that this function does is connect to the
MySQL database running in a separate EC2 instance. It waits a
certain amount of time to establish a successful connection.
After this time passes, if the connection is unsuccessful, the
function times out.
o By default, a MySQL database uses the MySQL protocol and
listens on port number 3306 for client access.
o Choose the Configuration tab again, and choose VPC. Notice
the Inbound rules for the security group that are used by the
EC2 instance running the database. Is the database port
number (3306) listed? You can choose the security group link
if you want to edit and add an inbound rule to it.
50. Once you have corrected the problem, return to browser tab
with the salesAnalysisReportDataExtractor function page.
Choose the Test tab, and choose Test again.
You should now see a green box showing the message “Execution
result: succeeded (logs).” This message indicates that the function
ran successfully.
{
"statusCode": 200,
"body": []
}
The body field, which contains the report data that the function
extracted, is empty because there is no order data in the database.
52. To open the café website on a new browser tab, find the
public IP address of the café EC2 instance.
The URL for the website has the format http://publicIP/cafe where
publicIP is the public IP address of the café EC2 instance. There are
two ways to find the public IP address:
Option 1:
Option 2:
Now that there is order data in the database, you test the function
again.
{
"statusCode": 200,
"body": [
{
"product_group_number": 1,
"product_group_name": "Pastries",
"product_id": 1,
"product_name": "Croissant",
"quantity": 1
},
{
"product_group_number": 2,
"product_group_name": "Drinks",
"product_id": 8,
"product_name": "Hot Chocolate",
"quantity": 2
}
]
}
Note: If the Topics link is not visible, choose the three horizontal
lines icon, and then choose Topics.
You need to specify this ARN when you configure the next Lambda
function.
63. Check the inbox for the email address that you provided.
You should see an email from SARTopic with the subject "AWS
Notification - Subscription Confirmation."
A new browser tab opens and displays a page with the message
"Subscription confirmed!"
aws configure
cd activity-files
ls
Note: Before you create the function, you must retrieve the ARN of
the salesAnalysisReportRole IAM role. You specify it in the following
steps.
72. To find the ARN of an IAM role, open the IAM management
console, and choose Roles.
73. In the search box, enter salesAnalysisReportRole, and
choose the role name. The Summary page includes the ARN.
74. Copy and paste the ARN to a text editor document.
75. Next, you use the Lambda create-function command to create
the Lambda function and configure it to use the
salesAnalysisReportRole IAM role.
In particular, read through the function code, and use the embedded
comments to help you understand the logic.
Notice on line 26 that the function retrieves the ARN of the topic to
publish to, from an environment variable named topicARN.
Therefore, you need to define that variable in the Environment
variables panel.
83. Choose the Test tab, and configure the test event as follows:
o For Test event action, choose Create new event.
o For Event name, enter SARTestEvent
o For Template, choose hello-world.
The function does not require any input parameters. Leave the
default JSON lines as is.
Tip: If you get a timeout error, choose the Test button again.
Sometimes, when you first run a function, it takes a little longer to
initialize, and the Lambda default timeout value (3 seconds) is
exceeded. Usually, you can run the function again, and the error will
go away. Alternatively, you can increase the timeout value. To do
so, follow these steps:
cron(35 11 ? * MON-SAT *)
cron(35 16 ? * MON-SAT *)
The new trigger is created and displayed in the Function overview panel
and Triggers pane.
92. Consider the following challenge question, and adjust the Cron
expression as needed: What should the Cron expression be when
you deploy the function in production? Remember that you need to
schedule the trigger every day, Monday through Saturday. Assume
the you are in the UTC time zone.
93. Wait until 5 minutes have elapsed, and then check your email
inbox.
If there were no errors, you should see a new email from AWS
Notifications with a subject of "Daily Sales Analysis Report." The
CloudWatch Events event invoked this message at the time that you
specified in the Cron expression.
Conclusion
Congratulations! You now have successfully done the following:
Recognized necessary IAM policy permissions to enable a Lambda
function to other AWS resources
Created a Lambda layer to satisfy an external library dependency
Created Lambda functions that extract data from database, and
send reports to user.
Deployed and tested a Lambda function that is initiated based on a
schedule and that invokes another function
Used CloudWatch logs to troubleshoot any issues running a Lambda
function