Getting Started With AWS Free Tier And Connect DynamoDB With .NET

We will see all the steps for creating an AWS Free Tier account (for one year) and we will create an Amazon DynamoDB table from a .NET WinForms application. Later, we will insert some records to this DynamoDB table and display all the records in a Data Grid View control.

In this article, we will see all the steps for creating an AWS Free Tier account (for one year) and we will create an Amazon DynamoDB table from a .NET WinForms application. Later, we will insert some records to this DynamoDB table and display all the records in a Data Grid View control.

Step 1 - Create a free AWS account for one year.
 
AWS Marketplace offers free and paid software products that run on the AWS Free Tier. If you qualify for the AWS Free Tier, you can use many products.

Please visit this URL to create a free account.

Create a Free AWS account for one year 
 
Please give your email address and choose a password for your AWS account. Also, give a valid AWS account name. Click the “Continue” button to proceed. Give your contact information. Please give a valid phone number because it will be validating in the debit/credit card validation time.
 
Create a Free AWS account for one year
 
Here, you must give the Credit/Debit card information. Please give the valid information.
 
Create a Free AWS account for one year
 
You will be charged Rs. 2 from your bank account for this validation process. After some days, this amount will be credited to your bank account.
 
Create a Free AWS account for one year
Please provide your phone number. You will get a call to this number for validating your account.
 
Create a Free AWS account for one year
 
You can choose a plan from the below screen. Opt for the free plan. If needed, you can choose other plans also.
 
Create a Free AWS account for one year
 
The next screen will be personalized to your experience. Please choose the suitable details.
 
Create a Free AWS account for one year
 
 
Our Sign-Up process will be completed now.
 
Create a Free AWS account for one year 
 
You can click the “Complete Sign Up” button.
 
Create a Free AWS account for one year
 
If you log on to your account, you will be notified with the below message. You can wait for some more time and your account will be activated shortly.
 
Create a Free AWS account for one year 
 
After some time, you will get an email to your registered email address from Amazon.
 
Create a Free AWS account for one year
 
Congratulations, your AWS free tier account is now activated.
 
You can sign into your account console now.
 
Create a Free AWS account for one year
 
You can see all the services available with AWS. Please note that all these services are not free. But you can use many free services.
 
Currently, there are 15 geographic locations available in AWS. Please choose any of these locations. I chose Asia Pacific (Mumbai) location.
Create a Free AWS account for one year

Step 2 - Please create Access Keys.

We must create access keys for connecting any of the AWS services from outside AWS. We are going to create an Amazon DynamoDB from the .NET application we need the access key to do this.

Please click “My Security Credentials” option under your username.

create Access Keys
 
It will open another screen. Please expand the “Access keys”.

create Access Keys
Please click “Create New Access Key” button. It will create a new access key for you. Often, you should change your access keys for security reasons.
 
create Access Keys 
 
Your access key is created now. You can download the access key and secret key. Please save these keys carefully. We will use these keys in our .NET application soon.
 
create Access Keys
 
Step 3 - Create a .NET WinForms application to create DynamoDB

We are going to create a WinForms application. We will create a Student model with Student Id, Student Name, College Name, and Class Name. We will use this application to insert the Student data to DynamoDB and display Student records in a Grid. I am using Visual Studio 2017 to create a WinForms Application.

Please add “AWSSDK.DynamoDBv2” NuGet Package to your WinForms application. This library is used to create DynamoDB database and used for all CRUD actions.
 
Create a .NET WinForms application to create DynamoDB
 
We can add two buttons to our main form - one button for inserting the student record and another button for displaying the existing records from DynamoDB.
 
Create a .NET WinForms application to create DynamoDB
 
I have added a panel control and added three text boxes to get user input for Student data. Please note that Student Id will be created automatically (We use GUID for this).
 
Create a .NET WinForms application to create DynamoDB
 
We can add one more panel control and add a data grid view control to display our student data.
 
Create a .NET WinForms application to create DynamoDB

Our design part is over now.

We can add a Student model class now. Please create a “Models” folder and create Student class inside this folder. 

Student.cs
  1. using Amazon.DynamoDBv2.DataModel;  
  2.   
  3. namespace AmazonDynamoDB.Models  
  4. {  
  5.     [DynamoDBTable("Student")]  
  6.     public class Student  
  7.     {  
  8.         public string studentId { getset; }  
  9.         public string studentName { getset; }  
  10.         public string collegeName { getset; }  
  11.         public string className { getset; }  
  12.         public int isActive { getset; }   
  13.     }  
  14. }  

I have imported “Amazon.DynamoDBv2.DataModel” library in this class.

We can add a “CreateTable” method now. We will call this method inside the Form_Load event.

CreateTablemethod
  1. private void CreateTable()  
  2.         {  
  3.             var credentials = new BasicAWSCredentials(accessKey, secretKey);  
  4.             client = new AmazonDynamoDBClient(credentials, RegionEndpoint.APSouth1);  
  5.   
  6.             var tableResponse = client.ListTables();  
  7.             if (!tableResponse.TableNames.Contains(tableName))  
  8.             {  
  9.                 MessageBox.Show("Table not found, creating table => " + tableName);  
  10.                 client.CreateTable(new CreateTableRequest  
  11.                 {  
  12.                     TableName = tableName,  
  13.                     ProvisionedThroughput = new ProvisionedThroughput  
  14.                     {  
  15.                         ReadCapacityUnits = 3,  
  16.                         WriteCapacityUnits = 1  
  17.                     },  
  18.                     KeySchema = new List<KeySchemaElement>  
  19.                     {  
  20.                         new KeySchemaElement  
  21.                         {  
  22.                             AttributeName = hashKey,  
  23.                             KeyType = KeyType.HASH  
  24.                         }  
  25.                     },  
  26.                     AttributeDefinitions = new List<AttributeDefinition>  
  27.                     {  
  28.                         new AttributeDefinition { AttributeName = hashKey, AttributeType=ScalarAttributeType.S }  
  29.                     }  
  30.                 });  
  31.   
  32.                 bool isTableAvailable = false;  
  33.                 while (!isTableAvailable)  
  34.                 {  
  35.                     Console.WriteLine("Waiting for table to be active...");  
  36.                     Thread.Sleep(5000);  
  37.                     var tableStatus = client.DescribeTable(tableName);  
  38.                     isTableAvailable = tableStatus.Table.TableStatus == "ACTIVE";  
  39.                 }  
  40.                 MessageBox.Show("DynamoDB Table Created Successfully!");  
  41.             }  
  42.         }  

BtnInsert_Click event

  1. private void BtnInsert_Click(object sender, EventArgs e)  
  2.         {  
  3.             TxtStudentName.Text = string.Empty;  
  4.             TxtCollegeName.Text = string.Empty;  
  5.             TxtClassName.Text = string.Empty;  
  6.             PnlInsert.Visible = true;  
  7.         }  

BtnSave_Click event

  1. private void BtnSave_Click(object sender, EventArgs e)  
  2.         {  
  3.             //Set a local DB context  
  4.             context = new DynamoDBContext(client);  
  5.   
  6.             //Create an Student object to save  
  7.             Student currentState = new Student  
  8.             {  
  9.                 studentId = Guid.NewGuid().ToString(),  
  10.                 studentName = TxtStudentName.Text,  
  11.                 collegeName = TxtCollegeName.Text,  
  12.                 className = TxtClassName.Text,  
  13.                 isActive = 1  
  14.             };  
  15.   
  16.             //Save an Student object  
  17.             context.Save<Student>(currentState);  
  18.   
  19.             MessageBox.Show("Student Record Inserted Successfully!");  
  20.   
  21.             PnlInsert.Visible = false;  
  22.         }  
BtnDisplay_Click event
  1. private void BtnDisplay_Click(object sender, EventArgs e)  
  2.         {  
  3.             //Set a local DB context  
  4.             context = new DynamoDBContext(client);  
  5.   
  6.             Table StudentTable = Table.LoadTable(client, tableName);  
  7.   
  8.             ScanFilter scanFilter = new ScanFilter();  
  9.             scanFilter.AddCondition("isActive", ScanOperator.Equal, 1);  
  10.   
  11.             Search search = StudentTable.Scan(scanFilter);  
  12.   
  13.             List<Document> documentList = new List<Document>();  
  14.   
  15.             DGV.Rows.Clear();  
  16.             DGV.ColumnCount = 4;  
  17.   
  18.             DGV.Columns[0].Width = 270;  
  19.   
  20.             DataGridViewRow row = new DataGridViewRow();  
  21.             row.CreateCells(DGV);  
  22.             row.Cells[0].Value = "Student Id";  
  23.             row.Cells[1].Value = "Student Name";  
  24.             row.Cells[2].Value = "College Name";  
  25.             row.Cells[3].Value = "Class Name";  
  26.             row.DefaultCellStyle.BackColor = Color.Blue;  
  27.             row.DefaultCellStyle.ForeColor = Color.White;  
  28.             DGV.Rows.Add(row);  
  29.             do  
  30.             {  
  31.                 documentList = search.GetNextSet();  
  32.                 foreach (var document in documentList)  
  33.                 {  
  34.                     row = new DataGridViewRow();  
  35.                     row.CreateCells(DGV);  
  36.                     foreach (var attribute in document.GetAttributeNames())  
  37.                     {  
  38.                         string stringValue = null;  
  39.                         var value = document[attribute];  
  40.                         if (value is Primitive)  
  41.                             stringValue = value.AsPrimitive().Value.ToString();  
  42.                         else if (value is PrimitiveList)  
  43.                             stringValue = string.Join(",", (from primitive  
  44.                                             in value.AsPrimitiveList().Entries  
  45.   
  46.                                                             select primitive.Value).ToArray());  
  47.                         if (attribute == "studentId")  
  48.                         {  
  49.                             row.Cells[0].Value = stringValue;  
  50.                         }  
  51.                         else if (attribute == "studentName")  
  52.                         {  
  53.                             row.Cells[1].Value = stringValue;  
  54.                         }  
  55.                         else if (attribute == "collegeName")  
  56.                         {  
  57.                             row.Cells[2].Value = stringValue;  
  58.                         }  
  59.                         else if (attribute == "className")  
  60.                         {  
  61.                             row.Cells[3].Value = stringValue;  
  62.                         }  
  63.                     }  
  64.                     DGV.Rows.Add(row);  
  65.   
  66.   
  67.                 }  
  68.             } while (!search.IsDone);  
  69.   
  70.             foreach (DataGridViewColumn c in DGV.Columns)  
  71.             {  
  72.                 c.DefaultCellStyle.Font = new Font("Arial", 12F, GraphicsUnit.Pixel);  
  73.             }  
  74.   
  75.             PnlGrid.Visible = true;  
  76.         }  

We have completed all the event methods inside our form. We can run our application.

We have called the DynamoDB Table creation method inside the Form Load event. Our new DynamoDB table will be created shortly.
 
Create a .NET WinForms application to create DynamoDB 
 
We can insert a Student record now.
 
Create a .NET WinForms application to create DynamoDB 

After clicking the “Save” button, our new Student record will be inserted to the DynamoDB table.

We can verify this data in AWS console. Please come to AWS console and open DynamoDB service.

Create a .NET WinForms application to create DynamoDB 

You can see the newly created Student table there. We have used a “studentid” key as a partition key. This partition key can be used for all the searching operations.

If you click on the "Student", it will open the table details. Please click the “Items” tab. It will show all the records. Currently, there is only one record inside the table.

Create a .NET WinForms application to create DynamoDB
 
If you double-click the studentid, it will open entire row information.
 
Create a .NET WinForms application to create DynamoDB
We can insert one more student record from our WinForms application. Now, we can display these two records by clicking “Display Students” button.
Create a .NET WinForms application to create DynamoDB
 
In this article, we saw the steps for creating a free AWS account (one year) and after that, we created a DynamoDB table from our WinForms application. We successfully inserted two student records to DynamoDB table and we displayed those records in a Data Grid View control also. In my upcoming articles, we can see entire CRUD actions with DynamoDB and see more AWS services.