Convert tuple to string in output.tf

My resource from a module is getting created based on create variable like so:

resource "aws_api_gateway_rest_api" "ui_apigw_regional" {
  count                        = var.create == true ? 1 : 0

I need the output value of it to be a string like this:

 "comp_rest_api_id": {
            "sensitive": false,
            "type": "string",
            "value": "8347123a"
        }

Currently, it is coming like this:

 "comp_rest_api_id": {
            "sensitive": false,
            "type": [
                "tuple",
                [
                    "string"
                ]
            ],
            "value": [
                "8347123a"
            ]
        },

I assumed that referring to the first index would extract it as a string, but it is not so it seems. What can I change here to get the output value as a string?

output "rest_api_id" {
  description = "The ids of the user pool clients"
  value       =  var.create == true ? aws_api_gateway_rest_api.ui_apigw_regional[0].id : null
}

I have tried the one function as well as mentioned here : Terraform outputs with count.index - #2 by apparentlymart

output "rest_api_id" {
  description = "The ids of the user pool clients"
  value       =  one(aws_api_gateway_rest_api.ui_apigw_regional[*].id)
}
output "comp_rest_api_id" {
  value = module.comp.rest_api_id
}

But it also returns a list.

Hi @devang.sanghani,

I would’ve expected the final example you shared, using the one function, to work as you intended.

Did you run terraform apply and apply the plan after making that change? The output values reported by terraform output only get updated in the Terraform state once the plan is applied, so if you have not applied the change then you may be referring to the result of the previous attempt.

Another way to test this, if you don’t want to apply the change yet, would be to run terraform console and then enter this expression at the interactive prompt:

module.comp.rest_api_id

This should cause Terraform to return the same value that would get assigned to this output value, since that expression exactly matches the value argument in your output "comp_rest_api_id" block.

1 Like

I did miss this. After TF apply, it is working as expected. Thanks for the reply and this reminder above! :slight_smile: