Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - Optional SSM parameter lookup

I'm doing a lookup for an SSM parameter which may or may not exist depending on a variable passed in:

data "aws_ssm_parameter" "server_tags" {
  name  = "/${var.env_number}/server_tags"
}

I am then using it like below in my locals and passing to my module:

locals {
  server_tags = data.aws_ssm_parameter.server_tags != null ? jsondecode(data.aws_ssm_parameter.server_tags.value) : {}
  instance_tags = merge(var.instance_tags, local.server_tags)
}

This works fine when my parameter exists, but if I pass in a value where my parameter doesn't exist, I get an error:

Error describing SSM parameter (/997/server_tags): ParameterNotFound: 

Is there anyway I can do a pre-check to see if the parameter exists or make it optional somehow?

Thanks

like image 306
CallumVass Avatar asked Nov 01 '25 23:11

CallumVass


1 Answers

Sadly you can't do this. There is no way build-on mechanism for TF to check if a data source exists or not. But you can program your own logic for that using External Data Source.

Since you program the external data source, you can create a logic for checking if a resource exists or not.

like image 61
Marcin Avatar answered Nov 04 '25 02:11

Marcin