How to load all json files for `aws_dynamodb_table_item`

956 views
Skip to first unread message

dongla...@gmail.com

unread,
Jun 8, 2018, 10:25:55 AM6/8/18
to Terraform
Hey,

Confused to apply `aws_dynamodb_table_item` to load all json files in some directory which path i can get from tfvars. 

I did the following code to try, and get failed. How to apply `dirname` to get file name list, and pass to a variable with type list?  or other idea?

variable "json_file_name_list" {
  type = "list"
 default= '${dirname(var.path)}'
}

resource "aws_dynamodb_table_item" "init-items" {
 count = "${length(var.json_file_name_list)}"
 table_name = "${var.table_name}"
 hash_key = "key"
 item = <<EOF
${file("${var.path}${element(var.json_file_name_list, count.index)}.json")}
EOF
}



Regards,
Donglai

laxman Singh Rathore

unread,
Jun 9, 2018, 10:59:58 AM6/9/18
to terrafo...@googlegroups.com
Hi,

Use shell script to get all file name in variable and dynamodb restore script as well as you need to use provisioner with terraform i.e. local and remote exe to perform Desired functionality that will work.

Regards,
Laxman S Mandloi


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/9192e2ce-2f18-4d37-ae9b-c07ed2099e96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dongla...@gmail.com

unread,
Jun 11, 2018, 10:39:48 AM6/11/18
to Terraform

Thank you for your replying, and the following is my understand, is that your suggest:


Approach - 1:
in `build.sh`, apply `ls` and regarding script to get all filenames, then execute `TF_VAR_jsonfilelist=['file1.json','file2.json'...] terraform apply` instead of `terraform apply` directly.

Approach - 2: 
apply provisioner in initialized module, something like 
resource "null_resource" "get_file_list" {
   provisioner "local-exec" {
       
command = "ls ${var.data_file_path} > file_json_list"
   }
}
output
"json_file_list" {
value
=<<EOF
$
{file("file_json_list")}
EOF
}

Then, applied variable `json_file_list ` in dynamo restore. 


Regards,
Donglai Zhang
Reply all
Reply to author
Forward
0 new messages