2. TERRAFORM
A. Introduction:
• What is Terrafrom
• Why use Terraform
• Providers
B. Installation & Setting up Lab
• Installing Terraform – Windows users
• Installing Terraform – Linux users
• Setting up AWS Account
C. Deploying Infrastructure with
Terraform
• Creating First EC2 Instance with Terraform
• Understanding Resources and Providers
• Destroying Infrastructure with Terraform
• Terraform state
3. D. Interpolation, Attributes & Variables:
• Attributes and Output Values
• Referencing Cross-Account Resource Attributes
• Terraform Variables
E. Terraform Provisioners
• Understanding Provisioners in
Terraform
• Implementing remote-exec
provisioners
• Implementing local-exec
provisioners
• Integrating Ansible with Terraform
4. F. Terraform Modules & Workspaces:
• DRY Principle
• Implementing EC2 module with Terraform
• Variables and Terraform Modules
• Terraform Workspace
G. Terraform State:
• Local state & Remote State
• Configuring Remote State File S3
H. Discussions
5. A. INTRODUCTION
What is Terraform?
• Terraform is a tool for building, changing, and versioning
infrastructure safely and efficiently. Terraform can manage
existing and popular service providers as well as custom in-
house solutions.
• Configuration files describe to Terraform the components
needed to run a single application or your entire datacenter.
Terraform generates an execution plan describing what it will
do to reach the desired state, and then executes it to build the
described infrastructure. As the configuration changes,
Terraform is able to determine what changed and create
incremental execution plans which can be applied.
• The infrastructure Terraform can manage includes low-
level components such as compute instances, storage, and
networking, as well as high-level components such as DNS
entries, SaaS features, etc.
6. Key Features:
Infrastructure as Code
Infrastructure is described using a high-level configuration syntax. This allows a
blueprint of your datacenter to be versioned and treated as you would any other code.
Additionally, infrastructure can be shared and re-used.
Execution Plans
Terraform has a "planning" step where it generates an execution plan. The execution
plan shows what Terraform will do when you call apply. This lets you avoid any
surprises when Terraform manipulates infrastructure.
Change Automation
Complex changesets can be applied to your infrastructure with minimal human
interaction. With the previously mentioned execution plan and resource graph, you
know exactly what Terraform will change and in what order, avoiding many possible
human errors.
7.
8.
9.
10. B. INSTALLATION & SETTING UP
LAB
• Installing Terraform – Windows users or Linux Users
• To install Terraform on any supported system:
• Find the appropriate Terraform distribution package for your system and
download it. .
• After downloading Terraform, unzip the package to a directory of your
choosing.
• Optional but highly recommended: modify the path to include the directory
that contains the Terraform binary.
• After unzipping the package run ./terraform from command
• line and continue with your code..
11.
12.
13. C. Deploying Infrastructure with Terraform
• Creating First EC2 Instance with Terraform
1. Go to AWS console and launch an ec2 instance to manage or create the
infrastructure . Install terraform on that and configure the environment
path.
2. Now create a file with terraform code with .tf extension to launch an
ec2-instance .
3. It can be written in HCL (Hashicorp Configuration Language) or JSON.
17. • Commands :
1. $ terraform init // initializing and installing the plugins
2. $ terraform validate // validating the terraform files
3. $ terraform plan //testing the configuration files before run
4. $ terraform apply //applying and running the code mentioned
inside the terraform files
18.
19. • Destroying Infrastructure with Terraform
terraform destroy // destroy all resources mention in the .tf file
terraform destroy -target aws_instance.myec2 // destroy the target only
=> After destroying the resources do comment out the resources inside
terraform file. Otherwise it will recreate again
Infrastructure managed by Terraform will be destroyed. This will
ask for confirmation before destroying. The terraform
destroy command terminates resources
defined in your Terraform configuration. This command is the
reverse of terraform apply in that it terminates all the resources
specified by the configuration. It does not destroy resources
running elsewhere that are not described in the current
configuration.
20. • Terraform State
Terraform must store state about your managed infrastructure and configuration.
This state is used by Terraform to map real world resources to your configuration,
keep track of metadata, and to improve performance for large infrastructures.
This state is stored by default in a local file named "terraform.
Desired State:
It is the state where you have defined in your configuration, with the actual state of
your existing resources.
Current State:
Current configuration which is running in the environment and mentioned in the
local file.
21.
22. To refresh the current state:
terraform refresh
Scenario:
If you change a parameter manually in any services inside AWS and
then you want to roll back to previous value then it is mandatory to
have it inside the desired state files.