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:
- Github Account
- AWS Account ( Basic understanding about How to create new IAM User and assign permissions)
- Basic knowledge about Terraform
Step 1: Create Terraform Cloud Account
As of now, Terraform is offering below pricing models for account
- Free
- Team & Governance
- 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
- Enter organization name and email id
- Click on “Create organization”
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
- For that, Navigate to Settings > VCS Providers
- Click on “Add a VPC provider”
- Select GitHub > Github.com (Custom)
Now let’s follow the steps mentioned on the “Set up provider” page.
Goto > On GitHub, register a new OAuth Application. Refer this link https://github.com/settings/applications/new
Populate Application Name, Homepage URL, and Authorization callback URL as given by terraform. Click on “Register Application”.
- 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
Click on “Connect and Continue”
Authorize Terraform Cloud (your_application_name) on github.com
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
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
- Go back to Terraform and navigate to the “Workspaces” Tab.
- Click on “New Workspace”
- Choose your workflow = Version control workflow
- Connect to a version control provider = Github
- Choose a repository =
<your_repository name>
in this case, it would be > claim-demo-app-dev - Click on “Create Workspace”
Wait for the configuration check, once that is done you would be provided this message “Configuration uploaded successfully”
Step 7: Configure Variables
let’s add below AWS environment variables so that we can spin up some EC2 instances.
name | value |
---|---|
AWS_ACCESS_KEY_ID | AKIA5CC5BLBOFG2SFAKBNN |
AWS_SECRET_ACCESS_KEY | WIkBoRFVETGFBQ9KHd7Hj567jibzTQDhJY6/CVYG4OFhZMFv2H |
AWS_DEFAULT_REGION | us-east-1 |
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.
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.
Go to github.com > [[you_repo]]
Create a file with the below code. Make sure that the file extension is
.tf
Go back to terraform > workspaces > [[select your workspace]] in this case = claim-demo-app-dev
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.
When the job at “NEEDS CONFIRMATION”, check all the infrastructure configuration and Click on “Confirm and Apply”
Step 9: Validate EC2 Instance
Once the above step is completed, login to the AWS account and verify EC2.
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.
So go ahead and try some.
Step 10: Destroy created infrastructure
To destroy infrastructure, navigate to workspace > app > Settings > Destruction and Deletion
Click on “Queue destroy plan” button
Enter the workspace and confirm