0% found this document useful (0 votes)
43 views

Terraform Basic Architecture

Uploaded by

Youness Hamdaoui
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)
43 views

Terraform Basic Architecture

Uploaded by

Youness Hamdaoui
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
You are on page 1/ 7

Learning Objectives

Learners will be able to…

Define Terraform and understand its usage and use-cases


Install Terraform
Understand Terraform providers and resources
Write initial Terraform config

info

Make Sure You Know


JSON and how it’s used
## Limitations
In this course, we will only use AWS instances with Terraform
What is Terraform?

Terraform
Terraform is an open-source, infrastructure as code (IaC), software tool
created by HashiCorp.

As an IaC tool, Terraform lets users:

define, change and manage data center infrastructure on multiple


cloud platforms
use a human-readable configuration language instead of a GUI
commit configurations to version control to track and/or reuse
securely collaborate with team members in a stable environment

Terraform uses a declarative configuration language, meaning that it


allows users to describe the desired state of the cloud resources.

Here’s an example of the configuration file, where a user wants 3 servers


to be deployed:

tex -hide-clipboard resource "aws_instance" "web-servers" { ami


= "ami-abcd" instance_type = "t3.micro" count = 3 }

All one needs to do, is describe the type of resource needed and its count.

Terraform will analyze the state of all the resources it manages, and
execute the required actions and API calls to set the cloud resources to the
desired state.

Why do we need Terraform?


Terraform provides clarity with respect to infrastructure and cloud
configuration. Amongst other things, it lets users:

spin up the same configuration for testing and staging environments.


synchronize changes making sure that testing environments are
similar to production.
track changes throughout deployments for review processes.

Terraform also makes infrastructure knowledge transfer very easy, and


improves security by limiting access for manual actions.
Providers, Resources and
Terraform State
Let’s go over some fundamentals of Terraform that we’ll expand on
throughout this course.

1. Providers

Terraform interacts with over 1000 cloud platforms and services using
their APIs. These interactions are made possible by Terraform plugins,
which are called providers.

Providers are specified in the Terraform configuration code, which specify


the services it needs to interact with.

Once specified, each provider shows all the different resources and
data types available for use.

2. Resources

Resources are the most important elements in the Terraform language.


They’re defined as blocks of code, called resource blocks.

Each resource block can define one or more infrastructure objects, for
example compute instances or private networks. Resources can be
composed of objects from different providers as well.

3. Terraform State

Terraform records and logs information about all the resources it creates
and manages in a file, called the state file.

This state file acts as a source of truth for Terraform, and maps the objects
in its configuration to the real world (remote instances and infrastructure).

It uses this state file and determines all the changes required to align the
infrastructure with the declared configuration.
Install Terraform
Terraform is written in the Go programming language and doesn’t have
any special dependencies.

It can be installed on any Linux, Windows or Mac machine and can be


used in CI/CD to apply infrastructure changes automatically.

Getting started

info

Note
Terraform has been pre-installed in this course.

Installing from binary

Terraform is distributed as a binary package and can be installed using the


popular package managers (eg. Homebrew, Chocolatey, etc.).

To install Terraform on your own machine, you can follow the instructions
on the Terraform website and download the relevant version -
https://developer.hashicorp.com/terraform/downloads

Here’s an example on how to download the Terraform binary on a Linux


machine from the terminal:

wget
https://releases.hashicorp.com/terraform/1.3.4/terraform_1.3.4_l
inux_amd64.zip
unzip terraform_1.3.4_linux_amd64.zip
chmod +x terraform

Installing using tfenv

tfenv is an open-source version management tool for Terraform. It can be


used to:

easily install any version of Terraform


update Terraform from one version to another
manage multiple environments with different versions of Terraform

You can check out the tfenv Github repo for more information on its usage.

info

Note
We used tfenv to install Terraform in this course!

You can check the installed version of Terraform by pasting the following
command in the terminal on the left:

terraform --version
Let’s create and initialize our first
configuration!
We’ll begin by defining an AWS EC2 instance.

info

Note
We will use this AWS EC2 instance to run our demo app for this course.
Click here for more information on EC2.

You should see a file called main.tf in the top-left panel. This file will
contain the main set of configuration.

In order to define an EC2 instance by AWS (the provider), only 2


parameters are required:

ami - an AMI id for the instance


instance_type - the size of the instance.

Config Details

info

The AMI ID used in this configuration:


ami-0ff8a91507f77f867 is specific to the
us-east-1 region.

t2.micro - instance type with 1 vCPU and 1 GiB of memory

Initializing

First, we need to initialize the state and check all dependencies. This is
done by running terraform init in the terminal.

Let’s try it by pressing this button:

Running this command will also -

check version compatibility between modules, providers and code.


download all used plugins and modules to the .terraform module
important

Note
The .terraform module should usually be excluded from version
control.

Use terraform init or


terraform init -upgrade to update all components to their latest
versions.

Locks

You should also be able to see the .terraform.lock.hcl file in the file tree. It
locks the dependencies to the exact versions (hashes) for components in the
configuration.

Terraform will always reselect versions of components it uses from the lock
file unless -upgrade is specified for the init command.

If a particular provider has no existing recorded selection, then the latest


version that matches the given version constraint will be selected and
added in the lock file.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy