Nerd For Tech
Published in

Nerd For Tech

Python FastAPI with AWS DynamoDB

Leveraging FastAPI Web App Using NoSQL-based Database AWS DynamoDB

In this article, we’ll move from using local DynamoDB to using the cloud version. In general, there is nothing much to change within our code. But one thing is certain that we need to change the hardcoded database’s configurations. Obviously, you don’t want to hardcode sensitive data.

Note that this article is the second article of FastAPI and AWS DynamoDB series. If you haven’t seen the first article, you can go here:

https://medium.com/nerd-for-tech/introduction-to-fastapi-and-local-dynamodb-595c990ed0f8

Setting Up Access Keys

Before changing the database configurations, we need to have an access key.

First, log in to your AWS account. Click the top right corner that contains your AWS account name. Then choose “Security credentials”.

After that, you’ll be redirected to IAM console management. Below AWS IAM credentials tab, you’ll see a button titled “Create access key”. Click it and make sure that you download the CSV file that contains your access key id and secret access key.

In case you lost your access key id and secret access key, you can deactivate and delete the old one, then create a new one.

Up to this point, we have the access key. Now, let’s create a DynamoDB table.

Create DynamoDB Table

For this example, we’ll keep things simple by choosing default settings.

Then click “Create table”.

Updating Code

Create .env file inside project’s root directory to store our credentials. Change these values into the generated access key we have earlier and specify in which region you created a DynamoDB table.

Now, change our database configurations inside db.py file.

In lines 7 and 9, we store project’s root directory that contains .env file as base_dir then load it.

Now, let’s test our application. Go to http://localhost:5000/docs to access our API documentation.

You can create a recipe using documentation’s example then get all recipes.

Conclusion

In the previous article, we focused on building the application and using local DynamoDB for development. After you’ve passed the development process, you want to deploy your application, but for certain reasons you should use the cloud version instead of the local one. There are AWS DynamoDB’s functionalities that will help you optimize your application and take your burden of managing servers.

You can get the full code for this article here https://github.com/agusrichard/aws-workbook/tree/master/fastapi-dynamo

Thank you for reading and happy coding!

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store