Hello everyone, welcome back! In this article, you will learn how to write, plan, and maintain infrastructure using Terraform cloud.
Let us start with a brief about the Terraform cloud.
Terraform cloud is a self-hosted SaaS product that provides services to help teams and organizations collaborate, manage, maintain a consistent environment, and most importantly, automate the process and work as a team.
This article focuses on guiding individuals to step into the Terraform cloud. We will see how we can create a workspace and integrate it with the version control system to maintain the infra from Terraform cloud graphics.
So, let us go forward and use the Terraform Cloud.
To go further with Terraform cloud, you need to have GitHub and AWS Account.
- GitHub repository with Terraform configuration files. If you don’t have files, fork the repo https://github.com/shylajohn/Terraform-Cloud.git
- AWS account as we are going to provision resources on AWS. You can Signup here http://console.aws.amazon.com/
1. Sign up and create an Organization:
Quickly signup and create a free account from https://app.terraform.io/signup/account. Verify the email address.
2. Create a Workspace:
Now that we have created an organization, let us create workspaces inside them. You will land on a page to create a workspace, if not, click on a button to create a workspace inside an organization.
A workspace is where we have all the configuration files present. It is important to connect the workspace with VCS like Git, GitLab, Azure DevOps, Bit bucket. Having the source files in a version control system is in fact the recommended practice in the Software development process.
In tab 1, Select VCS (Version Control System)
In tab 2, select the VCS you want to use. Terraform Cloud provides flexibility to integrate with different version control systems like GitHub, GitLab, BitBucket, and Azure DevOps. In this demo, we are using GitHub, so select github.com.
- Once you click Github.com, a new window will popup. Sign in to your Github account from there. Perform the verification and install Terraform on Git.
- Now, we have the Github account connected with Terraform cloud.
In tab 3 choose the repository. In tab 4, Provide the name for the workspace and click the Create Workspace button. Once the workspace is created, you will see a success message in a popup.
3. Plan and Apply the changes:
Now we created our workspace and connected to the VCS. Let us plan and apply the files.
Our workspace contains two files, namely instance.tf and vars.tf. We are going to deploy an EC2 machine on AWS.
The snippet of the files is as below:
We are using two variables in the provider section to pass the AWS Account access key and secret key. These credentials are used to authenticate with AWS. It is not acceptable to hardcode them directly on the configuration as it is highly open to risk as it is sensitive data. Instead, we can pass as a variable.
In Terraform, we pass the sensitive data in 2 ways:
a. Using Command-line: Sensitive data is recommended to be stored in *.tfvars file that will be git ignored.
b. From Terraform Cloud: we can add a variable in a secured manner. let’s have a look at it
Now on the terraform cloud graphics, click Configure variables. In this demo, we will pass the credentials of AWS (Access key and secret key) to authenticate with users.
Click on + Add variable and provide the following details. Make sure you enable the sensitive check box.
- Once variables are added, it should look like this:Apply the files.
- Click on Queue plan to plan and apply the workspace. Provide the reason and click Queue plan.
Every operation will perform (be it Either apply or destroy). Terraform Cloud performs a plan before it. Once the Plan is completed, it asks for approval before performing the actual operation.
Now, you will notice that the Plan and Apply operation is scheduled. The terminal output is shown on the black screen.
Once Plan is successful, scroll down a bit, and it will wait for the confirmation/approval to apply the changes. Click Confirm & Apply.
Provide a message in the textbox and click on Confirm Plan.
You will see that terraform apply is happening.
4. Terminate the resources
- On the settings tab, click on Destruction and Deletion
Now, click on Queue destroy plan.
Provide the Workspace name and click on Queue Destroy plan
- You will notice that the queue is scheduled. The deletion queue has two stages. Plan and apply (to delete the infra)
Approve to start the delete operation
I hope that you have learned how to integrate the VCS to the Terraform cloud. We have seen how to manage the infrastructure easily from the graphics. Hope that this blog helps to get started with Terraform cloud. Follow for more updates on DevOps related information.
Drop your queries in the comments section below.