How to access terragrunt variables in terraform code - terraform

I have terragrunt config where have declared the variables using locals as below at root level. In the child module , have declared the child terragrunt config file called (terragrunt.hcl).
parent terragrunt file has following code :
locals {
location = "East US"
}
child module terragrunt file has below code :
include {
path = find_in_parent_folders()
}
locals {
myvars = read_terragrunt_config(find_in_parent_folders("terragrunt.hcl"))
location = local.myvars.locals.location
}
now, trying to access location variable in the terraform code (main.tf) using following code :
location = "${var.location}"
but it throws error:
Error: Reference to undeclared input variable
on main.tf line 13, in resource "azurerm_resource_group" "example":
13: location = "${var.location}"
Not getting how i can access variables defined in the terragrunt file in the terraform code . please suggest

This error message means that your root module doesn't declare that it is expecting to be given a location value, and so you can't refer to it.
In your root Terraform module you can declare that you are expecting this variable by declaring it with a variable block, as the error message hints:
variable "location" {
type = string
}
This declaration will then make it valid to refer to var.location elsewhere in the root module, and it will also cause Terraform to produce an error if you accidentally run it without providing a value for this location variable.

Related

Import variables from module terraform

please help to understand how I can import variables files from a different location?
I have tried to do this from the module system, but it's not working for me.
My structure :
/
/variables.tf
/my_ec2/main.tf
/my_ec2/variables.tf
How I can import variables from root folder? Need to specify it somehow on main.tf
My /my_ec2/main.tf
module "global_vars" {
source = "../../../"
}
provider "aws" {
region = "module.global_vars.region_aws"
}
my /variables.tf
variable "region_aws" {
default = "eu-central-1"
}
How can I do this?
P.S. Did the same with "${var.region_aws}", but same result
Error: Reference to undeclared input variable
on ../my_ec2/main.tf line 10, in resource "aws_instance" "server":
10: region = "${var.region_aws}"
An input variable with the name "aws_instance" has not been declared. This
variable can be declared with a variable "environment" {} block.
Maybe use :
"${module.global_vars.region_aws}"
Instead of
"module.global_vars.region_aws"

Unable to read variables from Terraform variable file

Here is my setup,
Terraform version - Terraform v0.12.17
OS - OSX 10.15.1
Use Case - define a provider file and access the variables defined in the vars file
Files
main.tf - where the code is
provider "aws" {
}
variable "AWS_REGION" {
type = string
}
variable "AMIS" {
type = map(string)
default = {
us-west-1 = "my ami"
}
}
resource "aws_instance" "awsInstall" {
ami = var.AMIS[var.AWS_REGION]
instance_type = "t2.micro"
}
awsVars.tfvars - where the region is defined
AWS_REGION="eu-region-1"
Execution
$ terraform console
var.AWS_REGION
Error: Result depends on values that cannot be determined until after "terraform apply".
What mistake I have done, I don't see any syntax but have issues in accessing the variables, any pointers would be helpful
Thanks
Terraform does not automatically read a .tfvars file unless its filename ends with .auto.tfvars. Because of that, when you ran terraform console with no arguments Terraform did not know a value for variable AWS_REGION.
To keep your existing filename, you can pass this variables file explicitly on the command line like this:
terraform console -var-file="awsVars.tfvars"
Alternatively, you could rename the file to awsVars.auto.tfvars and then Terraform will read it by default as long as it's in the current working directory when you run Terraform.
There's more information on how you can set values for root module input variables in the Terraform documentation section Assigning Values to Root Module Variables.
Note also that the usual naming convention for input variables and other Terraform-specific objects is to keep the names in lowercase and separate words with underscores. For example, it would be more conventional to name your variables aws_region and amis.
Furthermore, if your goal is to find an AMI for the current region (the one chosen by the AWS_DEFAULT_REGION environment variable, or in the provider configuration), you could use the aws_region data source to allow Terraform to determine that automatically, so you don't have to set it as a variable at all:
variable "amis" {
type = map(string)
default = {
us-west-1 = "my ami"
}
}
data "aws_region" "current" {}
resource "aws_instance" "awsInstall" {
ami = var.amis[data.aws_region.current.name]
instance_type = "t2.micro"
}

Terraform module throws error about requiring a string

I've been trying to split my terraform code from one large file into separate modules. I keep running into an issue where the following error appears when running Terraform Plan.
Error: Incorrect attribute value type
on modules/nsg/main.tf line 11, in resource "azurerm_network_security_group" "InternalProdNSGPrivate":
11: resource_group_name = "${module.rg.main-rg-id}"
Inappropriate value for attribute "resource_group_name": string required.
I created an outputs.tf file which has the following:
output "main-rg-id" {
value = "${azurerm_resource_group.InternalProd}"
}
The main.tf for this module has the following:
module "global_variables" {
source = "../global_variables"
}
resource "azurerm_resource_group" "InternalProd" {
name = "Internal"
location = "${module.global_variables.location}"
}
In the main.tf file for the NSG i have the following configured:
module "rg" {
source = "../rg"
}
module "global_variables" {
source = "../global_variables"
}
resource "azurerm_network_security_group" "InternalProdNSGPrivate" {
name = "Internal-NSG"
location = "${module.global_variables.location}"
resource_group_name = "${module.rg.main-rg-id}"
....
}
Not sure where im going wrong here with the configuration. Tried looking at multiple different resources, blogs, etc. but no luck.
azurerm_resource_group.InternalProd is an object representing the whole of resource "azurerm_resource_group" "InternalProd".
To produce just the id of that object, you can access attribute id like this:
output "main-rg-id" {
value = azurerm_resource_group.InternalProd.id
}

Values to variables mentioned in variables.tf in Terraform through environment variables file .tfvars

How do I pass the values of variables mentioned in variable.tf of type "list" or "map" . Are there any mistakes in the syntax in the input.tfvars file mentioned below?
The goal is not to hard code any values in the variables.tf or the main.tf file. In the Terraform.io docs I verified that the values can be provided in this format.
This is from the site:
Lists are defined either explicitly or implicitly:
# implicitly by using brackets [...]
variable "cidrs" { default = [] }
# explicitly
variable "cidrs" { type = "list" }
You can specify lists in a the terraform.tfvars file:
cidrs = [ "10.0.0.0/16", "10.1.0.0/16" ]
Now, when I try to do
terraform plan -var-file=input.tfvars
it is unable to read the variable values from the tfvars file and presents the following error:
Error: module root:
module vpc: required variable "vpccidr" not set
module vpc: required variable "vpcname" not set
The variables.tf file looks like:
variable "vpccidr" { type = "list"}
variable "vpcname" { type = "list" }
The input.tfvars file looks like:
vpccidr=[ "10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16" ]
vpcname=[ "vpc1", "vpc2", "vpc3" ]
The main.tf file looks like:
module "vpc"{
source = "modules/network/vpc"
}
The VPC module under modules/network has the following configuration file main.tf and the variable file mentioned above in variables.tf:
resource "aws_vpc" "customVpc" {
count = "${length(var.vpccidr)}"
cidr_block = "${element(var.vpccidr, count.index)}"
tags {
count = "${length(var.vpcname)}"
Name = "${element(var.vpcname, count.index)}"
}
}
You need to pass the variables to your module, i.e
module "vpc"{
source = "modules/network/vpc"
vpccidr = "${var.vpccidr}"
vpcname = "${var.vpcname}"
}

Terraform Azure database module - unknown resource

trying to use the database module from: https://registry.terraform.io/modules/Azure/database/azurerm/1.0.1
i have made objects in Azure from my workstation, so the basics are all in place.
I copied the main / variables / outputs files to a folder (sql) and then used the example (below) to call it.
# Configure the Azure Provider
provider "azurerm" {}
module "sql-database" {
source = "./sql"
resource_group_name = "myapp"
location = "westus"
db_name = "mydatabase"
sql_admin_username = "mradministrator"
sql_password = "P#ssw0rd12345!"
tags = {
environment = "dev"
costcenter = "it"
}
}
When i run terraform init, i get the below error, any idea what im doing wrong?
PS C:\GITHUB\terraform_azure\app> terraform init
Initializing modules...
- module.sql-database
Error getting plugins: 9 problems:
- output 'sql_server_name': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.name
- output 'sql_server_location': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.location
- output 'sql_server_version': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.version
- output 'sql_server_fqdn': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.fully_qualified_domain_name
- output 'connection_string': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.administrator_login_password
- output 'connection_string': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.fully_qualified_domain_name
- output 'connection_string': unknown resource 'azurerm_sql_database.db'
referenced in variable azurerm_sql_database.db.name
- output 'connection_string': unknown resource 'azurerm_sql_server.server'
referenced in variable azurerm_sql_server.server.administrator_login
- output 'database_name': unknown resource 'azurerm_sql_database.db'
referenced in variable azurerm_sql_database.db.name
# Configure the Azure Provider
provider "azurerm" {}
module "sql-database" {
version = "1.0.1"
source = "./sql"
resource_group_name = "myapp"
location = "westus"
db_name = "mydatabase"
sql_admin_username = "mradministrator"
sql_password = "P#ssw0rd12345!"
tags = {
environment = "dev"
costcenter = "it"
}
}
Updated the main file to above and it worked (AKA I added the version number.. Not sure why that made it work! I dont see anything in the database Main file saying anything about version.. but that got it the init completing!

Resources