Quay lại

BÀI 3: Bài lab cơ bản về Terraform với EC2 – Tạo máy chủ ảo AWS bằng mã Chuyên mục Devops    2025-05-15    1 Lượt xem    1 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

Sau khi đã làm quen với Terraform qua việc tạo S3 Bucket, giờ là lúc bạn tiếp cận một thành phần quan trọng hơn trong hạ tầng cloud: máy chủ EC2. EC2 là trung tâm của rất nhiều hệ thống backend, web server, API…


🎯 Mục tiêu bài lab

  • Tạo một instance EC2

  • Gán vào subnet cụ thể

  • Gán SSH key để truy cập

  • Mở cổng 22 để SSH vào máy

  • Gán public IP để có thể SSH từ bên ngoài


🛠️ Yêu cầu trước

  • Tài khoản AWS và access key + secret key (cấu hình qua aws configure)

  • Đã cài đặt Terraform

  • 1 VPC default và ít nhất 1 subnet public

  • Có file .pem (SSH key) đã tạo trước trong EC2 hoặc tạo mới qua lệnh:

ssh-keygen -t rsa -b 2048 -f dev-key
aws ec2 import-key-pair \
  --key-name "dev-key" \
  --public-key-material fileb://dev-key.pub​

📁 Cấu trúc dự án

terraform-ec2-lab/
├── main.tf
├── variables.tf
├── terraform.tfvars
├── outputs.tf​

🧱 Bước 1: Khai báo provider và resource (main.tf)

provider "aws" {
  region = var.region
}

data "aws_vpc" "default" {
  default = true
}

data "aws_subnet" "public" {
  id = var.subnet_id
}

resource "aws_security_group" "allow_ssh" {
  name        = "allow_ssh"
  description = "Allow SSH inbound traffic"
  vpc_id      = data.aws_vpc.default.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web" {
  ami                         = var.ami_id
  instance_type               = var.instance_type
  subnet_id                   = data.aws_subnet.public.id
  vpc_security_group_ids      = [aws_security_group.allow_ssh.id]
  key_name                    = var.key_name
  associate_public_ip_address = true

  tags = {
    Name = "Terraform-EC2"
  }
}​

⚙️ Bước 2: Khai báo biến (variables.tf)

variable "region" {
  default = "us-east-1"
}

variable "ami_id" {
  description = "Amazon Machine Image ID (Ubuntu, Amazon Linux...)"
  type        = string
}

variable "instance_type" {
  description = "Loại máy EC2"
  default     = "t2.micro"
}

variable "key_name" {
  description = "Tên SSH key đã import vào AWS"
  type        = string
}

variable "subnet_id" {
  description = "ID của subnet nơi đặt máy chủ"
  type        = string
}​

📝 Bước 3: Gán giá trị cho biến (terraform.tfvars)

ami_id        = "ami-026c39f4021df9abe"
key_name      = "liveapp-dev-key"
instance_type = "t2.micro"
subnet_id     = "subnet-07c430403014d1538"​

📌 Bạn có thể tìm subnet_idami_id trong AWS Console (EC2 & VPC)
📌 AMI ID trên vùng us-east-1 thường dùng: ami-026c39f4021df9abe (Amazon Linux 2023)


📤 Bước 4: Xuất thông tin máy chủ (outputs.tf)

output "public_ip" {
  value = aws_instance.web.public_ip
}

output "instance_id" {
  value = aws_instance.web.id
}​

🚀 Bước 5: Chạy Terraform

terraform init
terraform plan
terraform apply​

Nhập yes để xác nhận.


🔍 Bước 6: Kiểm tra và SSH vào máy chủ

Sau khi apply thành công:

ssh -i liveapp-dev-key ec2-user@<public_ip>​

Lưu ý:

  • Amazon Linux dùng ec2-user

  • Ubuntu dùng ubuntu


🧹 Bước 7: Dọn dẹp

terraform destroy​

💡 Tổng kết bài lab

✅ Tạo EC2 instance với Terraform
✅ Sử dụng data để lấy VPC/subnet có sẵn
✅ Tự tạo security group mở port SSH
✅ Gán SSH key để truy cập
✅ Gán IP công khai để truy cập từ Internet


👉 Tiếp theo: [Bài 4 – Hiểu về hướng viết theo dạng Module trong Terraform: cấu trúc, chia nhỏ, tái sử dụng]

Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough

Bài viết liên quan

Learning English Everyday