Contents

Tutorial - Setting up Terraform Cloud with Github


Tutorial - Setting up Terraform Cloud with Github


In this tutorial, we are going to configure a Terraform Cloud account with Github and will be creating a few EC2 instances. This tutorial is not to explain how terraform works or its concepts. This is just to show you how terraform cloud can be configured with Github.

Pre-requisite:

  1. Github Account
  2. AWS Account ( Basic understanding about How to create new IAM User and assign permissions)
  3. Basic knowledge about Terraform

Step 1: Create Terraform Cloud Account

As of now, Terraform is offering below pricing models for account

  1. Free
  2. Team & Governance
  3. Business

In this tutorial, we will be using a Free account.

Create a new account using this link - https://app.terraform.io/signup/account. Once the account is created, confirm the provided email address.

On the welcome screen, click on “Not right now, skip questions”.

Step 2: Create a new organization

Organizations are common space for the teams to collaborate on infrastructure. Refer this link to learn more about organization https://www.terraform.io/docs/cloud/users-teams-organizations/organizations.html

  1. Enter organization name and email id
  2. Click on “Create organization”

/terraform-cloud-setup-with-git/terraform-organization.jpg

Step 3: Create a new Workspace

Once an organization is created, you will be navigated to the next step that is “Create a new Workspace”.

if you are familiar with terraform workspaces then you might know that we can create workspaces to organizes cloud infrastructures. For example, You can create a separate cloud infrastructure for Development, Test, and Prod environment.

below workflows can be used to create workspaces,

  • Version Control such as Github, Gitlab, BitBucket and Azure DevOps

  • CLI Driven

  • API Driven

before creating a new workspace, let’s configure a VCS provider.

Step 4: Configure a VCS provider

/terraform-cloud-setup-with-git/terraform-vcs-provider.jpg

  1. For that, Navigate to Settings > VCS Providers
  2. Click on “Add a VPC provider”
  3. Select GitHub > Github.com (Custom)

Now let’s follow the steps mentioned on the “Set up provider” page.

/terraform-cloud-setup-with-git/terraform-setup-github.jpg

  1. Goto > On GitHub, register a new OAuth Application. Refer this link https://github.com/settings/applications/new

  2. Populate Application Name, Homepage URL, and Authorization callback URL as given by terraform. Click on “Register Application”.

/terraform-cloud-setup-with-git/github-register-app.jpg

  1. Once the application is registered on github.com. you will be provided Client ID and Secret Key. Go back to Terrform page and enter Name as “Github.com”. Populate Client ID and Secret key provided by github.com

/terraform-cloud-setup-with-git/github-client-deatils.jpg

  1. Click on “Connect and Continue”

  2. Authorize Terraform Cloud (your_application_name) on github.com

/terraform-cloud-setup-with-git/github-authorize-app.jpg

You will be navigated to the “Set up SSH keypair (optional)” page, lets skip this step as it is optional.

Step 5: Create a GitHub repo

Create a GitHub repository. This repo will be used to save all terraform configurations files for Development env.

For this demo, we will using this repository https://github.com/ahireharshal/claim-demo-app-dev

/terraform-cloud-setup-with-git/sample-github-repo.jpg

As of now, keep the repository empty. In the next steps, we will add some terraform infrastructure files.

Step 6: Continue step 3 > create workspace

  1. Go back to Terraform and navigate to the “Workspaces” Tab.
  2. Click on “New Workspace”
  3. Choose your workflow = Version control workflow
  4. Connect to a version control provider = Github
  5. Choose a repository = <your_repository name> in this case, it would be > claim-demo-app-dev
  6. Click on “Create Workspace”

Wait for the configuration check, once that is done you would be provided this message “Configuration uploaded successfully”

/terraform-cloud-setup-with-git/terraform-workspace-created.jpg

Step 7: Configure Variables

let’s add below AWS environment variables so that we can spin up some EC2 instances.

namevalue
AWS_ACCESS_KEY_IDAKIA5CC5BLBOFG2SFAKBNN
AWS_SECRET_ACCESS_KEYWIkBoRFVETGFBQ9KHd7Hj567jibzTQDhJY6/CVYG4OFhZMFv2H
AWS_DEFAULT_REGIONus-east-1

/terraform-cloud-setup-with-git/terraform-environment-variables.jpg

Note: To get aws_access and secret_key, Login to AWS Account.

Go to IAM > users > [[your_user_name]] > Security Credentials

IMPORTANT: Make sure that IAM user has Programmatic access and AdministratorAccess permissions.

/terraform-cloud-setup-with-git/aws-iam-page.jpg

Step 8: Spin up some EC2 instances

So far we have configured GitHub and created a workspace for our development environment. Now let’s create some EC2 instance using terraform.

  1. Go to github.com > [[you_repo]]

  2. Create a file with the below code. Make sure that the file extension is.tf

  1. Go back to terraform > workspaces > [[select your workspace]] in this case = claim-demo-app-dev

  2. Click on the “Queue plan” list and enter some comment and click “Queue Plan”. A new job would be triggered once you click on the “Queue plan” button.

  3. When the job at “NEEDS CONFIRMATION”, check all the infrastructure configuration and Click on “Confirm and Apply”

/terraform-cloud-setup-with-git/terraform-app-confirm.jpg

Step 9: Validate EC2 Instance

Once the above step is completed, login to the AWS account and verify EC2.

/terraform-cloud-setup-with-git/terraform-job-completed.jpg

/terraform-cloud-setup-with-git/aws-ec2.jpg

Now whenever GitHub code is updated, a new job would be triggered at terraform automatically. Those jobs can be viewed from the Workspaces > App > Runs tab.

/terraform-cloud-setup-with-git/terraform-run-tab.jpg

So go ahead and try some.

Step 10: Destroy created infrastructure

  1. To destroy infrastructure, navigate to workspace > app > Settings > Destruction and Deletion

  2. Click on “Queue destroy plan” button

  3. Enter the workspace and confirm




Conclude:
I hope you liked this blog and got a good understanding of Terraform Cloud Setup with Github. If you are facing any issue while implementing this then reach out to me or refer this terraform troubleshooting page. https://registry.terraform.io/providers/hashicorp/oci/latest/docs/guides/troubleshooting