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

323063_Assignment5

The document outlines an assignment on using Terraform to create an EC2 instance on AWS, emphasizing the principles of Infrastructure as Code (IaC) and the use of HashiCorp Configuration Language (HCL). It details the steps for installation, configuration, and execution of Terraform commands, including provider setup and resource management. The conclusion highlights the learning outcomes related to Terraform code writing and instance validation.

Uploaded by

atharv.22210392
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)
6 views

323063_Assignment5

The document outlines an assignment on using Terraform to create an EC2 instance on AWS, emphasizing the principles of Infrastructure as Code (IaC) and the use of HashiCorp Configuration Language (HCL). It details the steps for installation, configuration, and execution of Terraform commands, including provider setup and resource management. The conclusion highlights the learning outcomes related to Terraform code writing and instance validation.

Uploaded by

atharv.22210392
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/ 13

Submitted By :

Name : Atharv SAWANT


Roll No. : 323063
PRN No. : 22210392
Div : TY C Batch : C3

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

Step 2 : Add provider in main.tf file


Step 3 : Create an IAM user with Administrator access
Step 4 : Create an access key for the user
Step 5 : Configure the provider with the access key and secret key

Step 6 : Configure the terraform file to generate PEM file


Step 7 : Create a key pair

Step 8 : Save the private key in local system

Step 9 : Create an EC2 instance


Step 10 : Initialise terraform and start the instance
This will create an EC2 instance and can be checked in EC2 dashboard

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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"
}

resource "tls_private_key" "rsa_4096" {


algorithm = "RSA"
rsa_bits = 4096
}

variable "key_name" {}

resource "aws_key_pair" "key_pair" {


key_name = var.key_name
public_key = tls_private_key.rsa_4096.public_key_openssh
}

resource "local_file" "private_key" {


content = tls_private_key.rsa_4096.private_key_pem
filename = var.key_name
}

resource "aws_instance" "terraform_instance" {


ami = "ami-080e1f13689e07408"
instance_type = "t2.micro"
key_name = aws_key_pair.key_pair.key_name

tags = {
Name = "terraform_instance"
}
}

Conclusion : Learned to write Terraform code to create and configure an EC2


instance, use input and output variables to customize the deployment, and
validate the deployment by accessing the instance.

You might also like