Populate Dynamodb table items with single Terraform resource from JSON file

Muhammad Badawy
3 min readApr 5, 2022

In this tutorial I will demonstrate how to populate data as Dynamodb table items from JSON file using single Terraform resource and for_each argument.

Usually Terraform provides this resource to apply single dynamodb table item, so if you need to apply multiple items, you will duplicate the resource as how many items you need.

So in this demo, I will introduce an effective way to populate data with single resource and using for_each to loop on the items in the JSON file.

Now let’s jump to the demo.

Prerequisites:

  • Terraform installed
  • AWS environment to run the terraform and apply resources
  • All the files used in this demo can be downloaded from here

Now let’s jump into a demo:

When you download the files from the repo, you will see 3 files as below:
data.json which contains data in JSON format.
locals.tf where we decode the JSON format and define local variables.
main.tf which contains the terraform resources.

Notice in locals.tf file, the JSON file has been read by “file” function, then it has been decoded into…

--

--

Muhammad Badawy

Living in the line between DevOps Engineering Enthusiasm and Arabic Calligraphy as a passion. LinkedIn: https://www.linkedin.com/in/muhammad-badawy-linked1n/