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
- You should have a look following lab:
- 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
}
}
- 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"
}
- Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/variables-locals-output/variables.tf
- 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
- 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
- Run init command:
terraform init
- Validate file:
terraform validate
- Run plan command with DEV tfvar file:
terraform plan --var-file="terraform-dev.tfvars"
- Run apply command to create resources, with DEV tfvar file. Then, Terraform asks to confirm, write "yes":
terraform apply --var-file="terraform-dev.tfvars"
- On AWS EC2 Instances:
- On VPC Section:
- Destroy DEV Environment:
terraform destroy --var-file="terraform-dev.tfvars"
- Update locals for PROD in main.tf:
....
locals {
staging_env = "product"
}
.....
- Run plan command with PROD tfvar file:
terraform plan --var-file="terraform-prod.tfvars"
- Run apply command to create resources, with PROD tfvar file. Then, Terraform asks to confirm, write "yes":
terraform apply --var-file="terraform-prod.tfvars"
- On AWS EC2 Instances:
- On VPC Section:
- Destroy PROD Environment:
terraform destroy --var-file="terraform-prod.tfvars"
- On EC2 Instances, all instances are terminated: