How To Create A DynamoDB Table using Python in Cloud9

Reading Time: 5 minutes

Hello Readers ! In this blog we will see How To Create A DynamoDB Table using Python. You have seen how you can do Creating A DynamoDB Table using The AWS CLI. But in this article you will see how you can do this by using Python .

So let’s get Start

Introduction

Cloud9

AWS Cloud9 is an integrated development environment, or IDE. It provides a rich code editing experience with support for multiple programming languages ​​and runtime debuggers, as well as an integrated terminal.

It contains a collection of tools you use to code, build, run, test, and debug software, and helps you publish it to the cloud. You are accessing the AWS Cloud9 IDE through a web browser. You can configure the IDE according to your preferences.

Prerequisites

  • AWS account.
  • Cloud9 IDE.
  • Command Line Terminal with Boto3 installed.

Steps

Step 1: Cloud9 Environment Setup

Setting up our cloud9 environment is the initial stage in this . Go to the search bar in the AWS console and enter “Cloud9” there. In the “Developer Tools” section under services, you may also find Cloud9.

When the Cloud9 is visible, click “Create Environments.”

For the sake of our project, we’re going to accept the defaults settings for the rest of our options and proceed to “Create Environments”.

When you click on Create Environment , you will see the screen as :

Once your environment has been spin up, you should have the screen below.

Step 2: Boto3 Installation on CLI

On this step we will be installing Boto3 on our command line. As stated, a few paragraphs above Boto3 is Python’s SDK (software development kit). In short it allows us to generate python commands on our terminal.

To install Boto3 enter the following command:

python3 -m pip install --user boto3
Step 3: Table creation

The source code need for the creation of our table can be found by clicking on the following link.

Edits would have to be made to the source in order to configure it to your liking and purpose. Once your edits have been made click on the “+” symbol in your IDE, create it a new file, and paste your code in it.

A few things to bear in mind would be to associate your table with the region you are located in, as well as to append the “.py” at the end of your filename in order for Cloud9 to be able to read the python scripts in your file.

Below is the code for our MyCarTable table.

import boto3

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.create_table(

    TableName='MyCarTable',

    KeySchema=[

        {

            'AttributeName': 'brand',

            'KeyType': 'HASH'  #Partition key

        },

        {

            'AttributeName': 'model',

            'KeyType': 'RANGE'  #Sort key

        }

    ],


    AttributeDefinitions=[
        {
            'AttributeName': 'brand',

            'AttributeType': 'S'

        },

        {
            'AttributeName': 'model',

            'AttributeType': 'S'

        },

    ],

    ProvisionedThroughput={

        'ReadCapacityUnits': 10,

        'WriteCapacityUnits': 10

    }

)

print("Table status:", table.table_status)

The table can be create by pressing the “run” button locate at the top of our IDE or by entering the following command:

python3 <your file_name>.py 

See image below:

We can double check our table has been create in two different ways:

1. In the AWS management console, click on “Database”, select “DynamoDB”, and click on “Tables” to view our tables. Or simply by typing “DynamoDB” in the search bar.

2. In the terminal section of our IDE by entering the following command:

aws dynamodb list-tables

Step 4: Table Data Addition

On this step we will be adding items to our table. Now in order for us to do so.

  1. Follow the steps from “Step 3 ” in order to create a new file. With the only difference being that this file will be save as .json file.

2. Now that we have data, we will need to create a python script that would write the data into our table. the source of load.py can be found by clicking on the link.

Similarly, to “Step 3” once your edits have been configure, click on the “+” symbol in your IDE, create it a new file, and paste your code in it. Then save as “.py”file.

Below is our script that will insert data into our table

import boto3

import json

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('MyCarTable')

with open("mydreamcar.json") as json_file:

    dreamcardata = json.load(json_file)

    print(dreamcardata)

    for dreamcar in dreamcardata:

        brand = dreamcar['brand']

        model = dreamcar['model']

        color = dreamcar['color']

        print(type(dreamcar))

        print("Adding car:", brand, model,color)

        table.put_item(

           Item =  dreamcar) 

3. Data can be insert in our table by pressing the “run” button located at the top of our IDE or by entering the following command:

python <your file_name>.py

As previously stated, we can double check our work via the management console.

Step 5: Table Deletion

Last but not least follow the same steps above to create a delete.table.py file. Once your file has been create, the table can now be delete by entering the command below:

python <filename>.py

and You can check in the console as:

Yes, Successfully Done !!!

Conclusion

So, In this Blog We have see that How To Create A DynamoDB Table using Python . I hope you all have enjoyed working with this and learn something new and useful . See you in the next learning platform .Till them enjoy working with this .

Thank You !!!

Happy Learning !!!

Reference

Click Here.

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading