Datasource as Default value in Terraform Variable
About : Logic Building with Variable and Datasource
Level : Intermediate
Audience : Terraform Engineer
We often try to create a logic in programming where we want to provide a value of variable in some specific cases and in all others, we want to use the same value(default).
Terraform Variable can do the same thing while specifying the default value in variable block, see the below example.
variable "test" {
default = "name"
}
In the above example, if we specify some value of test variable it will override the value "name".
A Terraform data source allows you to query and retrieve data from external sources such as cloud providers, databases, or APIs. This data can then be used as input for creating or managing resources in your infrastructure code.
See the below example for DataSource to retrieve Public IP of the Workstation
provider "http" {
? # Configure the HTTP provider
}
data "http" "public_ip" {
? url = "https://api.ipify.org"
}
output "public_ip" {
? value = data.http.public_ip.body
}
While creating the logic to combine terraform variable with DataSource for specifying a default which is retrieved from DataSource.
领英推荐
variable "test" {
default = data.http.public_ip.body
}
The above variable block is not allowed as DataSource are retrieved in apply phase and variable need to provided in "plan" phase. If you run the above block an error as "Variables are not allowed" will be shown.
One common use case for Terraform is to configure infrastructure resources based on the values of variables or data sources. In this article, we'll show you how to use either a data source or a variable value in Terraform, depending on whether a variable value is given or not.
First, let's look at an example of a Terraform script that uses either a data source or a variable value. Here's the code:
variable "awsami"
}
data "aws_ami" "ubuntu" {
? most_recent = true
? filter {
? ? name ? = "name"
? ? values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
? }
? filter {
? ? name ? = "virtualization-type"
? ? values = ["hvm"]
? }
? owners = ["099720109477"] # Canonical
}
resource "aws_instance" "web" {
? ami ? ? ? ? ? = var.awsami != "" ? var.awsami : data.aws_ami.ubuntu.id
? instance_type = "t3.micro"
? tags = {
? ? Name = "HelloWorld"
? }
}{
In this example, we've defined a data source for an AWS AMI using the aws_ami data source. The data source is filtered to only include Ubuntu AMI. We've also defined a variable awsami of type string.
In the aws_instance resource, we use the ternary operator (?) to conditionally set the ami based on the value of var.awsami. If var.awsami is not an empty string, it will be used as the ami. If var.awsami is an empty string, the ami will be set to the value of the id field from the aws_ami data source.
In this way, you can use either a data source or a variable value in Terraform, depending on whether a variable value is given or not. This can be useful in scenarios where you want to provide the option to specify a value via a variable, while also having a default value available through a data source.
In summary, you can't use the value of a data source as the default value of a variable in Terraform because data sources are evaluated after variables, and their values can't be used to set the default value of a variable. To use the value of a data source as the value of a variable, you must set the variable value dynamically, either via the command line or in a separate file.
#Terraform #IaC #InfrastructureAsCode #AWS #DataSource #VariableValue #InfrastructureManagement #Automation #OpenSource #Code #DevOps #Provisioning #Repeatable #Predictable #TernaryOperator #InfrastructureResources #AWSInstance #InstanceId #CloudComputing #OperatingSystem #RunningInstances #FilteredData #ConditionalValues #OptionToSpecify #DefaultValues #InfrastructureAutomation #ProgrammingLanguages #CloudInfrastructure #Serverless #TerraformScript #ProgrammaticInfrastructure #Hashtags #InfrastructureProvisioning #DevOpsTools #DevOpsLife #InfrastructureCode #AWSCloud #CloudManagement #SoftwareDevelopment #SoftwareEngineering #ContinuousIntegration #ContinuousDelivery
CloudTruth co-founder & CPO
1 年Great post. FYI, there’s a free version of CloudTruth that you can use to parameterize Terraform variables from a single source of truth. More info at this link: https://www.cloudtruth.com/terraform-solution
Mohd. Fawaz Akhtar Awesome! Thanks for Sharing! ??