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
@2020 copyright KalKey training
3. @2020 copyright KalKey training
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
@2020 copyright KalKey training
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.
@2020 copyright KalKey training
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.
@2020 copyright KalKey training
12. 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.
@2020 copyright KalKey training
17. • 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.
@2020 copyright KalKey training
18. • 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.
@2020 copyright KalKey training
20. 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.
@2020 copyright KalKey training
21. • D. Interpolation, Attributes & Variables:
• Attributes and Output Values
Resource instances managed by Terraform each export attributes whose values can be used elsewhere in
configuration. Output values are a way to expose some of that information to the user of your module.
Note: For brevity, output values are often referred to as just "outputs" when the meaning is clear from
context.
@2020 copyright KalKey training
40. • Steps:
1. Install Ansible on the system.
2. Write a playbook to install and configure the applications.
3. Copy and paste the pem file which is tagged to the instance.
4. Write your tf code to build and provision the instance . Inside the tf code call
ansible playbook to run it like below.
@2020 copyright KalKey training