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.Defining and Introducing Variables in Terraform
Instead of hard-coding resource values, it is best practice to use variables. First, create avariables.tf file with definitions for your instance name and VPC name:
main.tf) as shown below:
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.terraform.tfvars file with:
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 theterraform.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
- Navigate to the Environment Variables section in Spacelift and click Edit.
- Instead of setting individual variables, select the Mounted File option.
-
Provide the file path:
-
Upload an existing
terraform.tfvarsfile or create a new one with the following content:
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 theterraform destroy command. Since Spacelift cannot handle interactive prompts, include the --auto-approve flag:
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!