This article explores best practices for using variables in Terraform configurations while managing AWS resources with Spacelift’s mounted files feature.
In this article, we explore best practices for using variables in Terraform configurations while managing AWS resources with Spacelift: Elevate Your Infrastructure Deployment. You will learn how to update an AWS instance configuration, define and use Terraform variables, and leverage Spacelift’s mounted files feature to supply variable values consistently.
Below is an example Terraform configuration for an AWS EC2 instance. Modify the configuration as needed, for instance by updating the instance type or other resource parameters.
Copy
Ask AI
resource "aws_instance" "app_server" { ami = "ami-02396cdd13e9a1257" instance_type = "t2.micro" tags = { Name = "app-server" }}
After updating the resource, commit the changes using Git:
Copy
Ask AI
git commit -m "changing to t2.micro"[main e890439] changing to t2.micro 1 file changed, 1 insertion(+), 1 deletion(-)
And then push the commit:
Copy
Ask AI
git pushEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 12 threadsCompressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 362 bytes | 362.00 KiB/s, done.Total 3 (delta 1), reused 0 (delta 0), pack-reused 0remote: Resolving deltas: 100% (1/1), completed with 1 local object.To https://github.com/Sanjeev-Thiyagarajan/spacelift-demo.git dbf10f3..e890439 main -> main
Instead of hard-coding resource values, it is best practice to use variables. First, create a variables.tf file with definitions for your instance name and VPC name:
Copy
Ask AI
variable "instance_name" { description = "Value of the Name tag for the EC2 instance" type = string default = "myInstance"}variable "vpc_name" { description = "Name of the VPC" type = string}
If you need the values to be provided at runtime, simply remove the default from the variable declaration:
Copy
Ask AI
variable "instance_name" { description = "Value of the Name tag for the EC2 instance" type = string}variable "vpc_name" { description = "Name of the VPC" type = string}
Reference these variables in your main Terraform configuration (main.tf) as shown below:
If you trigger a Terraform run without setting required variable values, you will encounter errors during the planning stage. For example:
Copy
Ask AI
Error: No value for required variable on variables.tf line 1:1: variable "instance_name" {The root module input variable "instance_name" is not set, and has no default value. Use a var or -var-file command line argument to provide a value for this variable.Error: No value for required variable on variables.tf line 7:7: variable "vpc_name" {
These errors indicate that the values for instance_name and vpc_name have not been provided. To resolve this, supply the values using a terraform.tfvars file.
Managing numerous variables via environment variables can be cumbersome for large projects. Fortunately, Spacelift: Elevate Your Infrastructure Deployment offers a mounted file feature that allows you to supply all your variable definitions through the terraform.tfvars file.In Spacelift, workloads execute in a dedicated directory structure:
All operations occur in /mnt/workspace.
Your Git repository is cloned into /mnt/workspace/source.
Ensure your terraform.tfvars file is mounted inside /mnt/workspace/source.
After mounting the file, trigger another run. The output should reflect that the variable values have been successfully injected. For example, you might see:
Copy
Ask AI
Plan: 0 to add, 2 to change, 0 to destroy.Changes are GOUploading the list of managed resources...Please be aware that Run Changes calculation includes Terraform output changes.Resource list upload is GOGenerating JSON representation of the plan...loading custom plan policy inputs...Evaluated 1 plan policypreflight checks are GOExporting workspace...Uploading workspace...workspace upload is GO
Within the detailed plan, the changes might appear similar to:
Copy
Ask AI
Terraform will perform the following actions: # aws_instance.app_server will be updated in-place resource "aws_instance" "app_server" { id = "i-aadd255fc794a8b9" tags = { "Name" = "app-server" "env" = "testing-instance" } } tags_all = { "Name" = "app-server" "env" = "testing-instance" } # (30 unchanged attributes hidden) # (7 unchanged blocks hidden)
This confirms that the terraform.tfvars file data was correctly used during the Terraform run.
To delete all the resources created by Terraform, you can use the terraform destroy command. Since Spacelift cannot handle interactive prompts, include the --auto-approve flag:
Copy
Ask AI
terraform destroy --auto-approve
If you experience issues—such as the command being blocked by a recent commit—consider discarding those changes and trying again. You can view the current state of resources with:
Copy
Ask AI
terraform state list
Once the process starts, Terraform will refresh the configuration, display a list of resources to be destroyed, and proceed with cleanup. After a short wait, you should receive confirmation of successful deletion.That concludes this comprehensive guide on managing Terraform variables and utilizing Spacelift’s mounted file feature. For additional resources, refer to the following links: