We ended up importing all of our certs into ACM. But independent of that, we use the
technique with conditional inclusion based on directories. For example,
" is included in production. ("
) copies all the relevant files to a working directory used by Terraform. Then we have the following files:
data "aws_acm_certificate" "domain1_wildcard" {
}
data "aws_acm_certificate" "domain2" {
}
data "null_data_source" "cert_arn" {
inputs = {
service1 = "${data.aws_acm_certificate.domain1_wildcard.arn}"
service2 = "${data.aws_acm_certificate.domain2.arn}"
# ...
}
}
data "aws_acm_certificate" "domain1_wildcard" {
}
data "aws_acm_certificate" "domain2" {
}
data "null_data_source" "cert_arn" {
inputs = {
service1 = "${data.aws_acm_certificate.domain1_wildcard.arn}"
service2 = "${data.aws_acm_certificate.domain2.arn}"
# ...
}
}
The wrapper script picks up only the appropriate version of
cert_arn.tf for the environment. Then we can just use things like
certificate_arn = "${data.null_data_source.cert_arn.inputs.service1}" in our common resources. Obviously, I'm showing all ACM certs, but IAM certs should work as well.
If you don't want a wrapper script that assembles files depending on environment, you can use the conditional interpolation expressions in newer versions of Terraform to select the correct cert resource and make the cert resources themselves conditional using the count attribute.
-Trevor