Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 1.54 KB

Ex-7.md

File metadata and controls

54 lines (43 loc) · 1.54 KB

Creating aws EC2 instance using terraform

Prerequisites

  • AWS account
  • Terraform installed
  • AWS CLI installed
  • AWS CLI configured with your credentials

Procedure using terraform EC2 module (ez way)

  1. Create a directory for your project and change into it.
  2. 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"
}
  1. Run terraform init to initialize the directory. terraform init
  2. Run terraform plan to see what Terraform will do. terraform plan
  3. Run terraform apply to create the resources. terraform apply terraform apply AWS EC-2 running
  4. Run terraform destroy to destroy the resources. terraform destroy terraform destroy AWS EC-2 terminated

Procedure using terraform aws provider

  1. Create a directory for your project and change into it.
  2. 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"
}
  1. Run terraform init to initialize the directory.
  2. Run terraform plan to see what Terraform will do.
  3. Run terraform apply to create the resources.
  4. Run terraform destroy to destroy the resources.