0% found this document useful (0 votes)
770 views21 pages

HashiCorp Certified Terraform Associate (003) WhizCard

The document provides an overview of key concepts for the HashiCorp Certified Terraform Associate exam, including introductions to Terraform, providers, the Terraform workflow, state, commands, variables and outputs, modules, and workspaces. It includes examples of configuration files and blocks. The information is intended to help exam aspirants prepare for the certification through educational "WhizCards" summarizing important Terraform concepts.

Uploaded by

devops8355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
770 views21 pages

HashiCorp Certified Terraform Associate (003) WhizCard

The document provides an overview of key concepts for the HashiCorp Certified Terraform Associate exam, including introductions to Terraform, providers, the Terraform workflow, state, commands, variables and outputs, modules, and workspaces. It includes examples of configuration files and blocks. The information is intended to help exam aspirants prepare for the certification through educational "WhizCards" summarizing important Terraform concepts.

Uploaded by

devops8355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Are you Ready for HashiCorp

Certified Terraform Associate


exam? Self-assess yourself with
“Whizlabs FREE TEST”

HashiCorp Certified Terraform Associate


WhizCard
Quick Bytes for you before the exam!

The information provided in WhizCards is for educational purposes only; created in


our efforts to help aspirants prepare for the HashiCorp Certified Terraform
Associate certification exam. Though references have been taken from Terraform
documentation, it’s not intended as a substitute for the official docs. The document
can be reused, reproduced, and printed in any form; ensure that appropriate
sources are credited and required permissions are received.
Index
Topic Names
Page No.
● Introduction to Terraform 3
● Terraform Providers 5
● Terraform Workflow ( init -> plan -> apply -> destroy) 6

● Terraform State, state files, state locking and remote state 7

● Terraform Backend Configuration 8

● Terraform Commands 9

● Terraform Variables and Outputs 11

● Terraform: Dependency Lock File 14

● Terraform Functions 15

● Terraform Registry 17

● Terraform Modules 18

● Terraform Workspaces and Terraform Cloud Workspaces 19

● Terraform Cloud 20
2
Introduction to Terraform
Why IaC?
What is Infrastructure as Code
(IaC)?
Automation, Version control, Scalability,
A way of managing and provisioning IT infrastructure Consistency, and reduced human error, are easily
through code and automation. Typically, in the form of implemented in CI/CD integration.
configuration files or scripts.

What is Terraform?
Well known IaC tools in the market:
● Terraform is an infrastructure as a code tool that
● HashiCorp Terraform – Well known platform agnostic allows you to define both Cloud and On-prem
IaC tool. resources in human-readable configuration files.
● AWS CloudFormation – IaC tool from AWS and can only ● HashiCorp Configuration Language (HCL) is used
be used to build AWS resources. to define the resources as a code.
● Azure Resource Manager – IaC tool from Azure and can ● Terraform is Cloud agnostic. It works with
only be used to build Azure resources. multiple cloud providers. Terraform enables
● Ansible, Chef & Puppet, etc. – Configuration developers to maintain the same workflow
management tools used to manage large groups of when provisioning resources across
computer systems. cloud/infrastructure providers.

3
Introduction to Terraform

● Below is the example of how terraform configuration looks like. ● Terraform vs Other IaC:

Terraform Other IaC tools (Ansible,


Chef, Puppet etc.)
Multi-Cloud support – Terraform Other tools are platform
can be used in any Cloud specific.
platforms and even in
On-premises.
Terraform is declarative language Procedural - you have to
- you just describe the required define step by step what to
end resource and terraform is do.
intelligent to know how to
achieve it.
● Below is the example of how resource configuration looks like. HCL – Terraform code is Other tools language varies
developed in HCL language which as per the requirement and
is easy to learn and implement. complex compared to HCL.

Agentless – Terraform doesn’t Tools like Ansible and Puppet


require any agents to be installed needs agents to be installed.
to run.

Exam Tips:
Focus on the features Terraform offers over other IaC tools.

4
Terraform Providers

● In Terraform, "providers" are plugins or ● Below is the example of AWS


provider block.
extensions that enable communication ● Providers can be Official, Partner
between Terraform and various and Community supported.
infrastructure platforms or services.
Providers uses API to interact. ● Official providers are owned and
maintained by HashiCorp.

● Partner providers are written,


● Terraform supports more than 200 maintained, validated and
Providers. published by third-party
○ Few of them are – AWS, Azure, companies against their own
Google cloud, Kubernetes etc. APIs.
● How to use Providers in your Terraform
code? ● Community providers are
○ Terraform Providers official published to the Terraform
website - Registry by individual
https://registry.terraform.io/brows maintainers, groups of
e/providers maintainers, or other members
we have to copy the providers block to of the Terraform community.
install all dependencies required to
interact to the platform.

● Exam Tips:
o Make sure to practice and remember the syntax of Providers block.
5
Terraform Workflow

● Init – “terraform init” is a command you will run to initialize your workspace so Terraform can apply your configuration.

● Plan – “terraform plan” is a command that allows you to preview the changes Terraform will make before you apply them.

● Apply – “terraform apply” is a command that creates/modifies the actual resources as shown in the plan output.

● Destroy – “terraform destroy” is a command that destroys your all resources mentioned in the code.

● Exam tips:
o ‘auto-approve’ - to apply/destroy Terraform configurations and automatically approve without user
interaction, you can use the -auto-approve flag with the terraform apply command.
▪ terraform apply -auto-approve
▪ terraform destroy -auto-approve

6
Terraform State
● State lock – Terraform locks your State file to prevent others from
● State file – It is an HCL/JSON file that Terraform manages acquiring the lock and potentially corrupting it.
automatically to record the current state of your
infrastructure. This file contains information about the ● State Backend – It determines where the State file is stored and how it is
resources that Terraform manages, their attributes, and managed.
their current configuration.
o Local Backend – This is the default Backend configuration in which
○ Example state file snippet. the State file is stored on the same machine Terraform runs.
o Remote Backend – This allows you to store your State file on a
remote server or Cloud storage service. A few of the remote
backend options are Amazon S3, Azure Blob storage, etc.

● Exam Tips:
o Remote backend is the secure way of storing your State files.
o Always make sure not to store any sensitive data in the State
file.

7
Terraform Backend Configuration
● Terraform Backend defines where and how the State file will be stored. ● Remote Backend allows multiple
people to access the State file and
● Local Backend - This is the default Backend configuration in which the State file is stored work collaboratively on the collection
on the same machine Terraform runs. of resources.

● Remote Backend - This allows you to store your State file on a remote server or Cloud
storage service. A few remote backend options are Amazon S3, Azure Blob storage, etc.

● Local Backend Configuration – This is the default Backend configuration in Terraform. If


the configuration includes no backend block, Terraform defaults to using the local
backend, which stores the state as a plain file in the current working directory.
● Remote Backend Configuration – Remote Backend can store State files in different places
such as AWS S3, Azure Blob storage, etc.

a. Below is the Configuration block to store the State file in AWS S3.

● Exam Tips: Source: terraform backend


o Remote backend is the secure way of storing
your State files.
o Always make sure not to store any sensitive
data in the State file.
8
Terraform Commands
● terraform init - Initializes the working directory, installs required plugins. ● terraform destroy – Destroys all the resources
mentioned in the Configuration file.

● terraform plan – Shows the execution plan without performing the actual
actions.
● terraform fmt – Formats your configuration to the
HCL code standards.

● terraform apply – Creates or Updates the infrastructure based on the


Configuration file.
● terraform show – Shows the State file in a human
readable format.

9
Terraform Commands
● terraform state list – Lists all the resources tracked in the current State file. ● terraform refresh – Refreshes the State file with
as per the Configuration file.

● terraform import – Imports the resource into state


file.
○ Let's say you want to import an existing AWS
S3 bucket with the name
"my-whizlabs-bucket" into your Terraform
● terraform state rm – Removes the specified resource from the State file.
configuration.
○ Below is the format and example command
used to import the bucket.

10
Terraform Variables and Outputs

● In Terraform, Variables allow you to customize your values when you


run your Terraform configuration. This feature of Terraform allows you ● Input Variables – Each input variable accepted by a
to use and share the modules without altering the source code. module must be declared using a “variable” block.

● Local Variables - Local variables are declared using the “locals” block. It o In the below example, a variable named
is a group of key-value pair that can be used in the configuration. "region" of type “string” is declared with a
default value of "ap-south-1".
o Below example shows how to use local variable block for
creating EC2 configuration.

● Below block shows how the variable will be


called in the main configuration code.

11
Terraform Variables and Outputs

● The commonly used other types of Input variables are – Number, Boolean,
List, Map. o Below is the example of variable type “map”.

o Below is the example of variable type “number”.

● Environment Variables - Input variable values can also be


o Below is the example of variable type “boolean”. set using Terraform environment variables.

o It can be done by setting the environment variable in


the format TF_VAR_<variable name>.
o The variable name part of the format is the same as
the variables declared in the variables.tf file. For
example, to set the ami variable run the below
command to set its corresponding value.
o Below is the example of variable type “list”.

12
Terraform Variables and Outputs
● Passing variables through CLI and. tfvars – In the o The variables defined in .tfvars file can be used through plan command by
above examples we have seen only default values passing the file name “terraform plan -var-file values.tfvars”.
being set.
o The default values can be overridden in two ● Precedence to variables – Terraform allocate the
ways: precedence to know in which order the passed variables
should be read.
▪ Passing the values in CLI as -var
o The precedence is shown below from first to last.
argument.
▪ cli arguments > .tfvars > default variables.
▪ Using .tfvars file to set variable values
explicitly.
● Output variables – When you deploy a large infrastructure
o Below example shows how to pass variables using Terraform you would be required to read the values
through CLI. of some resources in other modules or will be required to
print the values of provisioned resources and in that
scenario, we will make use of “output” variable. This
should be defined as a separate file “outputs.tf”.
o Below example shows how to pass
variables through .tfvars file. o Below is the example block code how to use
“output” variable.

13
Terraform: Dependency Lock File

● Terraform 0.14 and above have introduced a feature that tracks dependency selections of your code.

● This Dependency lock file will be named “.terraform.lock.hcl”.

● Terraform recommends including this file in your source code repository so that any changes to dependencies can be identified and updated.

● Dependency lock file can be updated by running “terraform init” command.

14
Terraform Functions
● Terraform functions are built-in, reusable code blocks o Numeric Functions – This allows you to perform
that perform specific tasks within Terraform various Numeric operations such as min, max,
configurations. Functions make your code more dynamic. floor, ceiling, etc.

o Syntax example: ▪ Example code for “min” and “max”


Numeric function.

● Terraform as of now supports only built-in Functions. You cannot define your
own Functions.

● Let’s see different types of Terraform Functions and their example syntax.

o String Functions – This allows you to perform various String operations o Collection Functions – This allows you to
such as format, indent, join, lower, split, etc. perform various Collection operations such as
lookup, list, map, concat and etc.
▪ Example code for “join” String function.
▪ Example code for “lookup” Collection
function.

15
Terraform Functions
● Filesystem Functions – This allows you to perform various
Filesystem operations such as dirname, basename, file, o Type Conversion Functions – This allows
fileexists and etc. you to perform various Type Conversion
operations such as tobool, tolist, tomap,
o Example code for “file” Filesystem function. tonumber and etc.

▪ Example code for “tolist” Type


Conversion function.

● Date and Time Functions – This allows you to perform


various Date and Time operations such as formatdate,
timeadd, timestamp and etc.

o Example code for “formatdate” Date and Time


function.

16
Terraform Registry

● Terraform Registry is a central repository for sharing, discovering, and using Terraform modules and provider plugins.

● Terraform's official registry website is found here https://registry.terraform.io/.

● Terraform Registry serves different purposes:

o Module sharing: Terraform Registry allows you to share your Terraform code in the form of Terraform modules.
These modules can be reused by others to provision common infrastructure components.

o Provider documentation: Terraform providers are plugins that allow Terraform to interact with specific cloud and
infrastructure platforms. Terraform Registry provides documentation and examples for such Terraform providers.

o Collaboration: Terraform Registry allows collaboration between multiple users and organizations to contribute
and maintain modules and providers.

17
Terraform Modules

● Sources - Terraform modules can be


What is a Terraform Module?
stored either locally or remotely. The
`source` argument will change
Terraform Module is a Container that holds
depending on their location.
multiple resource configuration files (.tf)
and can be reused.
● Version - Terraform modules can be
versioned to manage the lifecycle and
consistency of the modules you use.

Module usage – ● Root module – The root module is the


Module “my-module” entry point for your Terraform
defined above can be configuration.
referred/used like It's the main directory where your
below. main Terraform files, normally named
main.tf, variables.tf, and outputs.tf, are
Module structure: located.

● Child module - Child modules are


● Benefits of Terraform module: reusable units of Terraform code that
o Reusability. can be called from the root module or
o Keep the code DRY as much as other child modules.
possible. Each child module can define its own
o Modules can be versioned to set of variables, resources, and outputs.
maintain consistency.
o Collaboration across teams.
18
Terraform Workspaces
● To check the current Workspace.
● Terraform Workspaces allows you to manage resources in multiple
environments using a single Terraform configuration code.

● For example, if you want to create an EC2 instance in 3 different


environments say Dev, PPE, and Prod. Then you have to create
three different Workspaces and switch between them and can use
the same code to deploy EC2 in all 3 environments. ● To create a new Workspace and switch
to it.

● Below is the code example of how you can use the same code and deploy ● To list existing Workspaces.
the different types of EC2 instances based on the Workspace you are in.
o The below code provisions “m4.large” EC2 if you are in the “prod”
workspace and provisions “t2.micro” if you are in any other
workspace.

Terraform Cloud Workspaces are nothing but Workspaces that will be


created in Terraform Cloud rather than in your local machine.
19
Terraform Cloud

● Terraform vs Terraform Cloud – Terraform is an open-source ● Terraform Cloud comes with a private registry that allows you to
platform that you need not pay anything for. Whereas, if you share your organization's Terraform modules and providers.
want to have extended features of Terraform such as Security,
CI/CD, SSO, etc. you can make use of the Terraform Cloud and
this comes with a charge. ● Terraform Cloud comes with below features:

● Terraform Cloud is a platform that helps manage your o Variables and Variable Set - Terraform Cloud stores your
Terraform code in the Cloud rather than on your local machine. variables securely, encrypting them at rest. Variable
This allows teams to use Terraform together. Sets,help you reuse variables in multiple workspaces,
without having to declare them multiple times.

o Remote State – Terraform Cloud comes with a feature to


store your State file remotely and securely. This feature
allows your team members to access the State file and work
collaboratively.

o Policies - With Terraform Cloud, you get Sentinel policies to


enforce security practices and governance throughout your
workflow.

Image source: Terraform official document

20
Terraform Cloud
o Single Sign-On - Terraform Cloud enables you to achieve SSO by using an identity provider such as Okta, SAML, or Microsoft Azure AD.

o Private Registry - Private registry is feature for hosting your Terraform modules and providers.

o Drift detection - Terraform Cloud comes with a feature that detects the drift in your Infrastructure. Drift you will be notified to you, and
you will be able to quickly fix the issue.

● Terraform Pricing – The details of Terraform Cloud pricing can be found here at the official Terraform website
https://www.hashicorp.com/products/terraform/pricing.

Image source: Terraform official document


21

You might also like