Terraform - Store remote state
Prerequisites
This tutorial assumes that you have completed previous tutorials. If you have not, create a directory named learn-terraform-aws-instance
and paste this code into a file named main.tf
.
Run terraform init
to initialize your configuration directory and download the required providers. It is safe to re-run this command even if you have already done so in this directory.
Next, apply your configuration. Type yes
to confirm the proposed changes.
Terraform provisioned an AWS EC2 instance and stored data about the resource in a local state file.
Set up Terraform Cloud
If you have a HashiCorp Cloud Platform or Terraform Cloud account, log in using your existing credentials. For more detailed instructions on how to sign up for a new account and create an organization, review the Sign up for Terraform Cloud tutorial.
Next, modify main.tf
to add a cloud
block to your Terraform configuration, and replace organization-name
with your organization name.
Note
Older version of Terraform do not support the cloud
block, so you must use 1.1.0 or higher in order to follow this tutorial. Previous versions can use the remote
backend block to configure the CLI workflow and migrate state.
Login to Terraform Cloud
Next, log into your Terraform Cloud account with the Terraform CLI in your terminal.
Confirm with a yes
and follow the workflow in the browser window that will automatically open. You will need to paste the generated API key into your Terminal when prompted. For more detail on logging in, follow the Authenticate the CLI with Terraform Cloud tutorial.
Initialize Terraform
Now that you have configured your Terraform Cloud integration, run terraform init
to re-initialize your configuration and migrate your state file to Terraform Cloud. Enter "yes" when prompted to confirm the migration.
Now that Terraform has migrated the state file to Terraform Cloud, delete the local state file.
When using Terraform Cloud with the CLI-driven workflow, you can choose to have Terraform run remotely, or on your local machine. When using local execution, Terraform Cloud will execute Terraform on your local machine and remotely store your state file in Terraform Cloud. For this tutorial, you will use the remote execution mode.
Set workspace variables
The terraform init
step created the learn-tfc-aws
workspace in your Terraform Cloud organization. You must configure your workspace with your AWS credentials to authenticate the AWS provider.
Navigate to your learn-tfc-aws
workspace in Terraform Cloud and go to the workspace's Variables page. Under Workspace Variables, add your AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
as Environment Variables, making sure to mark them as "Sensitive".
Apply the configuration
Now, run terraform apply
to trigger a run in Terraform Cloud. Terraform will show that there are no changes to be made.
This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, Terraform does not need to do anything.
Terraform is now storing your state remotely in Terraform Cloud. Remote state storage makes collaboration easier and keeps state and secret information off your local disk. Remote state is loaded only in memory when it is used.
Destroy your infrastructure
Make sure to run terraform destroy
to clean up the resources you created in these tutorials. Terraform will execute this run in Terraform Cloud and stream the output to your terminal window. When prompted, remember to confirm with a yes
. You can also confirm the operation by visiting your workspace in the Terraform Cloud web UI and confirming the run.
Next Steps
This concludes the getting started tutorials for Terraform. Now you can use Terraform to create and manage your infrastructure.
For more hands-on experience with the Terraform configuration language, resource provisioning, or importing existing infrastructure, review the tutorials below.
Configuration Language - Get more familiar with variables, outputs, dependencies, meta-arguments, and other language features to write more sophisticated Terraform configurations.
Modules - Organize and re-use Terraform configuration with modules.
Provision - Use Packer or Cloud-init to automatically provision SSH keys and a web server onto a Linux VM created by Terraform in AWS.
Import - Import existing infrastructure into Terraform.
To read more about available configuration options, explore the Terraform documentation.
Learn more about Terraform Cloud
Although Terraform Cloud can store state to support Terraform runs on local machines, it works even better as a remote run environment. It supports two main workflows for performing Terraform runs:
- A VCS-driven workflow, in which it automatically queues plans whenever changes are committed to your configuration's VCS repo.
- An API-driven workflow, in which a CI pipeline or other automated tool can upload configurations directly.
For a hands-on introduction to the Terraform Cloud VCS-driven workflow, follow the Terraform Cloud getting started tutorials. Terraform Cloud also offers commercial solutions which include team permission management, policy enforcement, agents, and more.
Comments
Post a Comment