Architecting On AWS - Lab 6 - Implementing A Serverless Architecture With AWS Managed Service
Architecting On AWS - Lab 6 - Implementing A Serverless Architecture With AWS Managed Service
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
SCENARIO
https://labs.netec.com/pages/lab6.html 1/16
22/7/2021 AWS Labs
This triggers a Lambda function, which will read the file and
insert items into a DynamoDB table.
DURATION
START LAB
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
Select the name of the account and the list of labs will appear.
https://labs.netec.com/pages/lab6.html 3/16
22/7/2021 AWS Labs
8. Copy and paste the following code into the Function Code
editor.
Copy Code
s3 = boto3.resource('s3')
dynamodb = boto3.resource('dynamodb')
inventoryTable = dynamodb.Table('Inventory');
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'
try:
s3.meta.client.download_file(bucket, key,
localFilename)
except Exception as e:
print(e)
raise e
rowCount = 0
rowCount += 1
try:
inventoryTable.put_item(
Item={
'Store': row['store'],
'Item': row['item'],
'Count': int(row['count'])})
except Exception as e:
print(e)
# Finished!
6. Click Deploy
https://labs.netec.com/pages/lab6.html 5/16
22/7/2021 AWS Labs
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:
Each bucket must have a unique name, so you will add a random
number to the bucket name. For example: inventory-123
inventory-123
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.
https://labs.netec.com/pages/lab6.html 6/16
22/7/2021 AWS Labs
Load-Inventory
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
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 Show,18
Berlin,Echo Plus,0
Berlin,Echo Look,10
Berlin,Amazon Tap,15
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.)
25. Click Close button on the Upload: status page to return back
to the Bucket Overview window.
28. Select the Outputs tab and click on the URL of the
Dashboard
https://labs.netec.com/pages/lab6.html 8/16
22/7/2021 AWS Labs
You can also view the data within the DynamoDB table.
The data from the inventory file is displayed, showing the Store,
Item, and inventory Count.
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
39. On the lower half of the page, click Create subscription and
configure:
Protocol: SMS
https://labs.netec.com/pages/lab6.html 10/16
22/7/2021 AWS Labs
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.
Name: Check-Stock
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
if newImage:
count = int(record['dynamodb']['NewImage']['Count']
['N'])
if count == 0:
store = record['dynamodb']['NewImage']['Store']
['S']
item = record['dynamodb']['NewImage']['Item']['S']
print(message)
# Connect to SNS
sns = boto3.client('sns')
alertTopic = 'NoStock'
if
t['TopicArn'].lower().endswith(':' + alertTopic.lower())]
[0]
sns.publish(
TopicArn=snsTopicArn,
Message=message,
Subject='Inventory Alert!',
MessageStructure='raw'
# Finished!
https://labs.netec.com/pages/lab6.html 13/16
22/7/2021 AWS Labs
Click Add
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.
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).
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
54. Select the DynamoDB: Inventory trigger and click Delete and
then Delete.
56. In the upper right corner click on Actions then Delete function
finally Delete.
61. Select one by one the subscriptions that you have created and
click Delete twice.
63. Select the topic called NoStock, click Delete and type delete
me.
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).
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
CONCLUSION
END LAB
Click to go up
https://labs.netec.com/pages/lab6.html 16/16