I have a requirement where I have to deploy resources only if a certain file exists at a certain location otherwise it will skip the resource.
Like here is the code to deploy a certain identity provider in certain AWS accounts. Along with this identity provider (say abc) many other identity providers are also deployed from the same main.tf file so all has to be here. The only challenge is while deploying the IAM layer for any accounts we will only deploy this certain resource only if abc-${var.aws_account}.xml file exists in the filepath in
saml_metadata_document part. If it does not exists in the path it will simply ignore the resource creation and will go ahead with the rest of the code.
resource "aws_iam_saml_provider" "xyz" {
name = "abc-${var.aws_account}"
saml_metadata_document = "${file("${path.module}/metadata/abc-${var.aws_account}.xml")}"
}
Folder Structure
IAM-Module
|
main.tf
variables.tf
metadata
|
abc-127367223.xml
abc-983297832.xml
abc-342374384.xml
How can a conditional check be put in Terraform 0.11 to check the file exists?
countcan be used to create an array of resources instead of just a single resource, so setting count = 0 will create an array of resources of length 0, effectively disabling the resource.NOTE You will need access to
fileexistswhich only exists in 0.12