C3w2a1 - Terraform - How does the Variables get its value

In c3w2a1 - Assignment 1 of week 2. [Data Storage and Queries Course] - How does Terraform gets the Variable values.
I do not see it getting passed using tfcars file or not using command line when terraform plan or apply.

List of Varaibales :
project
region
vpc_id
private_subnet_a_id
db_sg_id
source_host
source_port
(… more are there)

Hello @joyan
It is nice that you are paying attention to these details :slightly_smiling_face:
These terraform variables are read from the OS environment variables and are defined in the setup_jupyter_env.sh file. When you run source scripts/setup_jupyter_env.sh in the command line, these variables are initiated. The following is part of the setup_jupyter_env.sh file.

# Define Terraform variables
echo "export TF_VAR_project=$de_project" >> $HOME/.bashrc
echo "export TF_VAR_region=$AWS_DEFAULT_REGION" >> $HOME/.bashrc
echo "export TF_VAR_vpc_id=$VPC_ID" >> $HOME/.bashrc
echo "export TF_VAR_private_subnet_a_id=$(aws ec2 describe-subnets --filters "Name=tag:aws:cloudformation:logical-id,Values=PrivateSubnetA" "Name=vpc-id,Values=$VPC_ID" --output text --query "Subnets[].SubnetId")" >> $HOME/.bashrc
echo "export TF_VAR_data_lake_name=$de_project-$(aws sts get-caller-identity --query 'Account' --output text)-$AWS_DEFAULT_REGION-data-lake"  >> $HOME/.bashrc

echo "export TF_VAR_db_sg_id=$(aws rds describe-db-instances --db-instance-identifier $de_project-rds --output text --query "DBInstances[].VpcSecurityGroups[].VpcSecurityGroupId")" >> $HOME/.bashrc
echo "export TF_VAR_source_host=$(aws rds describe-db-instances --db-instance-identifier $de_project-rds --output text --query "DBInstances[].Endpoint.Address")" >> $HOME/.bashrc
echo "export TF_VAR_source_port=3306" >> $HOME/.bashrc
echo "export TF_VAR_source_database="classicmodels"" >> $HOME/.bashrc
echo "export TF_VAR_source_username="admin"" >> $HOME/.bashrc
echo "export TF_VAR_source_password="adminpwrd"" >> $HOME/.bashrc
echo "export TF_VAR_curated_db_name="curated_zone"" >> $HOME/.bashrc
echo "export TF_VAR_curated_db_ratings_table="ratings"" >> $HOME/.bashrc
echo "export TF_VAR_curated_db_ml_table="ratings_for_ml"" >> $HOME/.bashrc
echo "export TF_VAR_ratings_new_column_name="ratingtimestamp"" >> $HOME/.bashrc
echo "export TF_VAR_ratings_new_column_type="string"" >> $HOME/.bashrc
echo "export TF_VAR_presentation_db_name="presentation_zone"" >> $HOME/.bashrc
echo "export TF_VAR_presentation_db_table_sales="sales_report"" >> $HOME/.bashrc
echo "export TF_VAR_presentation_db_table_employee="employee_report"" >> $HOME/.bashrc
echo "export TF_VAR_source_data_lake_name=$de_project-$(aws sts get-caller-identity --query 'Account' --output text)-$AWS_DEFAULT_REGION-source" >> $HOME/.bashrc
echo "export TF_VAR_glue_role_name=Cloud9-$de_project-glue-role" >> $HOME/.bashrc
1 Like

Thank you Amir. This step creates a bashrc file with export commands. At time of SSH initialization, bashrc executes are create environment variables like TF_VAR_project. How does terraform knows that it need to look at environment variables starting with “TF_VAR” and look for remaining part of the environment variable name to get terraform variable? Is this a by -default - configuration of Terraform execution?

Hello again @joyan
Yes, this is a by default configuration of Terraform. For further information, you can take a look at this part of the Terraform documentation.