Terraform: How can I read a value from a json file? - terraform

I have the json file (map.json):
[
{
"QueueName": "queue1",
"TypeName": "type1"
},
{
"QueueName": "queue2",
"TypeName": "type2"
},
{
"QueueName": "queue3",
"TypeName": "type2"
}
]
which I can load into the following variable:
locals {
maps = jsondecode(file("maps.json"))
}
How can I read the TypeName value for QueueName = "queue2"?

I think you are looking at something like this:
type = [for el in local.maps : el["TypeName"] if el["QueueName"] == "queue2"]
Output
$ terraform console
> local.response
[
"type1",
]
Logically this will return a list of elements, but if you want to retrieve only the first result, then you can use:
response = [for el in local.maps : el["TypeName"] if el["QueueName"] == "queue1"]
type = length(local.response) > 0 ? local.response[0] : ""
Outputs
$ terraform console
> local.type
"type1"
>
Or just:
type = local.maps[index(local.maps.*.QueueName, "queue1")]["TypeName"]
But this will throw an exception if the element queueName does not exist, something like this:
Call to function "index" failed: item not found

Related

Loading values dynamically from Terraform into a map

I'm trying to load some external data from a json file into Terraform to merge into an appSettings map
It's loading it in as a tuple - and no matter what conversion I do, I can't get a map out of it:
Call to function "merge" failed: arguments must be maps or objects, got "tuple".
Json File
[
{
"appCode": "value",
"containerName": "value",
"databaseName": "value",
"referer": "bvalue",
"shortCode": "value",
"user": "value"
},
{
"appCode": "value",
"containerName": "value",
"databaseName": "value",
"referer": "value",
"shortCode": "value",
"user": "value"
}
]
Locals:
customerSettings = jsondecode(file("vars/${var.environment}.json"))
customerAppSettingsFromJson = {
for index, externalCustomer in local.customerSettings :
externalCustomer => {
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__AppCode" = "${externalCustomer.appCode}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__ContainerName" = "${externalCustomer.containerName}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__DatabaseName" = "${externalCustomer.databaseName}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__Referer" = "${externalCustomer.databaseName}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__ShortCode" = "${externalCustomer.shortCode}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__User" = "${externalCustomer.user}"
}
}
Main.tf appSettings block, has inferred ones, ones from vars and ones from json
app_settings = merge({}, var.app_settings, local.customerAppSettingsFromJson)
You were very close to the solution, but here is how to convert the list of objects into a map:
customerSettings = jsondecode(file("vars/${var.environment}.json"))
customerAppSettingsFromJson = {
for index, externalCustomer in local.customerSettings :
index => {
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__AppCode" = "${externalCustomer.appCode}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__ContainerName" = "${externalCustomer.containerName}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__DatabaseName" = "${externalCustomer.databaseName}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__Referer" = "${externalCustomer.databaseName}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__ShortCode" = "${externalCustomer.shortCode}"
"DynamicCosmosDbSettings__CosmosHostSettings__${index}__User" = "${externalCustomer.user}"
}
}

Appending values to a JSON key with terraform?

I am quite new to terraform. I have a provider that will accept JSON as input.
I have stored JSON config files in my project folder, eg:
{
"id": 58187729,
"name": "My dashboard",
"tags": ["mytag1", "mytag2"]
}
and load them into the resource with the below code:
resource "datadog_monitor_json" "monitor_json" {
for_each = fileset(path.module, "/monitors/*.json")
monitor = file("${path.module}/${each.key}")
}
Is there anyway I can easily append to the "tags" key or will the entire JSON need parsing somehow?
Thanks.
Here is an example on how to add extra tags:
locals {
example = jsondecode(file("${path.module}/myfile.json"))
with_extra_tags = merge(local.example,
{tags = concat(
local.example["tags"],["mytag4", "mytag3"])})
}
gives:
test = {
"id" = 58187729
"name" = "My dashboard"
"tags" = [
"mytag1",
"mytag2",
"mytag4",
"mytag3",
]
}

Terraform - How to convert lists into map (How to fetch AMI tags using terraform)

I am trying to fetch the tags of AMI using AWS CLI and want to reuse the values from the output.
I have a terraform code below which is returning outputs in string format(Maybe not sure of format) which I want to convert into a map object.
variable "ami" {
default = "ami-xxxx"
}
locals {
tags = {
"platform" = lookup(data.local_file.read_tags.content, "platform", "") #Expecting to get platform from Map of read_tags
}
}
data "template_file" "log_name" {
template = "${path.module}/output.log"
}
resource "null_resource" "ami_tags" {
provisioner "local-exec" {
command = "aws ec2 describe-tags --filters Name=resource-id,Values=${var.ami} --query Tags[*].[Key,Value] > ${data.template_file.log_name.rendered}"
}
}
data "local_file" "read_tags" {
filename = "${data.template_file.log_name.rendered}"
depends_on = ["null_resource.ami_tags"]
}
output "tags" {
value = local.tags
}
output "cli-output-tags" {
value = "${concat(data.local_file.read_tags.content)}"
}
output of cli-output-tags is below:
[
[
"ENV",
"DEV"
],
[
"Name",
"Base-AMI"
],
[
"platform",
"Linux"
]
]
How can I convert this output into Map as below using terraform/(jq command), or is there any other way to fetch required values directly from cli-output-tags output:
{
ENV = "DEV",
Name = "Base-AMI",
platform = "Linux"
}
I have also tried changing the CLI command a bit like below but still not able to fetch values as expected:
'Tags[].{Key:Key,Value:Value}'
Resulted below output:
[
{
"Key": "ENV",
"Value": "DEV"
},
{
"Key": "Name",
"Value": "Base-AMI"
},
{
"Key": "platform",
"Value": "Linux"
}
]
You could use zipmap:
output "cli-output-tags" {
value = zipmap(
jsondecode(data.local_file.read_tags.content)[*][0],
jsondecode(data.local_file.read_tags.content)[*][1]
)
}
The code first changes string data from your file to json, then
gets all first elements [*][0] (same for second elements [*][1]), and zips them into map.
How can I convert this output into Map as below
One way would be to use jq as follows (assuming cli-output-tags is the name of the file holding the JSON array of arrays):
jq -r -f '"{", (.[] | "\(.[0]) = \"\(.[1])\""), "}"' cli-output-tags

Is it possible to perform nested iterations in HCL resulting in a flat list without calling flatten?

Is it possible with HCL to have nested iterations returning a flat list(map) without resorting to flatten?
I have this:
locals {
mappings = flatten([
for record_type in var.record_types : [
for host in var.hosts : {
type = record_type,
host = host
}
]
])
}
I would like to remove the need for flatten like this:
locals {
mappings = [
for record_type in var.record_types :
for host in var.hosts : {
type = record_type,
host = host
}
]
}
But it seems like each for .. in must return data.
One alternative I could think of to only have a single for-loop is using setproduct():
variable "record_types" {
default = ["type1", "type2"]
}
variable "hosts" {
default = ["host1", "host2"]
}
locals {
mappings = [
for i in setproduct(var.record_types, var.hosts) : {
type = i[0],
host = i[1],
}
]
}
output "mappings" {
value = local.mappings
}
after terraform apply resulting in:
Outputs:
mappings = [
{
"host" = "host1"
"type" = "type1"
},
{
"host" = "host2"
"type" = "type1"
},
{
"host" = "host1"
"type" = "type2"
},
{
"host" = "host2"
"type" = "type2"
},
]
Of course, the two variables need to be independent sets here.
If you want to support duplicates or have dependent inputs, flatten() with two loops is the way.

Using a list variable for ECS task in container_definitions with terraform

In terraform I am attempting to pass a variable (list) to a module that we built. This variable needs to be used within a aws_ecs_task_definition resource in the container_definitions.
Right now I am just starting with an empty default list defined as a variable:
variable "task_enviornment" {
type = "list"
default = []
}
My ECS task definition looks like this:
resource "aws_ecs_task_definition" "ecs_task_definition" {
family = "${var.ecs_family}"
network_mode = "awsvpc"
task_role_arn = "${aws_iam_role.iam_role.arn}"
execution_role_arn = "${data.aws_iam_role.iam_ecs_task_execution_role.arn}"
requires_compatibilities = ["FARGATE"]
cpu = "${var.fargate_cpu}"
memory = "${var.fargate_memory}"
container_definitions =<<DEFINITION
[
{
"cpu": ${var.fargate_cpu},
"image": "${var.app_image}",
"memory": ${var.fargate_memory},
"name": "OURNAME",
"networkMode": "awsvpc",
"environment": "${jsonencode(var.task_enviornment)}",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "demo"
}
},
"portMappings": [
{
"containerPort": ${var.app_port},
"hostPort": ${var.app_port}
}
]
}
]
DEFINITION
}
The part I am having a problem with with the "environment" part:
"environment": "${jsonencode(var.task_enviornment)}",
I have tried a few different ways to get this to work.
If I do "environment": "${jsonencode(var.task_enviornment)}",
I get ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type []*ecs.KeyValuePair
If I do "environment": "${var.task_enviornment}", or "environment": ["${var.task_enviornment}"],
I get At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 8 is TypeList) in:
Then it just outputs the contents of container_definitions
I did also try adding default values and I was getting similar error messages. However I do need to be able to handle no values being sent in, so an empty list.
variable "task_enviornment" {
type = "list"
default = [
{
"name" = "BUCKET",
"value" = "test"
}
]
}
After a lot of investigation and a fresh set of eyes looking at this figured out the solution. I am unsure why this fixes it, and I feel like this is likely a bug.
Needed to do 2 things to fix this.
Remove type = "list" from the variable definition.
variable "task_environment" {
default = []
}
Remove the quotes when using the variable:
"environment": ${jsonencode(var.task_environment)},
The below solution should work
in variable.tf
variable "app_environments_vars" {
type = list(map(string))
default = []
description = "environment variable needed by the application"
default = [
{
"name" = "BUCKET",
"value" = "test"
},{
"name" = "BUCKET1",
"value" = "test1"
}]
and in your task definition, you can use ${jsonencode(var.app_environments_vars)} similar to
container_definitions =<<DEFINITION
[
{
"cpu": ${var.fargate_cpu},
"image": "${var.app_image}",
"memory": ${var.fargate_memory},
"name": "OURNAME",
"networkMode": "awsvpc",
"environment": ${jsonencode(var.app_environments_vars)},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "${aws_cloudwatch_log_group.fargate-logs.name}",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "demo"
}
},
"portMappings": [
{
"containerPort": ${var.app_port},
"hostPort": ${var.app_port}
}
]
}
]
My guess is that you are trying to use the type "map" instead of lists, as showed above, the removal from type specification will work.
Example:
List_sample = [1,2,3]
Map_sample = { key_name = "value" }
Reference: Terraform - Type Constraints

Resources