Skip to content

Latest commit

 

History

History
275 lines (197 loc) · 7.15 KB

LAB03-Variables-Locals-Output-EC2.md

File metadata and controls

275 lines (197 loc) · 7.15 KB

LAB-03: Variables, Locals, Output => Provision EC2s

This scenario shows:

  • how to create EC2 using Variables, Locals and Output

Code: https://github.com/omerbsezer/Fast-Terraform/tree/main/labs/variables-locals-output

Prerequisite

Steps

  • Create main.tf and copy the code:
# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
   region     = var.location
}

locals {
  staging_env = "staging"
}

resource "aws_vpc" "my_vpc" {
  cidr_block = "10.0.0.0/16"
  enable_dns_hostnames = true
  tags = {
    Name = "${local.staging_env}-vpc-tag"
  }
}

resource "aws_subnet" "my_subnet" {
  vpc_id = aws_vpc.my_vpc.id
  cidr_block = "10.0.0.0/16"
  availability_zone = var.availability_zone
  tags = {
    Name = "${local.staging_env}-subnet-tag"
  }
}

resource "aws_internet_gateway" "my_vpc_igw" {
  vpc_id = aws_vpc.my_vpc.id
  tags = {
    Name = "${local.staging_env}-Internet Gateway"
  }
}

resource "aws_route_table" "my_vpc_eu_central_1c_public" {
    vpc_id = aws_vpc.my_vpc.id
    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.my_vpc_igw.id
    }
    tags = {
        Name = "${local.staging_env}- Public Subnet Route Table"
    }
}
resource "aws_route_table_association" "my_vpc_eu_central_1c_public" {
    subnet_id      = aws_subnet.my_subnet.id
    route_table_id = aws_route_table.my_vpc_eu_central_1c_public.id
}

resource "aws_instance" "ec2_example" {
   
   ami                         = var.ami
   instance_type               = var.instance_type
   subnet_id                   = aws_subnet.my_subnet.id
   associate_public_ip_address = true
   
   tags = {
           Name = var.tag
   }
}

# output single values
output "public_ip" {
  value = aws_instance.ec2_example.public_ip
}

# output single values
output "public_dns" {
  value = aws_instance.ec2_example.public_dns
} 

# output multiple values
output "instance_ips" {
  value = {
    public_ip  = aws_instance.ec2_example.public_ip
    private_ip = aws_instance.ec2_example.private_ip
  }
} 

image

  • Create variables.tf:
variable "instance_type" {
    type = string
    description = "EC2 Instance Type"
}

variable "tag" {
    type = string
    description = "The tag for the EC2 instance"
}

variable "location" {
    type = string
    description = "The project region"
    default = "eu-central-1"
}

variable  "availability_zone" {
    type = string
    description = "The project availability zone"
    default = "eu-central-1c"
} 

variable "ami" {
    type = string
    description = "The project region"
}

image

  • Create terraform-dev.tfvars:
 instance_type     =   "t2.nano"
 tag               =   "EC2 Instance for DEV"
 location          =   "eu-central-1"
 availability_zone =   "eu-central-1c"
 ami               =   "ami-0e067cc8a2b58de59" # Ubuntu 20.04 eu-central-1 Frankfurt

image

  • Create terraform-prod.tfvars:
instance_type     =   "t2.micro"
tag               =   "EC2 Instance for PROD"
location          =   "eu-central-1"
availability_zone =   "eu-central-1c"
ami               =   "ami-0d1ddd83282187d18" # Ubuntu 22.04 eu-central-1 Frankfurt

image

  • Run init command:
terraform init

image

  • Validate file:
terraform validate

image

  • Run plan command with DEV tfvar file:
terraform plan --var-file="terraform-dev.tfvars"

image

  • Run apply command to create resources, with DEV tfvar file. Then, Terraform asks to confirm, write "yes":
terraform apply --var-file="terraform-dev.tfvars"

image

image

  • On AWS EC2 Instances:

image

  • On VPC Section:

image

  • Destroy DEV Environment:
terraform destroy --var-file="terraform-dev.tfvars"

image

image

  • Update locals for PROD in main.tf:
....
locals {
  staging_env = "product"
}
.....

image

  • Run plan command with PROD tfvar file:
terraform plan --var-file="terraform-prod.tfvars"

image

  • Run apply command to create resources, with PROD tfvar file. Then, Terraform asks to confirm, write "yes":
terraform apply --var-file="terraform-prod.tfvars"

image

  • On AWS EC2 Instances:

image

  • On VPC Section:

image

  • Destroy PROD Environment:
terraform destroy --var-file="terraform-prod.tfvars"

image

image

  • On EC2 Instances, all instances are terminated:

image