Mahi
Mahi

Reputation: 403

Extract Values from terraform set variable

I am trying to provision aws service catalog product using terraform resource

resource "aws_servicecatalog_provisioned_product" "example" {}

Terraform resource output description

one of the export value of the resource is outputs which is in form of set and i am collecting that into an output variable using below

output "Provisioned_Product_Outputs" {
  value = aws_servicecatalog_provisioned_product.example.outputs
} 

Output Looks Like

Provisioned_Product_Outputs = toset([
  {
    "description" = "Backup plan"
    "key" = "BackupPlan"
    "value" = "light"
  },
  {
    "description" = "Current user zone to run"
    "key" = "CurrentAZ"
    "value" = "primary"
  },
  {
    "description" = "InstanceID of Vm"
    "key" = "EC2InstanceID"
    "value" = "i-04*******"
  },
  {
    "description" = "InstanceHostName"
    "key" = "InstanceHostName"
    "value" = "{\"fqdn\":\"foo.domain.com\"}"
  },
  {
    "description" = "The ARN of the launched Cloudformation Stack"
    "key" = "CloudformationStackARN"
    "value" = "arn:aws:cloudformation:{region}:{AccountID}:stack/SC-{AccountID}-pp-iy******"
  },
])

i would like to have only selected outputs values rather than entire set like below.

output "EC2InstanceID" {
  value = "i-04*******"
} 

output "InstanceHostName" {
  value = ""{\"fqdn\":\"foo.domain.com\"}""
}

output "CloudformationStackARN" {
  value =  "arn:aws:cloudformation:{region}:{AccountID}:stack/SC-{AccountID}-pp-iy******"
}

Is there a way to apply or have some condition which allows me to check for the right values using key value pair and apply the value in the outputs

regards

Upvotes: 0

Views: 2344

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

Since you know that your output is set, you can create a filter on the objects inside the set using contains:

output "outputs" {
  value = {
    for output in aws_servicecatalog_provisioned_product.example.outputs : output.key =>
    output.value if contains(["EC2InstanceID", "InstanceHostName", "CloudformationStackARN"], output.key)
  }
}

The output will be similar to this:

outputs = {
  "CloudformationStackARN" = "arn:aws:cloudformation:{region}:{AccountID}:stack/SC-{AccountID}-pp-iy******"
  "EC2InstanceID" = "i-04*******"
  "InstanceHostName" = "{\"fqdn\":\"foo.domain.com\"}"
}

If you want to have separate outputs, you have to type out each output manually:

output "EC2InstanceID" {
  value = [for output in aws_servicecatalog_provisioned_product.example.outputs : output.value if output.key == "EC2InstanceID"][0]
}

output "InstanceHostName" {
  value = [for output in aws_servicecatalog_provisioned_product.example.outputs : output.value if output.key == "InstanceHostName"][0]
}

output "CloudformationStackARN" {
  value = [for output in aws_servicecatalog_provisioned_product.example.outputs : output.value if output.key == "CloudformationStackARN"][0]
}

You can not have a for_each attribute for outputs. Currently resource and module blocks support for_each attributes.

Upvotes: 2

Related Questions