0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Ervin, Thanks for you kind help on the question this will certainly bring me closure the expected output. Can this be use to have each value a separate output rather than single block. Like one i have pasted in required output ``` output "EC2InstanceID" { value = "i-04*******" } ```
You can not have a for_each for an output. You will have to create a separate output for each key-value, this can not be done conditionally.
Hi Ervin, you mean to say like below ``` output "EC2InstanceID" { value = { for output in aws_servicecatalog_provisioned_product.example.outputs : output.key => output.value if contains(["EC2InstanceID"], output.key) } } ```
I meant the for_each meta argument.
Hi Ervin, thanks very much but could you please share an example with for_each as i am new to terraform i am not able to understand the context here . I am referring to "You can not have a for_each for an output. You will have to create a separate output for each key-value, this can not be done conditionally."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.