Override_data with tuple

Hello,

I would like to use override_data in terraform tests.
However I need that the data object was a tuple. How can I achieve this ?

override_data {                                                                                
  target = data.aws_s3_object.data                                              
  values = {                                                                                   
    body = "{\"data\":\"test\",\"description\":\"test\"}"                                      
  }                                                                                            
}
locals {
  decoded_data = jsondecode(data.aws_s3_object.data[0].body)                    
}

Errors:

Error: Invalid index                                                                                                                                                                        
│                                                                                                                                                                                             
│   on locals.tf line 8, in locals:                                                                                                                                                           
│    8:   decoded_data = jsondecode(data.aws_s3_object.data[0].body)                                                                                                                          
│     ├────────────────                                                                                                                                                                       
│     │ data.aws_s3_object.data is empty tuple 

Regards,

Hi @smutel, can you share your configuration for the aws_s3_object data source? It looks like you have defined it with a count attribute that is evaluating to zero, hence the call for data.aws_s3_object.data[0] is returning the error that data.aws_s3_object.data is empty.

Correct …

data "aws_s3_object" "data" {                                                   
  count  = (var.type == "output" && var.aws_account_id != "") || (var.type == "data" && var.namespace != "") ? 1 : 0
  bucket = var.data_bucket                                                      
  key    = local.key                                                            
}

I had to mock in some of the values, but this seems to work for me with your locals definition?

mock_provider "aws" {}

override_data {
  target = data.aws_s3_object.data
  values = {
    body = "{\"data\":\"test\",\"description\":\"test\"}"
  }
}

run "test" {
  assert {
    condition     = local.decoded_data["data"] == "test"
    error_message = "Text matches"
  }
}

If the count attribute resolves to 0, then the resource will not be created even if an override_data block exists for it. I think that’s the case here. You should make sure that (var.type == "output" && var.aws_account_id != "") || (var.type == "data" && var.namespace != "") is evaluating to true.

I changed my local to:

decoded_data = (var.type == "output" && var.aws_account_id != "") || (var.type == "data" && var.namespace != "") ? jsondecode(data.aws_s3_object.data[0].body) : null

Now I am able to test variable like this:

run "output_missing_input" {                                                    
  command = plan                                                                
                                                                                
  variables {                                                                   
    type = "output"                                                             
    key  = "test"                                                               
  }                                                                             
                                                                                
  expect_failures = [                                                           
    null_resource.cluster,                                                      
  ]                                                                             
}

My concern is resolved but I am still curious to know how to do list with override_data