Step-by-Step Guide to Manage Infrastructure on AWS using Terraform Cloud for FREE.

December 3, 2020 | Comments(1) |

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.

Prerequisites:

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.  

    1
    2

    Let us create an organization to manage workspaces. Once the account is verified, you will land on the page to create an organization. Provide appropriate name and click Create Organization.
    3

    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)
    4

    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.

    5

    • 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. 6In 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. 7

    8

    3. Plan and Apply the changes:

Configure variables

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:

instance.tf

9
vars.tf
10

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.

Table

  • Once variables are added, it should look like this:11Apply 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.

    13

    Now, you will notice that the Plan and Apply operation is scheduled. The terminal output is shown on the black screen.

    14
    Once Plan is successful, scroll down a bit, and it will wait for the confirmation/approval to apply the changes. Click Confirm & Apply.
    15

    Provide a message in the textbox and click on Confirm Plan.
    15
    You will see that terraform apply is happening.

    17
    On successful execution, you will see:

    18
    You can also see the state by clicking on the ‘States’ tab on the top
    19

    Verify that the resource has been created in your AWS Console
    20

    4. Terminate the resources

    • On the settings tab, click on Destruction and Deletion
      21
       Now, click on Queue destroy plan.22
      Provide the Workspace name and click on Queue Destroy plan23
    • You will notice that the queue is scheduled. The deletion queue has two stages. Plan and apply (to delete the infra)2424
      Approve to start the delete operation25
    • Once completed, check the run tab to check the status.27
    • Check the AWS console to verify that the resources are no longer active.

Conclusion:

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.


One Response to “Step-by-Step Guide to Manage Infrastructure on AWS using Terraform Cloud for FREE.”

Leave a Reply