What is Terraform?
- Terraform is an open-source infrastructure as a code tool created by HashiCorp, that lets you provision, build, change, and version cloud and on-prem resources. It lets you define both Cloud and on-prem resources in human-readable configuration files that you can version reuse, and share.
What is Infrastructure as Code?
- Infrastructure as code (IaC) is the process that allows you to manage infrastructure with configuration files rather than through a graphical user interface. IAC allows you to build, change, and manage your infrastructure in a safe, consistent, and repeatable way by defining resource configurations that you can version, reuse, and share.
What is Terraform state file?
When we create infrastructure after executing terraform apply command. Terraform creates a state file called terraform.tfstate. This state file contains all the information about the resources created using Terraform and keeps track of resources created by your configuration and maps them to real-world resources. Terraform state file is a sensitive file as it contains information about the infrastructure that we have created. You should never push this file to any version control system like GitHub. Store terraform.tfstate file in the backend to keep it safe.
The backend supported by Terraform:
Amazon S3
Azure Storage
Google Cloud Storage
HashiCorps Terraform Cloud and Terraform Enterprise.
In this article, we are using Terraform Cloud to store Terraform state file.
Prerequisite
AWS account and AWS Access key and Secret key created
Terraform installed on your IDE
AWS CLI installed and configured on your IDE
Basic understanding of AWS services and Terraform
Step-1: Create Terraform cloud account
Go to https://app.terraform.io/ and sign-up for a free account. After login creates an organization and a workspace. Choose Start from the scratch workflow.
Step-2: Create an organization
Create a new organization, enter your organization name and email address as shown below, and click Create organization.
Step-3: Create a workspace
Choose the workflow of your choice, here I am selecting a CLI-driven workflow.
Give a name and description to your workflow and click Create workspace.
Step-4: Clone repository
Clone this repository https://github.com/palakbhawsar98/Terraform/tree/main/terraform-remote-state-hands-on and Add the backend block in the configuration file and enter your organization-name and workspace name that you have created in previous steps. This block ensures that terraform.tfstate file doesn't get created in your local but in the remote backend i.e Terraform cloud.
terraform {
backend "remote" {
organization = "organization-name"
workspaces {
name = "terraform-backend-handson"
}
}
Step-5: Add environment variables
Go to the variables section in the left bar and click Add variable. Here we can add variables that are used by configuration files.
Select the Environment variable and Add AWS_ACCESS_KEY and SECRET_ACCESS_KEY and don't forget to checkbox Sensitive and click Save variable.
After adding environment variables the console will look like this. You can terraform variables also in this step.
Step-6: Terraform Login
Go to your project directory in the console and execute terraform login command and enter the value yes. Terraform will open the browser. Click Create API token.
Copy the API token and paste it into the terminal.
Now run the terraform init, terraform plan, and terraform apply commands to provision the infrastructure, You can see the console where the build will start.
After the triggered build is successful you will able to see the resources created in the AWS console, as well as the state file in Terraform Cloud.
Top comments (0)