In this lecture,
we will see how to create and manage DynamoDB tables using terraform.
To create DynamoDB tables using terraform,
we can make use of the AWS DynamoDB table resource type.
Using the same example as before,
let's create a table that will store information about cars.
This resource block expects a name, which is the name of the DynamoDB table
and the hash key, which denotes the primary key for the table.
Besides these two arguments, we must also make use of the argument called attribute,
which is used to store attributes of the table
such as the manufacturer, model name, et cetera.
If you look at the syntax,
you will note that the attribute is defined as another block within the resource block.
You can add as many attributes as needed within the attribute block,
but the primary key is mandatory.
In our example, this is the vehicle identification number or VIN of type string,
which is denoted by the upper case letter S.
Also note that we have added another argument called billing mode.
This argument controls how we are charged for read and writes to this table
and how to manage capacity.
As we saw in the demo, this is set to provisioned by default.
In which case we have to declare the read and write capacity units.
In this example, we have set it to the mode PAY-PER-REQUEST or the on-demand mode.
We are now ready to run our plan and then apply on this configuration
to create the DynamoDB table called cars.
The next step is to add items to the table that we have just created.
To insert new items into the table,
we can make use of another resource type called AWS DynamoDB table item.
This resource expects three required arguments,
the table name and the hash key,
both of which can be specified using reference expressions
from the DynamoDB table resource.
The next argument is the item.
For item, we can make use of the heredoc syntax using EOF as the delimiter,
and then we move one of the example documents under the item argument like this.
However, the item argument expects the value to be in a JSON representation
with the type and value split like this.
S for string and N for number.
Only the hash key attribute, which is the VIN in this case, is required.
The rest are optional.
Running the terraform apply now will insert the item into the DynamoDB table called cars.
Before we move on, please note that this method is not meant to be used
for managing large amounts of data in the table.
This is a simplified example of creating a table and adding items to it.
Now, let's head over to the hands-on labs
and practice working with DynamoDB tables in terraform.