AWS Serverless Web App - Step #5 - Boto3

Опубликовано: 21 Июнь 2026
на канале: Mr. Coxall
178
like

We will once again revisit Lambda functions. Now that we have a database with information in it, we can create a Lambda function that will retrieve a row (or several rows). As part of AWS, there is a Python library called Boto3. This library allows you to access any AWS service using Python code. We will use this library to access the DynamoDB, get back a JSON file, and then return it from Lambda.

There will be just one problem when we get back the data from DynamoDB. We created a row that was "age" and holds a "decimal" value type. It turns out that Boto3 returns this type of value in a strange format, that if we just returned it from lambda as JSON we would get an error. To prevent this, we will include a helper function that will re-format our decimal number value into something we can use.

Tasks:

create a new Lambda function
add in the Boto3 library to access the database
query the database to return a row of information
return the row in JSON format from the lambda function
we will pull out the "Item" element from the returned data since that is what we are interested in
if no row was returned, then we need to catch this case and return an empty return statement
if there is a row returned, we need to re-format the "decimal" type, so that we do not get an error. We will once again revisit Lambda functions. Now that we have a database with information in it, we can create a Lambda function that will retrieve a row (or several rows). As part of AWS, there is a Python library called Boto3. This library allows you to access any AWS service using Python code. We will use this library to access the DynamoDB, get back a JSON file and then return it from Lambda.

There will be just one problem when we get back the data from DynamoDB. We created a row that was "age" and holds a "decimal" value type. It turns out that Boto3 returns this type of value in a strange format, that if we just returned it from lambda as JSON we would get an error. To prevent this, we will include a helper function that will re-format our decimal number value into something we can use.

Tasks:

create a new Lambda function
add in the Boto3 library to access the database
query the database to return a row of information
return the row in JSON format from the lambda function
we will pull out the "Item" element from the returned data since that is what we are interested in
if no row was returned, then we need to catch this case and return an empty return statement
if there is a row returned, we need to re-format the "decimal" type, so that we do not get an error