Install Terraform : https://developer.hashicorp.com/terraform/install
-
Install AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
-
Run command
aws configure
and follow instructions.
-
Create a directory, use any name you like.
-
Create a
main.tf
file:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "app_server_[YOUR_NAME]" {
ami = "ami-0f58b397bc5c1f2e8"
instance_type = "t2.micro"
tags = {
Name = "my-terraform-demo-[YOUR_NAME]"
}
}
output "instance_ip_address" {
description = "The public IP address of the instance"
value = aws_instance.app_server_[YOUR_NAME].public_ip
}
-
In the directory, run the command
terraform init
-
Run
terraform plan
-
Run
terraform apply
, enterYes
.
-
Change the
ami
value in themain.tf
file toami-02e1a4ebef29334f9
-
Run
terraform plan
-
Run
terraform apply
, enterYes
- Create a new file
variables.tf
:
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
default = "my-new-terraform-demo-[YOUR_NAME]"
}
-
Make changes to the
main.tf
file:In the
tags
section, replaceName = "my-terraform-demo-[YOUR_NAME]
withName = var.instance_name
-
Run
terraform plan
-
Run
terraform apply
, enterYes
- Run
terraform destroy
, enterYes