323063_Assignment5
323063_Assignment5
Cloud Computing
Assignment 5
Aim : Write IaC using terraform to create EC2 machine on AWS or azure or
google cloud. (Compulsory to use Input and output variable files)
Theory :
Terraform is an open-source infrastructure as code (IaC) tool developed by
HashiCorp. It allows users to define and provision data center infrastructure
using a declarative configuration language. In simpler terms, Terraform enables
you to manage your infrastructure by writing code rather than manually
configuring resources.
• Infrastructure as Code (IaC) : Terraform follows the principle of
Infrastructure as Code, which means infrastructure is managed using
code rather than through manual processes or interactive configuration
tools. With Terraform, you define the desired state of your infrastructure
in code, and Terraform takes care of provisioning and managing the
actual resources to match that state.
• Declarative Configuration Language : Terraform uses its own
configuration language called HashiCorp Configuration Language (HCL) to
define infrastructure components and their relationships. HCL is a
human-readable language designed specifically for defining
infrastructure configurations. It allows you to specify the desired state of
various resources, such as virtual machines, networks, storage, and
more.
• Providers : Terraform uses providers to interact with different cloud
providers, infrastructure platforms, and services. Providers are
responsible for understanding API interactions and exposing resources
that Terraform can manage. For example, there are providers for Amazon
Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP),
VMware, and many others. Each provider offers a set of resources that
can be managed using Terraform.
• Terraform Configuration Files : Terraform configurations are defined in
files with a .tf extension. These files contain the infrastructure code
written in HCL. A Terraform configuration typically includes resource
definitions, variable declarations, and any other necessary configuration
settings. You can organize your configurations into multiple files and
directories for better maintainability.
• State Management : Terraform maintains a state file that keeps track of
the current state of your infrastructure. This state file is used to map the
relationships between your configuration and the real-world resources
provisioned by Terraform. It also helps Terraform determine what
changes need to be applied to reach the desired state. The state file can
be stored locally or remotely, depending on your configuration.
• Execution Plan : When you run Terraform commands, such as terraform
plan or terraform apply, Terraform generates an execution plan based on
the current state of your infrastructure and the changes defined in your
configuration. The execution plan outlines what actions Terraform will
take to create, update, or delete resources to reach the desired state.
• Apply Changes : Once you review and approve the execution plan, you
can apply the changes by running terraform apply. Terraform will then
execute the planned actions, provisioning or modifying resources as
necessary. During this process, Terraform communicates with the
respective providers' APIs to create, update, or delete resources
according to the configuration.
• Version Control Integration : Terraform configurations can be versioned
using version control systems like Git. This allows teams to collaborate on
infrastructure changes, track modifications over time, and revert to
previous versions if needed.
Implementation :
Step 1 : Download and install terraform
Verify Installation
We can also connect to this instance using SSH and the PEM file created
Terraform Code :
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-east-1"
access_key = "AKIA2UC3AMLO6RUC62FJ"
secret_key = "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"
}
variable "key_name" {}
tags = {
Name = "terraform_instance"
}
}