Terraform Provisioners — FULL HANDS-ON LAB

Lab Goal By the end of this lab, students will: Create EC2 using Terraform Use remote-exec → install nginx Use local-exec → save IP locally Use destroy provisioner → cleanup message Observe failure...

By · · 1 min read
Terraform Provisioners — FULL HANDS-ON LAB

Source: DEV Community

Lab Goal By the end of this lab, students will: Create EC2 using Terraform Use remote-exec → install nginx Use local-exec → save IP locally Use destroy provisioner → cleanup message Observe failure behavior (tainting) Architecture (simple flow) Terraform → AWS EC2 → remote-exec → configure server → local-exec → save IP locally STEP 1 — Prepare Environment 1.1 Create key pair in AWS Name: terraform-key Download: terraform-key.pem 1.2 Move key to project folder provisioner-lab/ ├── main.tf └── terraform-key.pem 1.3 Fix permissions (IMPORTANT) chmod 400 terraform-key.pem Why? SSH will fail if permissions are too open. 1.4 Create Security Group Allow: SSH → 22 HTTP → 80 Example: Type Port Source SSH 22 0.0.0.0/0 HTTP 80 0.0.0.0/0 STEP 2 — Write Terraform Code Create file: main.tf provider "aws" { region = "us-east-1" } resource "aws_instance" "web" { ami = "ami-0c02fb55956c7d316" instance_type = "t2.micro" key_name = "terraform-key" vpc_security_group_ids = ["sg-xxxxxxxx"] # replace # ----