DevOps and Kubernetes Fundamentals - Infrastructure As Code With Terraform
DevOps and Kubernetes Fundamentals - Infrastructure As Code With Terraform
DevOps and Kubernetes Fundamentals - Infrastructure As Code With Terraform
Fundamentals
TRAINING
Lecturer:
Mehmetali Shaqiri
CTO @ Appstellar
+
Who am I?
Mehmetali Shaqiri
CTO & Founder @ Appstellar
DEVOPS SERVICES
DEVOPS CONSULTING AND
CLOUD SOFTWARE IMPLEMENTATION
DEVELOPMENT KUBERNETES ENABLEMENT
AWS CLOUD CONSULTING
SERVERLESS SOLUTIONS
CONTAINERIZED SOLUTIONS
Infrastructure as Code
with Terraform
Get started with Terraform
Intro: Let's get things started
4
Today's
8 Modules
9 State
Agenda 10 Resources
2 Infrastructure as Code
12 Variables and Outputs
3 What Is Terraform?
13 Input Variables
4 The Problem?
14 Terraform Lifecycle
5 Major Components
15 Practicing time 👨💻
6 Core Concepts
16 Installing Terraform
7 Providers
17 Hands-on Labs
2. Infrastructure as Code
12
2. INFRASTRUCTURE AS CODE
Types of IaC
13
2. INFRASTRUCTURE AS CODE
Immutable infrastructure
Main use:
to create a custom image of a virtual machine or container
14
2. INFRASTRUCTURE AS CODE
Version Control
Idempotent
Main use:
to install and manage software on existing infrastructure resources
15
2. INFRASTRUCTURE AS CODE
Multiple providers
Main use:
to provision infrastructure components using a simple declarative
code
16
3. What is Terraform?
Terraform is an open-source infrastructure as Code tool developed by HashiCorp. It is used to
define and provision the complete infrastructure using an easy-to-learn declarative language.
17
3. WHAT IS TERRAFORM?
18
3, WHAT IS TERRAFORM?
19
4. The problem?
Day 1 Challenge Day 2+ Challenge
Our day 1 challenge - where we haven’t On day 2, we have an existing set of infrastructure which
actually started running anything yet. So, we’re trying to evolve. We’re changing it over time, we’re
how do we go from running nothing to adding new services, we’re removing services, and generally
running something?
evolvingthe way our infrastructure works
Day 1 Day 2+ /N
VM
VPC LB
TF Config. SG
20
4. THE PROBLEM?
Refresh
TF View ↔ Real World
Plan Real
World ↔ Desired Config
Apply
Plan ↔ Real World
21
4.1. HOW DOES TERRAFORM REALLY SOLVE THIS PROBLEM?
MONITOR CDN
Day 1 Day 2+ /N
VM
DNS
VPC LB
TF Config. SG
22
4.1. HOW DOES TERRAFORM REALLY SOLVE THIS PROBLEM?
23
4. THE PROBLEM?
K8s
IaaS Service DNS CDN
24
4. THE PROBLEM?
Making sure we’re only doing one run at a time, so we ensure there’s the
2 sequential application
25
5. Major Components
CORE
26
6. Core Concepts
Provider A plugin to interact with APIs of service and access its related
resources.
27
6. Core Concepts
Implemented by providers to return information on external objects to
Data Source
Terraform.
One of the stages where it applies the changes real/current state of the
Apply infrastructure in order to move to the desired state.
28
7. Providers
Terraform relies on plugins called Every resource type is implemented by
"providers" to interact with cloud a provider; without providers,
providers, SaaS providers, and other Terraform can't manage any kind of
APIs. infrastructure.
Terraform configurations must declare Terraform configurations must declare
which providers they require so that which providers they require so that
Terraform can install and use them. Terraform can install and use them.
Each provider adds a set of resource Each provider adds a set of resource
types and/or data sources that types and/or data sources that
Terraform can manage. Terraform can manage.
29
8. Modules
Modules are containers for multiple In addition to modules from the local
resources that are used together. A filesystem, Terraform can load
module consists of a collection of .tf modules from the local filesystem or a
and/or .tf.json files kept together in a public or private registry
directory. The Terraform Registry hosts a broad
Modules are the main way to package collection of publicly available free-to-
and reuse resource configurations use Terraform modules
with Terraform. Terraform Cloud and Terraform
Every Terraform configuration has at Enterprise both include a private
least one module, known as its root module registry for sharing modules
module internally within your organization.
30
9. State
Terraform must store state about your Prior to any operation, Terraform does a
managed infrastructure and configuration. refresh to update the state with the real
This state is used by Terraform to map real- infrastructure.
world resources to your configuration, While the format of the state files are just
keep track of metadata, and improve JSON, direct file editing of the state is
performance for large infrastructures. discouraged.
This state is stored by default in a local file The primary purpose of Terraform state is
named "Terraform.tfstate", but it can also to store bindings between objects in a
be stored remotely, which works better in a remote system and resource instances
team environment. declared in your configuration.
Terraform uses this local state to create
plans and make changes to your
infrastructure.
31
10. Resources
Resource Blocks documents the syntax for declaring resources.
The Meta-Arguments section documents special arguments that can be used with
every resource type, including depends_on, count, for_each, provider, and lifecycle.
resort.
32
11. Data Sources
Data sources allow Terraform to use information defined outside of Terraform, defined
by another separate Terraform configuration, or modified by functions.
33
12. Variables and Outputs
35
12.1. INPUT VARIABLES
36
12. VARIABLES AND OUTPUTS
12.2. Arguments
type This argument specifies what value types are accepted for the variable
37
12. VARIABLES AND OUTPUTS
Type constraints are created from The type constructors allow you
a mixture of type keywords and to specify complex types such as
type constructors. The supported collections:
type keywords are:
list(<TYPE>)
string set(<TYPE>)
number map(<TYPE>)
bool object({<ATTR NAME> =
<TYPE>, ... })
tuple([<TYPE>, ...])
38
12. VARIABLES AND OUTPUTS
Because the input variables of a module are part of its user interface, you can briefly
describe the purpose of each variable using the optional description argument:
39
12. VARIABLES AND OUTPUTS
Within the module that declared a variable, its value can be accessed from within
expressions as var.<NAME>, where <NAME> matches the label given in the declaration
block:
Note: Input variables are created by a variable block, but you reference them as
attributes on an object named var.
40
12. VARIABLES AND OUTPUTS
The above mechanisms for setting variables can be used together in any combination. If
the same variable is assigned multiple values, Terraform uses the last value it finds,
overriding any previous values. Note that the same variable cannot be assigned multiple
values within a single source.
Terraform loads variables in the following order, with later sources taking precedence
over earlier ones:
Environment variables Any *.auto.tfvars or *.auto.tfvars.json files,
processed in lexical order of their filenames.
The Terraform.tfvars file, if present.
The Terraform.tfvars.json file, if present. Any -var and -var-file options on the
command line, in the order they are
provided. (This includes variables set by a
Terraform Cloud workspace.)
41
13. Terraform Lifecycle
42
14. Terraform Enterprise
43
14. TERRAFORM ENTERPRISE
FEATURE BENEFITS
TEAM MANAGEMENT Multi-tenancy for separate BU. Govern separate teams with correct
permissions.
PRIVATE MODULE Pre-built sets of resources with best practices built-in; self-service
REGISTRY model of infrastructure.
44
14. TERRAFORM ENTERPRISE
Create policies as code and Eliminate provisioned resources that don't follow security,
SENTINEL enforce them on every compliance, or operational policies.
provisioning run.
SAML for SSO provides the ability Determine read and write controls for all users; easily automate
SAML
to govern user access to your and manage user control in TFE GUI.
applications.
SOURCE: HASHICORP
45
15. Terraform Best Practices
1. 2.
1. Don’t commit the .tfstate file
2. Configure a backend
46
15. Terraform Best Practices
6. 7.
6. Setup backend state locking
Execute Terraform in an automated
7. build
Manipulate state only through the
8, 8.
commands
9. Use variables (liberally) 10.
9.
47
Now let’s do some practical stuff! 👨💻
48
Installing Terraform
https://learn.hashicorp.com/tutoria
ls/terraform/install-cli
50
Verifying Terraform Installation
51
Hands-on Labs
52
Thank you
for being a stellar
crowd!