
PRODUCTION LAB — Using `user_data` (BEST PRACTICE)
Instead of: Terraform → SSH → install nginx We use: Terraform → EC2 boots → user_data runs automatically 👉 No SSH 👉 No keys 👉 Faster 👉 Production-ready 📁 Folder structure terraform-userdata/ └── main.tf ✅ FULL main.tf (COPY EXACTLY) provider "aws" { region = "us-east-2" } # ----------------------------- # GET LATEST AMAZON LINUX AMI # ----------------------------- data "aws_ami" "amazon_linux" { most_recent = true owners = [ "amazon" ] filter { name = "name" values = [ "amzn2-ami-hvm-*-x86_64-gp2" ] } } # ----------------------------- # SECURITY GROUP # ----------------------------- resource "aws_security_group" "web_sg" { name = "userdata-sg" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = [ "0.0.0.0/0" ] } # ❗ Notice: NO SSH (port 22 removed) egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = [ "0.0.0.0/0" ] } } # ----------------------------- # EC2 INSTANCE (NO KEY, NO SSH) # ----------------------------- resource "aws_instance" "web" { ami = data
Continue reading on Dev.to
Opens in a new tab



