Skip to content

Instantly share code, notes, and snippets.

@isaacarnault
Last active October 12, 2020 00:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacarnault/ed11132744fb3a1d554985477ef6cdcd to your computer and use it in GitHub Desktop.
Save isaacarnault/ed11132744fb3a1d554985477ef6cdcd to your computer and use it in GitHub Desktop.
Get started with Terraform Cloud using GitHub and AWS.
________ ________ ___ __ ___
|\_____ \|\ __ \|\ \|\ \ |\ \
\|___/ /\ \ \|\ \ \ \/ /|\ \ \
/ / /\ \ __ \ \ ___ \ \ \
/ /_/__\ \ \ \ \ \ \\ \ \ \ \
|\________\ \__\ \__\ \__\\ \__\ \__\
\|_______|\|__|\|__|\|__| \|__|\|__|
ignore Azure, GCP

isaac-arnault-terraform-12.jpg

β€’ Programming: HCL
β€’ Repository: GitHub
β€’ Cloud: Terraform Cloud (app.terraform.io), AWS
β€’ Database: DynamoDB

Gist writing, testing and debugging : 12 hrs

Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.

The following gist is intended to DevOps Engineers and Data Architects and is part of my cloud series.
It will help you start with Terraform Cloud using GitHub and AWS.

Thanks for forking this gist if you find it useful.

How this gist is structured

This gist is structured into 6 parts.

Part 1. Get started with Terraform Cloud UI
Part 2. Plan an apply a configuration
Part 3. Update variables
Part 4. Cost estimation and mitigation
Part 5. Sentinel Policies and Policy Sets.md Part 6. Destroy Infrastructure

Terraform is a freemium Infrastructure as Code tool which can help you deploy IT infrastructures from configuration files rather than interracting with your tools UIs.

Terraform is mainly dedicated to CI/CD projects and to DevOps Engineers, Cloud Engineers, Data Architects.

You can use Terraform locally in your Shell or in the Cloud (scope of this gist). Before diving deeper into Terraform, I recommend having prior knowledge of programming in any language, or in Cloud computing.

| Some useful resources
. https://www.terraform.io/docs/cloud/index.html
. https://www.terraform.io/docs/cloud/paid.html

Related tags

β€’ AWS
β€’ Terraform
β€’ Terraform Cloud
β€’ Terraform Cloud UI
MIT License
Copyright (c) 2020 Isaac Arnault
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

This gist will help you get started with Terraform Cloud.

Prerequisites

You must have a github account. If not, create a Github account for free: https://github.com/join.
Fork this repository: https://github.com/hashicorp/tfc-guide-example

Once you've completed the prerequisites, you are ready to take this part.

Get started

Create an account account..

πŸ”΅ See output

isaac-arnault-terraform-49.png

Log into your account and create new organization: mine is called Company123 (name must be a unique name).

πŸ”΅ See output

compay123.jpg

Create a new Workspace > Version control workflow.

Connect to VCS: choose Github since you now have a Github account.

πŸ”΅ See output

Screenshot-from-2020-09-12-15-34-57.jpg

Authorize Terraform CLoud.

πŸ”΅ See output

isaac-arnault-terraform-24.png

. Install on a specific repository: choose the repository you've forked (seach for 'tfc', then 'Create workspace')

πŸ”΅ See output

isaac-arnault-terraform-31.jpg

. Wait until the configuration is uploaded.

πŸ”΅ See output

isaac-arnault-terraform-cloud.png

Once you've completed the Part 1 of this gist, you are ready to begin this part.

In this section, we will provision a DynamoDB instance through Terraform Cloud.

Step 1 - Configure AWS (must have an AWS account)

We will retrieve our AWS credentials and set our Terraform Cloud variables to use those values.

Log into your AWS account. If you don't have an account, create one: https://portal.aws.amazon.com/billing/signup#/start.
You can access your console after Signing Up to AWS using this link: https://console.aws.amazon.com/console/home.

Once logged into your AWS console, go to IAM.
If you are new to AWS, please bypass all user configuration (green ticked marks below) for the sake of simplicity.

πŸ”΅ See output

isaac-arnault-aws-1.png

. Click on Users > Add user

. Next: permissions > Create group: call it admins_terraform.

. Filter policies: search for AdministratorAccess in the search bar.

. Select the AdministratorAccess policy and click on 'Create group'.

πŸ”΅ See output

isaac-arnault-aws-3.png

. Next: tags. Use as key: resources and as Value: terraform

. Next: review > create user > download the .csv file which contains security credentials for the user ' terraform_user'

Access key ID and a Secret access key were provided to you by AWS.

Be sure to keep these values in a secure location; you will use them in the next step.

You can keep this window opened and open a new AWS console window from your browser.


Important Follow `AWS` security best practices by deleting this user and the group created after completing this gist, since all security parameters for this user weren't fulfilled for the sake of simplicity.

Step 2 - Configure workspace variables

Go back to your Terraform Cloud window and click on 'Configure variables'

. Scroll down to Terraform variables > Add variables: here you'll create three (3) variables, one related to a user, and two related to the DynamoDB database: 'tag_user_name', 'db_write_capacity', 'db_read_capacity'.

. Do the same for Environment variables > Ass variables: create 2 variables 'AWS_ACCESS_KEY_ID' and 'AWS_SECRET_ACCESS_KEY'.

πŸ”΅ See output

isaac-arnault-aws-34.png

Step 3 - Queue and apply plan

. Click on Queue plan. When prompted, click on 'Confirm & Apply'.

πŸ”΄ See hint

isaac-arnault-terraform-34.png

If everything is set up correctly, you should have this:

πŸ”΅ See output

isaac-arnault-terraform-35.png

If you get an error, make sure you've followed the prerequisites (Get_Started section of this gist) and that you've set the 5 requested variables correctly.

You can go to your AWS console and check that the DynamoDB table was provisioned successfully.

πŸ”΅ See output

isaac-arnault-terraform-38.png


At the end of this part, you've configured your workspace and provisioned a `DynamoDB` instance using `Terraform Cloud`.

Step 1 - Change Infrastructure via Version Control

There are two (2) ways to update our workspace deployments on Terraform Cloud:

  • change the configuration in VCS.
    or
  • update variables in the Terraform Cloud UI.

Important - tools of our Workspace

Runs - shows a list of all of the plan and apply actions you have taken with this workspace.
States - shows a list of the entire tfstate file of your workspace after each successful run.
Variables - let you configure Terraform variables and environment variables.
Settings - contain all of the other configuration for your workspace.
Queue plan - lets you start a new plan.


Step 2 - Changing variables

In your Terraform Cloud UI, go to 'Variables'.
You can try changing your 'db_read_capacity' value from 2 to 1.

πŸ”΅ See output

isaac-arnault-terraform-37.png

Click on 'Queue plan' and you can set as reason: 'db_read_capacity update' (not mendatory).

πŸ”΄ See hint

Screenshot-from-2020-09-12-18-49-53.jpg

You'll notice once queuing the plan that the log indicates that there are "0 to add, 1 to change, 0 to destroy", which corresponds to your update.

πŸ”΅ See output

isaac-arnault-terraform-38.png

Then 'Confirm & update' and wait for the update confirmation:

πŸ”΅ See output

isaac-arnault-terraform-40.png

Estimating and mitigating costs of our AWS deployed resources using Terraform Cloud can be useful in our daily job.

Prerequisites

In order to get the cost estimation of our deployed resources upon Queue plan, we make sure we move from a free plan to a paid plan.

On the top bar click on Settings > Plan & Billing.

Choose the Team & Governance free trial plan to try new Terraform Cloud tools for free (30 days free trial).


isaac-arnault-terraform-51.png


Now that we've switched to a paid plan, we should see that new tabs appeared in our left sidebar such as Cost Estimation, Policy Sets.

Click on Cost Estimation and make sure that Enable Cost Estimation for all workspaces is checked.

πŸ”΅ See output

isaac-arnault-terraform-52.png

Now we are ready to go.

Infrastructure cost mitigation using Terraform Cloud UI

Let's go back to our Terraform Cloud UI on our tfc-guide-example workspace to add new parameters.

Go to Terraform variables.

Add 1 new variable: aws_region and set us-west-1 as value.

Update db_read_capacity to 2.

πŸ”΄ See hint

isaac-arnault-terraform-53.png

Then click on Queue plan.

Applying our new plan will unlock a new Cost estimation feature in our UI.


isaac-arnault-terraform-54.png


By clicking on the Cost estimation tab we can see:

  • Hourly
  • Monthly
  • Monthly Delta costs of our DynamoDB table deployed on AWS.

isaac-arnault-aws-55.png


Finally, let's confirm that our DynamoDB was deployed on AWS:

  • Log into our AWS console.
  • Switch to us-west-1 region.
  • Open DynamoDB service.

Here is our table!

πŸ”΅ See output

isaac-arnault-aws-56.png


To mitigate our costs, we shouldn't forget to delete the DynamoDB table created, directly throughout our AWS console.

πŸ”΅ See output

isaac-arnault-terraform-55.png

Doing part 5 is not mandatory.

But if we want to take advantage of your Team & Governance plan trial plan, we can set a Sentinel policy and a new Policy set.


Step 1 - Create a new Sentinel Policy and Policy Set

We connect to our Terraform Cloud UI, click on Settings, click on Policy Sets.

Now we will add a new Sentinel Policy. Click on Policies.

Create a new policy > call it tfc-guide-example-sentinel-policy

Description (not compulsory): 'First sentinel policy'.

Enforcement mode: choose Soft-mandatory

Policy code: add the following line of code

main = rule {
	true
}
  • click on Create your first policy set
  • connect to GitHub
  • choose a repository: choose tfc-guide-example
πŸ”΄ See hint

isaac-arnault-terraform-31.jpg

Scope of policies > select Policies enforced on selected workspaces.

In the Workspaces box, select tfc-guide-example workspace, then Add workspace.

Next: click on connect policy set

πŸ”΄ See hint

isaac-arnault-terraform-50.png


Step 2 - Update GitHub repository

Now that we have a new Sentinel policy and a Policy Set, we should add one file into our GitHub repository otherwise we'll get some error while queueing our plan.

We go to tfc-guide-example repository that we've forked.

Click on Add file, then Create new file

πŸ”΄ See hint

isaac-arnault-terraform-53.jpg

Call the file allowed-terraform-version.sentinel and pass the following code onto it, then click on Commit

main = rule {
	true
}

Our newly created file should look like this:

πŸ”΅ See output

isaac-arnault-terraform-57.png

We have one Sentinel policy and one Policy Set configured as well as a new file created in our GitHub repository.

We are now ready to run a 'Queue plan'.


Step 3 - Queue plan to apply your policies

We go to our tfc-guide-example workspace and click on 'Queue plan'.

Upon completion, a new tab Policy check should appear in our UI.

If we have properly configured our Sentinel policy and Policy Set as well as our repository file, Policy check should pass.


isaac-arnault-terraform-55.png

Destroying the infrastructure is part of Terraform normal workflow.

Destroy infrastructure

Go to your workspace in the Terraform Cloud UI.
From the top menu select "Settings -> Destruction and Deletion".

πŸ”΅ See output

isaac-arnault-terraform-31.png

. Queue destroy plan:

πŸ”΅ See output

isaac-arnault-terraform-32.png

Confirm and apply when prompted and wait for the destruction:

πŸ”΅ See output

isaac-arnault-33.png

You'll notice that you've destroyed the plan but your workspace is still alive.

πŸ”΅ See output

isaac-arnault-terraform-34.png

Go back to 'Settings' and proceed to the workspace deletion.

πŸ”΅ See output

isaac-arnault-terraform-35.png

Confirm Workspace deletion when prompted and wait for the destruction.

πŸ”΅ See output

isaac-arnault-terraform-36.png

In your Organization's workspace, you'll see that your tf-guide-example workspace was deleted.

πŸ”΅ See output

Selection-061.png


Important

Deleting a workspace does not destroy infrastructure that has been provisioned by that workspace.

This means that your AWS DynamoDB table you provisioned isn't destroyed after destroying the plan and infrastructure on Terraform Cloud.

Mitigating the cost of the Infrastructure, especially if you are a Data Architect / Solution Architect is quite important.

Please bear in mind that running costs may occure if you do not delete the resources provided by your Terraform actions.


Thus, feel free to go to your `AWS` console and delete the User, Group, DynamoDB table created upon this gist completion.
This gist is now completed. We have successfully completed parts 1 to 6.

isaac-arnault-terraform-56.png


We have learnt how to implement an Infrastructure as Code using Terraform Cloud and GitHub which helped us provision a DynamoDB table on AWS.
If you enjoyed this gist, thanks for forking it and do not hesitate to ask questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment