Creating aws EC2 instance using terraform
AWS account
Terraform installed
AWS CLI installed
AWS CLI configured with your credentials
Procedure using terraform EC2 module (ez way)
Create a directory for your project and change into it.
Create a file named main.tf
and add the following code to it:
module "ec2-instance" {
source = " terraform-aws-modules/ec2-instance/aws"
version = " 5.0.0"
}
Run terraform init
to initialize the directory.
Run terraform plan
to see what Terraform will do.
Run terraform apply
to create the resources.
Run terraform destroy
to destroy the resources.
Procedure using terraform aws provider
Create a directory for your project and change into it.
Create a file named main.tf
and add the following code to it:
provider "aws" {
region = " us-east-1"
}
resource "aws_instance" "devops-ex-7" {
ami = " ami-0c55b159cbfafe1f0"
instance_type = " t2.micro"
}
Run terraform init
to initialize the directory.
Run terraform plan
to see what Terraform will do.
Run terraform apply
to create the resources.
Run terraform destroy
to destroy the resources.