How to Migrate Existing Terraform State into Terraform Cloud

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Terraform is a tool for building, changing and destroying infrastructure such as cloud resources on-demand. For many users migrating from one provider to another can be complicated because of differing support and configuration options between providers. Terraform Cloud provides an easy way to manage your state from the comfort of Terraform by managing the complete lifecycle with our single interface that takes care of provisioning and deprovisioning your servers in parallel.

The “terraform migrate” command is a tool that allows users to migrate their existing Terraform state into Terraform Cloud.

How to Migrate Existing Terraform State into Terraform Cloud

Manually managing Terraform state files, particularly when migrating between on-premise and cloud-based setups, may be a nightmare. Terraform Cloud, on the other hand, may help you overcome this issue. Terraform Cloud allows you to manage your Terraform state file in the cloud, allowing you to monitor and observe changes in your infrastructure using a web-based user interface.

You’ll learn how to migrate your existing Terraform state file to Terraform Cloud in this tutorial. This allows you to manage your state file on your local PC while still using Terraform Cloud to access it.

Jump right in and get your hands dirty!

Prerequisites

This will be a hands-on presentation in this course. If you want to follow along, make sure you have the following items on hand:

How to Install Terraform on Linux and Windows is a related topic.

  • A user having administrative rights.
  • On the EC2 instance, AWS CLI was installed and configured.
  • A Linux computer — Amazon Linux 2 is used in this demonstration, but any Linux distribution will work.

Related: [Step-by-Step] How to Install Ubuntu 20.04

Constructing a Local Environment

Kick-off this tutorial by Constructing a Local Environment. Why? You will want to maintain your local state file and not depend on Terraform Cloud all the time. With a local environment set up, you can revert to your local state file for testing or if you’re not connected to the Internet.

To install git on your machine, SSH into your EC2 instance, open your terminal, and enter the yum command below. Later, you’ll clone the primary Terraform Cloud Git source using Git.

install git on your computer install git on your computer

2. Next, clone the Terraform cloud repository to your current directory using the git clone command.

https://github.com/Adam-the-Automator/terraform-state.git sudo git clone

The Terraform cloud repository is being cloned. The Terraform cloud repository is being cloned.

3. To list files and directories in the current directory, use the ls command.

You now have a new directory named migrate-state, as seen below. This is the directory you’ll use to follow along with this lesson.

displaying all of the newly created directories displaying all of the newly created directories

4. Switch (cd) to the migrate-state directory and list (ls) all the files and folders using the following commands.

This directory includes a main.tf file, as seen below. The main.tf file is the entry point to your infrastructure in all Terraform projects. As a result, the Terraform engine executes the code in this file when you run terraform apply.

Taking a look at the migrate-state directory Taking a look at the migrate-state directory

5. Use your preferred text editor to open the main.tf file. Replace the highlighted values below with the ami and subnet id of your EC2 instance, then save and exit the editor.

Replacing your current assets Replacing your current assets

6. To establish your working directory, use the terraform init command.

This tool reads all of the files in the current directory, identifies which ones are Terraform configuration files, and converts them into a dependency graph. terraform init sudo

To extract modifications from the configuration files, you may use the terraform init command as many times as you like.

Getting your working directory started Getting your working directory started

Finally, to apply your modifications to the working directory, use the terraform apply command below. When asked, type Y and click Enter.

This command will be performed every time you make a modification and want to send it to the cloud. apply terraform

Changing things up Changing things up

Getting a Terraform Cloud Environment Up and Running

Now that your local environment is ready, it’s time to set up your Terraform Cloud environment. Getting a Terraform Cloud Environment Up and Running enables you to access the remote state storage and sync it with various providers and your local state files.

1. Log in to the AWS IAM dashboard with your Admin user account using your favorite web browser.

To see the account’s details, go to the Users option on the left panel and choose your admin account from the list of users. For this example, the admin account is cloud user, as seen below.

AWS Identity and Access Management (IAM) Through Examples is a related article.

Getting Information About Your Admin Account Getting Information About Your Admin Account

2. Next, go to the Security Credentials page and generate an access key by clicking Create Access Key. You’ll need an access key to allow Terraform Cloud to connect with your AWS account.

Creating an Access Key for the Admin User Creating an Access Key for the Admin User

The credentials for the admin user are listed below (Access key ID and Secret access key). You’ll need these credentials later, so save them.

Viewing the Access Key for the Admin User Viewing the Access Key for the Admin User

3. Go to your Terraform Cloud dashboard and choose Create an organization from the drop-down menu. Organizing your Terraform Cloud resources and users under a single organization is as simple as creating a new organization.

Using the Terraform Cloud to create an organization Using the Terraform Cloud to create an organization

4. Enter a name for your organization (migrate-state) and an email address, then click Create organization. Terraform Cloud will send email alerts about your company to the address you provide (updates to your users, billing info, etc.).

Providing information about your company Providing information about your company

5. To have a command-line interface to your Terraform Cloud, choose the CLI-driven workflow option. Your current CI/CD pipelines may be used with the command-line interface.

Selecting a Workflow Selecting a Workflow

6. Provide your chosen Workspace Name; however, for this demonstration, the workspace name is migrate-state. To create a new workspace, click Create workspace.

Providing the Name of the Workspace Providing the Name of the Workspace

7. Now, under the workspace’s (migrate-state) settings page, go to the Variables tab and click Add variable to add variables.

Variable addition Variable addition

8. Create a variable with the following values for your access key ID:

  • In the Key text box, provide a desired name; however, for this demonstration, the variable name is AWS ACCESS KEY ID.
  • In the Value text box, enter the access key ID you recorded in step two.
  • To save the variable to your workspace, check the Sensitive box and click Save variable. The visibility of sensitive fields in your Terraform Cloud is controlled by the status of the Sensitive box.

When generating keys and values, it’s a good idea to check the Sensitive box. Any UI, such as the Terraform Cloud dashboard, will display the sensitive data. As a result, if you share a workspace with other users, they won’t be able to view your keys and values.

Adding the variable AWS ACCESS KEY ID Adding the variable AWS ACCESS KEY ID

9. Add a variable for your admin user’s secret access key you specified in step two by repeating steps seven to eight. But this time, give the variable a another name, such as AWS SECRET ACCESS KEY.

Variable AWS SECRET ACCESS KEY Variable AWS SECRET ACCESS KEY

You can see your keys in the list of variables once you’ve added the variables.

Using Terraform Output Variables in Infrastructure Configuration is a related topic.

Getting a Terraform Cloud Backend up and running

Now that you’ve created your Terraform Cloud account, it’s time to set up your Terraform Cloud backend. Terraform Cloud Backend allows you to manage resources, variables, and back-end settings from a single location.

Without hardcoding variable values, the Terraform Cloud backend enables you to collaborate with numerous teams or produce alternative configurations for different settings.

However, you must first generate a Terraform Cloud API token before you can set up your Terraform Cloud backend. The API token will be used to authenticate your Terraform Cloud backend. This token guarantees that no one else has unrestricted access to or modification of your infrastructure.

1. On your Terraform Cloud dashboard, go to User Settings and click on your profile name in the upper right corner.

Getting to User Preferences Getting to User Preferences

2. On the USER SETTINGS page, select the Tokens tab, then Create an API token to create a new API Token. You’ll be sent to a new popup where you may generate a new API Token (step three).

Token creation for API Token creation for API

3. In the Description text box, type migrate-state as the token’s name, and then click Create API token to complete the process.

Choosing an API token's description Choosing an API token’s description

4. Copy and store your API Token somewhere safe before clicking Done.

Keeping track of your API token Keeping track of your API token

5. Return to your terminal and use the terraform login command to log into Terraform Cloud via the command line. When asked, type yes and click Enter, as seen below.

Using your command-line interface to access Terraform Cloud. Using your command-line interface to access Terraform Cloud.

When asked to provide a value, enter the API token you saved previously.

Filling in your API token Filling in your API token

If you’ve successfully signed in to Terraform Cloud, you’ll get the following output.

Checking to see whether you've signed into Terraform Cloud successfully Checking to see whether you’ve signed into Terraform Cloud successfully

7. At establish your backend provider, open the main.tf file in your text editor and add the following backend codes to the beginning of the file’s content.

Replace the values below with your desired settings and save your changes:

  • THE NAME OF YOUR TERRAFORM CLOUD ORGANIZATION – The name of your Terraform Cloud organization. The name of the organization in this example is migrate-state.
  • THE WORKSPACE NAME – The name of the workspace you wish to utilize for this project. Make sure the value corresponds to the workspace name you specified in your Terraform Cloud dashboard. The workspace name in this example is migrate-state.

# Sets your backend provider terraform { backend “remote” { organization = “YOUR_ORG_NAME” workspaces { name = “YOUR_WORKSPACE” } } # This line is a required setting when you initialize a backend config. # You can set this multiple times to add additional providers. required_providers { # Sets your aws backend config. # You can also set custom providers like azurerm, gcp, and vsphere here. aws = { # Specifies the location of your config for this backend. source = “hashicorp/aws” # Specifies the version of your Backend Provider. version = “~> 3.27” } } }

The content of your main.tf file should look like this.

The main.tf Configuration File is being edited. The main.tf Configuration File is being edited.

Putting the Backend Configuration into Practice

You’ve put up your backend configuration, but it’s useless until you apply it to your Terraform Cloud.

1. Use the terraform fmt command to format the source code in your main.tf file so that it can be read by humans. terraform fmt terraform fmt terraform fmt

Formatting the main.tf document Formatting the main.tf document

2. To initialize the working directory, use the terraform init command. When asked, type Y and click Enter.

Getting the working directory started Getting the working directory started

3. Run the rm command to delete the existing state file. You will want to use a new state file generated by Terraform Cloud when Putting the Backend Configuration into Practice.

Because you have a backup copy of your state file, there is no chance of losing any modifications when you perform the rm -rf terraform.tfstate command. When you execute the terraform init command, Terraform builds the state file for you.

removing the state file that already exists removing the state file that already exists

4. Use the ls command to list the files in your working directory to confirm that terraform.tfstate is no longer there and that the backup copy of your state file is still present.

Ensure that a backup copy of your state file has been produced. Ensure that a backup copy of your state file has been produced.

5. To apply the backend settings, use the terraform apply command.

Because the command uses provider APIs, it may take some time to finish. Your backend setup is implemented after the procedure is finished.

Putting the Backend Configuration into Practice Putting the Backend Configuration into Practice

6. Return to your Terraform Cloud home page, then select the workspace’s hyperlink to get an overview of your workspace.

Getting into the Workplace Getting into the Workplace

7. Next, go to the Runs tab and make sure your most recent run is shown in your workspace, as seen below.

Preparing for your next run Preparing for your next run

8. Go to the States tab, as shown below, and double-click the state file to open it.

Getting to the State File Getting to the State File

You may now check and confirm that you’ve successfully migrated your state file in your Terraform Cloud, as shown in the picture below.

Seeing your State File in Action Seeing your State File in Action

Conclusion

Throughout this tutorial, you’ve learned how to migrate an existing Terraform state into your Terraform Cloud by Getting a Terraform Cloud Environment Up and Running and backend.

You should now have a completely working backend setup that you can use to migrate your Terraform state file to your Terraform Cloud.

Now, how do you intend to put your acquired information to use? Why not use Amazon S3 to store all of your Terraform state?

Terraform is a tool that allows you to create and manage infrastructure. It can be used for cloud, virtual or physical infrastructures. If you want to migrate your existing Terraform state into Terraform Cloud, then this article will help you out. Reference: terraform remote state.

Related Tags

  • terraform cloud import resource
  • terraform migrate backend
  • terraform move state between workspaces
  • terraform backend
  • terraform local migrate