0% found this document useful (0 votes)
238 views16 pages

Architecting On AWS - Lab 6 - Implementing A Serverless Architecture With AWS Managed Service

This document provides an overview of a lab that demonstrates implementing a serverless architecture on AWS using managed services. The lab scenario involves: 1) Uploading inventory files to S3 which triggers a Lambda function 2) The Lambda function loads the data into a DynamoDB table 3) A serverless web dashboard pulls data from DynamoDB to display inventory levels 4) Another Lambda function checks DynamoDB for low inventory and sends SMS/email notifications. The solution is serverless, automatically scales, and incurs very little cost compared to traditional server-based architectures.

Uploaded by

Deysi Rincon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views16 pages

Architecting On AWS - Lab 6 - Implementing A Serverless Architecture With AWS Managed Service

This document provides an overview of a lab that demonstrates implementing a serverless architecture on AWS using managed services. The lab scenario involves: 1) Uploading inventory files to S3 which triggers a Lambda function 2) The Lambda function loads the data into a DynamoDB table 3) A serverless web dashboard pulls data from DynamoDB to display inventory levels 4) Another Lambda function checks DynamoDB for low inventory and sends SMS/email notifications. The solution is serverless, automatically scales, and incurs very little cost compared to traditional server-based architectures.

Uploaded by

Deysi Rincon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

22/7/2021 AWS Labs

Architecting on AWS - Lab 6 -


Implementing a Serverless
Architecture with AWS Managed
Service
AWS Solutions Architect Labs

Main Page Lab 1 Lab 2 Lab 3 Lab 4 Lab 5 Lab 6

Open Console LAB OVERVIEW

Traditionally, applications run on servers. These can be physical


Connection Details servers or virtual environments running on top of physical servers,
but either requires purchasing and provisioning servers and
Region (Oregon) managing capacity. As an alternative, AWS Lambda can run
serverless code without having to pre-allocate servers. Simply
us-west-2 provide the code and define a trigger. The function can run
whenever required-once per week or hundreds of times per
second-and you only pay for what you use.

This lab demonstrates how to trigger a Lambda function when a file


is uploaded to Amazon Simple Storage Service (Amazon S3). The
file will be loaded into an Amazon DynamoDB table, and the data
will be available to view on a dashboard page that pulls the data
directly from DynamoDB. The solution is completely serverless,
automatically scalable, and incurs very little cost.

The system does not use Amazon Elastic Compute Cloud (Amazon
EC2). The system will automatically scale when it is used and incur
practically no cost when it is not being used (just a few cents for
data storage).

OBJECTIVES

After completing this lab, you will be able to:

Create a Lambda function


Trigger a Lambda function from Amazon S3 and DynamoDB
Configure Amazon SNS to send notifications

SCENARIO

https://labs.netec.com/pages/lab6.html 1/16
22/7/2021 AWS Labs

You are creating an inventory tracking system. Stores from around


the world will upload inventory files to Amazon S3. Your team wants
to be able to view the inventory levels and send a notification when
inventory levels are low.

The scenario workflow is as follows:

You upload an inventory file to an Amazon S3 bucket.


This triggers a Lambda function, which will read the file and
insert items into a DynamoDB table.

A serverless, web-based dashboard application uses Amazon


Cognito to authenticate to AWS and then gain access to the
DynamoDB table to display inventory levels.

Another Lambda function receives updates from the


DynamoDB table and sends a message to an Amazon Simple
Notification Service (Amazon SNS) topic when an inventory
item is out of stock.

Amazon SNS then sends an SMS or email notification to you


to request additional inventory.

DURATION

This lab requires approximately 60 minutes to complete.

START LAB

This starts the process of provisioning your lab resources. An


estimated amount of time to provision your labs resources is
displayed. You must wait for your resources to be provisioned
before continuing.

The following diagram shows the workflow:

https://labs.netec.com/pages/lab6.html 2/16
22/7/2021 AWS Labs

Open the AWS Console with the green button to the left of
this page.

On the login page, place the User assigned for the course

In the Password section paste the password assigned for the


course

Click "Sign in"


Congratulations you have logged in.


Click on the only account that appears in the user interface.


Select the name of the account and the list of labs will appear.

NOTE: Don’t try to change the password or the user.

NOTE: Do not change the Region unless instructed.

TASK 1: CREATING A LAMBDA FUNCTION TO LOAD DATA

In this task, you create a Lambda function to process an inventory


file. The Lambda function will read the file and insert information
into a DynamoDB table, as shown in the following diagram.

3. n the AWS Management Console, on the Services menu,


click Lambda.

4. Click Create function

Note: A blueprint is a code template for writing a Lambda function.


Blueprints are provided for standard Lambda triggers, such as
creating Alexa skills and processing Amazon Kinesis Data Firehose
streams. This lab provides you with a pre-written Lambda function,
so you will author the function from scratch.

https://labs.netec.com/pages/lab6.html 3/16
22/7/2021 AWS Labs

5. Configure the following:

Select Author from scratch


Function name: Load-Inventory

Runtime: Select Python 3.8


Expand: Change default execution role


Execution role: Select Use an existing role


Existing role: Select Lambda-Load-Inventory-Role

This role provides your Lambda function with the permissions it


needs to access Amazon S3 and DynamoDB.

6. Click Create Function


7. Scroll down to the Function Code section, and delete all of


the code that appears in the code editor.

8. Copy and paste the following code into the Function Code
editor.

Copy Code

# Load-Inventory Lambda function

# This function is triggered by an object being created


in an Amazon S3 bucket.

# The file is downloaded and each line is inserted into


a DynamoDB table.

import json, urllib, boto3, csv

# Connect to S3 and DynamoDB

s3 = boto3.resource('s3')

dynamodb = boto3.resource('dynamodb')

# Connect to the DynamoDB tables

inventoryTable = dynamodb.Table('Inventory');

# This handler is run every time the Lambda function is


triggered

def lambda_handler(event, context):

# Show the incoming event in the debug log

print("Event received by Lambda function: " +


json.dumps(event, indent=2))

# Get the bucket and object key from the event

bucket = event['Records'][0]['s3']['bucket']['name']

key = urllib.parse.unquote_plus(event['Records'][0]
['s3']['object']['key'])

https://labs.netec.com/pages/lab6.html 4/16
22/7/2021 AWS Labs
localFilename = '/tmp/inventory.txt'

# Download the file from S3 to the local filesystem

try:

s3.meta.client.download_file(bucket, key,
localFilename)

except Exception as e:

print(e)

print('Error getting object {} from bucket {}. Make


sure they exist and your bucket is in the same region
as this function.'.format(key, bucket))

raise e

# Read the Inventory CSV file

with open(localFilename) as csvfile:

reader = csv.DictReader(csvfile, delimiter=',')

# Read each row in the file

rowCount = 0

for row in reader:

rowCount += 1

# Show the row in the debug log

print(row['store'], row['item'], row['count'])

try:

# Insert Store, Item, and Count into the


Inventory table

inventoryTable.put_item(

Item={

'Store': row['store'],

'Item': row['item'],

'Count': int(row['count'])})

except Exception as e:

print(e)

print("Unable to insert data into DynamoDB


table".format(e))

# Finished!

return "%d counts inserted" % rowCount

Examine the code. It performs the following steps:

Download the file that was uploaded to Amazon S3 and


triggered the function.

Loop through each line in the file.


Insert the data into the DynamoDB Inventory table.

6. Click Deploy

Next, you will configure Amazon S3 to trigger the Lambda function


when a file is uploaded.

https://labs.netec.com/pages/lab6.html 5/16
22/7/2021 AWS Labs

TASK 2: CONFIGURING AN AMAZON S3 EVENT

Stores from around the world will provide inventory files to load into
the inventory tracking system. Rather than uploading files via FTP,
the stores can upload directly to Amazon S3. This can be done via
a web page, a script, or as part of a program. Once a file is
received, the Lambda function will be triggered, and it will load the
inventory file into a DynamoDB table, as shown in the following
diagram:

In this task, you create an Amazon S3 bucket and configure it to


trigger the Lambda function.

7. On the Services menu, click S3


8. Click Create bucket

Each bucket must have a unique name, so you will add a random
number to the bucket name. For example: inventory-123

9. Bucket name: Replacing "123" with a random number

inventory-123

10. At the bottom of the window, click Create bucket

Note: If you receive an error stating that a bucket with the same
name already exists, then change the bucket name and try again
until it is accepted.

Now, configure the bucket to automatically trigger the Lambda


function whenever a file is uploaded.

11. Click the name of your inventory-xxxx bucket. (Don't change


the Region)

12. Click the Properties tab.


https://labs.netec.com/pages/lab6.html 6/16
22/7/2021 AWS Labs

13. Scroll down to Event notifications, then click Create event


notification

You will configure an event to trigger when an object is created in


the S3 bucket.

Under General configuration section, Enter Event name: as

Load-Inventory

Under Event types section, Select All object create event


Under Destination section, Choose Lambda Function


From Specify Lambda function choose Choose from your


Lambda functions Load-Inventory

Click Save Changes

This tells Amazon S3 to trigger the Load-Inventory Lambda function


you created earlier whenever an object is created in the bucket.

Your bucket is now ready to receive inventory files.

TASK 3: TESTING THE LOADING PROCESS

You are now ready to test the inventory file loading process. You
will upload an inventory file, then check that it loaded successfully.
The following diagram shows these steps. You will test the
notification park of the workflow in a later task.

14. Right-click the following link and download the .zip file:
inventory-files.zip

15. Unzip the file

https://labs.netec.com/pages/lab6.html 7/16
22/7/2021 AWS Labs

The .zip file contains multiple inventory .csv files, which you can
use to test the system. The Berlin file contains the following data:

store,item,count

Berlin,Echo Dot,12

Berlin,Echo (2nd Gen),19

Berlin,Echo Show,18

Berlin,Echo Plus,0

Berlin,Echo Look,10

Berlin,Amazon Tap,15

20. In the console, return to your S3 bucket by clicking the


Amazon S3 tab at the top of the page.

21. Click the name of your inventory-xxxx bucket.


22. Click Upload.


23. Upload window will get open, upload one of the CSV files to
the bucket by clicking Add files button. (You can choose any
of the inventory files, but only upload one of them.)

24. Click Upload at the bottom of the page.


25. Click Close button on the Upload: status page to return back
to the Bucket Overview window.

YAmazon S3 automatically triggers the Lambda function, which


loads the data into a DynamoDB table.

A serverless dashboard application has been provided for you to


view the results.

26. Go to Services and click on CloudFormation.


27. Click on the name of the stack..


28. Select the Outputs tab and click on the URL of the
Dashboard

The dashboard application displays, showing the inventory data


you loaded into the bucket. If your dashboard looks like the
following image, the application was able to retrieve the data from
DynamoDB, which proves that the Lambda function was triggered
successfully.

https://labs.netec.com/pages/lab6.html 8/16
22/7/2021 AWS Labs

Note: If no information is displayed, ask your instructor to help


diagnose the problem.

This dashboard application is served as a static web page from


Amazon S3. The dashboard authenticates via Amazon Cognito as
an anonymous user, which provides sufficient permission for the
dashboard to retrieve data from DynamoDB.

You can also view the data within the DynamoDB table.

29. Return to the AWS Management Console browser tab. On the


Services menu, click DynamoDB.

30. In the left navigation pane, click Tables.


31. Click the name of the Inventory table.


32. Click the Items tab

The data from the inventory file is displayed, showing the Store,
Item, and inventory Count.

TASK 4: CONFIGURING NOTIFICATIONS

Now that you can quickly view the inventory files through the web
application, you wish to notify inventory management staff when a
store runs out of an item. For this serverless notification
functionality, you will use Amazon Simple Notification Service
(Amazon SNS), as shown in the following diagram:

https://labs.netec.com/pages/lab6.html 9/16
22/7/2021 AWS Labs

Amazon SNS is a flexible, fully managed publish/subscribe


(pub/sub) messaging and mobile notification service for the delivery
of messages to subscribing endpoints and clients. With Amazon
SNS, you can fan out messages to a large number of subscribers,
including distributed systems and services, and mobile devices.

33. On the Services menu, click Simple Notification Service.


34. In the left navigation pane, click Topics


35. Click Create topic


36. Select Standard option


37. In the Create topic box, for Name: enter NoStock

38. Click Create topic

To receive notifications you must subscribe to the topic. You can


choose to receive notifications via several methods, such as SMS
and email.

39. On the lower half of the page, click Create subscription and
configure:

Protocol: SMS

Endpoint: Enter your cell phone number in international


format (for example, +14155550100 or +917515550199)

Click Create subscription

Note: Incase if you see an option to add phone number using


Sandbox destination phone numbers as below, then follow the
steps below:

https://labs.netec.com/pages/lab6.html 10/16
22/7/2021 AWS Labs

Click on Add phone number


Use dropdown arrow to select the country. you will se an
option to search the country you want to select here.
Enter the phone number in international format (for example,
+14155550100 or +917515550199).
For Verification message language, select the desired
language in which you want to receive a message (for
example, English (United States) or German (Germany))
click Add phone number
Check your phone an enter the 6 digit verification code.
Click Verify phone number
For Endpoint, select the phone number you just added.
Click Create Subscription

Note If you do not wish to receive an SMS, you can instead


subscribe via email, providing an email address that you can
access from the classroom. After creating an email subscription,
you will receive a confirmation email. Open the email and click the
Confirm subscription link.

Now that the topic and subscription are set up, any message sent
to the SNS topic will be forwarded to you via SMS or email.

TASK 5: CREATING A LAMBDA FUNCTION TO SEND


NOTIFICATIONS

You could modify the existing Load-Inventory Lambda function to


check inventory levels while the file is being loaded, but this is not a
good architectural practice. Rather than overloading the Load-
Inventory function with business logic, create another Lambda function
that is triggered whenever data is loaded into the DynamoDB table.
This function will be triggered by a DynamoDB Stream.

There are several benefits to this architectural approach:

Each Lambda function performs a single, specific function. This


makes the code simpler and more maintainable.

You can add additional business logic by creating additional


Lambda functions. Each function operates independently, so
existing functionality is not impacted.
https://labs.netec.com/pages/lab6.html 11/16
22/7/2021 AWS Labs

In this task, you create another Lambda function that looks at


inventory as it is loaded into the DynamoDB table. If the function
notices that an item is out of stock (the count equals 0), the function
will send a notification via the Amazon SNS topic you created earlier.

The following diagram shows this workflow:

39. On the Services menu, click Lambda.


40. Click Create function and configure:

Select Author from scratch


Name: Check-Stock

Runtime Select Python3.8


Execution Role: Select Using an existing role


Existing Role Select Lambda-Check-Stock-Role


Click Create function

This role has been configured with permissions to send a notification


to Amazon SNS.

41. Click the Code tab.


42. Double-click lambda_function.py.


43. Delete all of the code that appears in the code editor.

44. Copy and paste the following code into the Function code
editor

Copy Code

https://labs.netec.com/pages/lab6.html 12/16
22/7/2021 AWS Labs

# Stock Check Lambda function

# This function is triggered when values are inserted into


the Inventory DynamoDB table.

# Inventory counts are checked, and if an item is out of


stock, a notification is sent to an SNS topic.

import json, boto3

# This handler is run every time the Lambda function is


triggered

def lambda_handler(event, context):

# Show the incoming event in the debug log

print("Event received by Lambda function: " +


json.dumps(event, indent=2))

# For each inventory item added, check if the count is


zero

for record in event['Records']:

newImage = record['dynamodb'].get('NewImage', None)

if newImage:

count = int(record['dynamodb']['NewImage']['Count']
['N'])

if count == 0:

store = record['dynamodb']['NewImage']['Store']
['S']

item = record['dynamodb']['NewImage']['Item']['S']

# Construct message to be sent

message = store + ' is out of stock of ' + item

print(message)

# Connect to SNS

sns = boto3.client('sns')

alertTopic = 'NoStock'

snsTopicArn = [t['TopicArn'] for t in


sns.list_topics()['Topics']

if
t['TopicArn'].lower().endswith(':' + alertTopic.lower())]
[0]

# Send message to SNS

sns.publish(

TopicArn=snsTopicArn,

Message=message,

Subject='Inventory Alert!',

MessageStructure='raw'

# Finished!

return 'Successfully processed {}


records.'.format(len(event['Records']))

Examine the code. It is performing the following steps:

https://labs.netec.com/pages/lab6.html 13/16
22/7/2021 AWS Labs

Loop through the incoming records.


If the inventory count is zero, send a message to the NoStock


SNS topic.

45. Click Deploy

Now, configure the function to be triggered whenever data is added to


the Inventory table in DynamoDB.

46. Scroll up to the top of the page.


47. Click + Add trigger and configure:

Select a trigger: DynamoDB


DynamoDB Table: Inventory


Click Add

You are now ready to test the system.

TASK 6: TESTING THE SYSTEM

You will now upload an inventory to Amazon S3, which will trigger the
original Load-Inventory function. This function will load data into
DynamoDB, which will then trigger the new Check-Stock Lambda
function. If the Lambda function detects an item with zero inventory, it
will send a message to Amazon SNS, which will notify you via SMS or
email.

48. On the Services menu, click S3.


49. Upload a different inventory file.


50. Return to the Inventory Dashboard web browser tab, and


refresh the page.

You should now be able to use the Store dropdown menu to view
inventory from both stores.

You should receive a notification via SMS or email telling you that the
store is out of stock of an item (every inventory file has one item out of
stock).

Note: If you do not receive a notification, please wait a few minutes


and try uploading a different inventory file. The DynamoDB trigger may
take a few minutes to enable. It is possible that some phones might
not receive the message due to anti-spam settings. If so, add an email
subscription to the Amazon SNS topic, which will send you a
confirmation email. Then, open the email and click the Confirm
subscription link. You can then upload another file to test the system.

https://labs.netec.com/pages/lab6.html 14/16
22/7/2021 AWS Labs

51. Try uploading multiple inventory files at the same time. What do
you think will happen?

CLEAN UP RESOURCES

52. Go to Services and select Lambda.


53. Click on the lambda called Check-Stock, select the


Configuration tab in the left side menu and click on Triggers.

54. Select the DynamoDB: Inventory trigger and click Delete and
then Delete.

55. Click Close.


56. In the upper right corner click on Actions then Delete function
finally Delete.

57. In the Functions menu select the function called Load-


Inventory then click on Actions and Delete.

58. Click Delete.


59. Go to Services and select SNS.


60. In the left side menu click on Subscriptions.


61. Select one by one the subscriptions that you have created and
click Delete twice.

62. In the left side menu click on Topics.


63. Select the topic called NoStock, click Delete and type delete
me.

64. Go to Services and select DynamoDB.


65. In the left side menu click on Tables.


66. Select the table called Inventory and the Delete table option.

67. Leave the default options and type idelete then click Close
(Ignore the Unauthorized message).

68. Go to Services and select S3.


69. Select in the circle the name of the bucket Inventory-xxx


70. In the upper panel click on the Empty button


71. In the text box write permanently delete and click on the Empty
button
https://labs.netec.com/pages/lab6.html 15/16
22/7/2021 AWS Labs

72. At the top right click on Exit


73. Select the Inventory-xxx bucket, click Delete


74. Enter the full name of your bucket, example: Inventory-xxx


75. Click on the Delete bucket button

IMPORTANT: Notify your instructor that you have


finished cleaning up the resources.

CONCLUSION

¡Congratulations! You now have successufully:

Created a Lambda function


Triggered a Lambda function from Amazon S3 and DynamoDB
Configured Amazon SNS to send notifications

END LAB

Click to go up

https://labs.netec.com/pages/lab6.html 16/16

You might also like