Skip to main content
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.

Updating the AWS Instance Configuration

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.
After updating the resource, commit the changes using Git:
And then push the commit:

Defining and Introducing Variables in Terraform

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:
If you need the values to be provided at runtime, simply remove the default from the variable declaration:
Reference these variables in your main Terraform configuration (main.tf) as shown below:
Once changes are made, commit the updates:
And push them:

Handling Missing Variable Values

If you trigger a Terraform run without setting required variable values, you will encounter errors during the planning stage. For example:
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.
Create a terraform.tfvars file with:
Then, commit and push your changes if needed:
On rerunning Terraform, the provided variable values should be correctly applied.

Leveraging Mounted Files in Spacelift

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.

Setting Up the Mounted File

  1. Navigate to the Environment Variables section in Spacelift and click Edit.
  2. Instead of setting individual variables, select the Mounted File option.
  3. Provide the file path:
  4. Upload an existing terraform.tfvars file or create a new one with the following content:
After mounting the file, trigger another run. The output should reflect that the variable values have been successfully injected. For example, you might see:
Within the detailed plan, the changes might appear similar to:
This confirms that the terraform.tfvars file data was correctly used during the Terraform run.

Cleaning Up Resources

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:
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:
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: Happy Infrastructure Deployments!

Watch Video