Skip to content

Latest commit

 

History

History
157 lines (101 loc) · 4.24 KB

LAB08-Modules-EC2.md

File metadata and controls

157 lines (101 loc) · 4.24 KB

LAB-08: Modules => Provision EC2

This scenario shows:

  • how to create and use modules

Code: https://github.com/omerbsezer/Fast-Terraform/tree/main/labs/modules

Prerequisite

Steps

  • With modules, it helps:

    • organize configuration
    • encapsulation
    • re-usability
    • consistency
  • Main.tf refers modules (directories)

  • Each modules have variables.tf

Module Calls (Main.tf)

Main.tf Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/modules/main.tf

...
module "webserver-1" {
  source = ".//module1"

  instance_type     =   "t2.nano"
  tag               =   "Webserver1 - Module1 - 20.04"
  location          =   "eu-central-1"
  availability_zone =   "eu-central-1c"
  ami               =   "ami-0e067cc8a2b58de59" # Ubuntu 20.04 eu-central-1 Frankfurt

}

module "webserver-2" {
  source = ".//module2"

  instance_type     =   "t2.micro"
  tag               =   "Webserver2 - Module2 - 22.04"
  location          =   "eu-central-1"
  availability_zone =   "eu-central-1a"
  ami               =   "ami-0d1ddd83282187d18" # Ubuntu 22.04 eu-central-1 Frankfurt
}

image

Module1

Module1 Variables.tf Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/modules/module1/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"
}

Module1 Main.tf Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/modules/module1/main.tf

Module2

  • Module2 variables.tf is same as module1 variables.tf

Module2 Variables.tf Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/modules/module2/variables.tf

  • Module2 main.tf code is different from module1 main.tf

Module2 Main.tf Code: https://github.com/omerbsezer/Fast-Terraform/blob/main/labs/modules/module2/main.tf

Terraform Run

  • Run init, validate command:
terraform init
terraform validate

image

image

  • Run plan, apply command:
terraform plan
terraform apply

image

image

  • On AWS, 2 EC2 Instances:

image

image

  • On AWS, 2 VPCs:

image

image

  • On Browser:

image

image

  • Destroy infrastructure:
terraform destroy 

image

image

  • On AWS, EC2s are terminated:

image