DevOps Terraform

Hướng dẫn tạo VPC trên AWS bằng Terraform chi tiết nhất

Terraform

Giới thiệu

Trong bài này, tôi sẽ hướng dẫn bạn cách tạo một VPC và có Subnets, Internet Gateway, Nat Gateway, và Route Table kèm theo. VPC sẽ của chúng ta sẽ gồm 4 Subnet: 2 Private và 2 Public, 2 NAT Gateway, 1 Internet Gateway và 4 Route Table. 

Trước khi bắt đầu, tôi giả sử là bạn đã có những kiến thức căn bản về Terraform và VPC trên AWS. Để tìm hiểu thêm về VPC và cách tạo ra nó trên AWS console, bạn có thể vào đây

Yêu Cầu 

  1. Hiểu biết căn bản về Terraform.
  2. Đã cài đặt Terraform trên hệ thống. 
  3. Có một account AWS
  4. Có access_key và secret_key của một user AWS IAM. ( Click vào đây, nếu chưa biết cách tạo )

Chúng ta sẽ làm gì ?

  1. Viết một file cấu hình Terraform để tạo một VPC với các tài nguyên phụ thuộc kèm theo.
  2. Tạo một VPC từ file cấu hình Terraform.
  3. Xóa VPC và các thành phần phụ thuộc bằng Terraform. 

Viết file cấu hình Terraform để tạo một VPC với các tài nguyên phụ thuộc kèm theo. 

Trước tiên, chúng ta sẽ tạo một thư mục dùng để chứa file cấu hình của Terraform. 

Sử dụng cấu lệnh sau để tạo, và dùng lệnh cd để đi vào thư mục đó. 

mkdir terraform
cd terraform/

Tôi sẽ sử dụng “vim” để chỉnh sửa file này, cái này tùy bạn, bạn có thể sử dụng các chương trình chỉnh sửa khác tùy ý như “vi, nano ….”.  Tiếp theo, bạn sẽ copy-paste các nội dung bên dưới vào file variables.tf, terraform.tfvars, và  main.tf

Tạo ‘main.tf’, file này có tính năng là tạo một VPC trên AWS với các tài nguyên kèm theo. File main.tf sẽ lấy các giá trị tham số từ 2 file variables.tf và  terraform.tfvars.

vim main.tf
provider "aws" {
      region     = "${var.region}"
      access_key = "${var.access_key}"
      secret_key = "${var.secret_key}"
}


# VPC resources: This will create 1 VPC with 4 Subnets, 1 Internet Gateway, 4 Route Tables. 

resource "aws_vpc" "default" {
  cidr_block           = var.cidr_block
  enable_dns_support   = true
  enable_dns_hostnames = true
}

resource "aws_internet_gateway" "default" {
  vpc_id = aws_vpc.default.id
}

resource "aws_route_table" "private" {
  count = length(var.private_subnet_cidr_blocks)

  vpc_id = aws_vpc.default.id
}

resource "aws_route" "private" {
  count = length(var.private_subnet_cidr_blocks)

  route_table_id         = aws_route_table.private[count.index].id
  destination_cidr_block = "0.0.0.0/0"
  nat_gateway_id         = aws_nat_gateway.default[count.index].id
}

resource "aws_route_table" "public" {
  vpc_id = aws_vpc.default.id
}

resource "aws_route" "public" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.default.id
}

resource "aws_subnet" "private" {
  count = length(var.private_subnet_cidr_blocks)

  vpc_id            = aws_vpc.default.id
  cidr_block        = var.private_subnet_cidr_blocks[count.index]
  availability_zone = var.availability_zones[count.index]
}

resource "aws_subnet" "public" {
  count = length(var.public_subnet_cidr_blocks)

  vpc_id                  = aws_vpc.default.id
  cidr_block              = var.public_subnet_cidr_blocks[count.index]
  availability_zone       = var.availability_zones[count.index]
  map_public_ip_on_launch = true
}

resource "aws_route_table_association" "private" {
  count = length(var.private_subnet_cidr_blocks)

  subnet_id      = aws_subnet.private[count.index].id
  route_table_id = aws_route_table.private[count.index].id
}

resource "aws_route_table_association" "public" {
  count = length(var.public_subnet_cidr_blocks)

  subnet_id      = aws_subnet.public[count.index].id
  route_table_id = aws_route_table.public.id
}


# NAT resources: This will create 2 NAT gateways in 2 Public Subnets for 2 different Private Subnets.

resource "aws_eip" "nat" {
  count = length(var.public_subnet_cidr_blocks)

  vpc = true
}

resource "aws_nat_gateway" "default" {
  depends_on = ["aws_internet_gateway.default"]

  count = length(var.public_subnet_cidr_blocks)

  allocation_id = aws_eip.nat[count.index].id
  subnet_id     = aws_subnet.public[count.index].id
}
 

Tạo ‘variables.tf’ , dùng để khai báo các biến môi trường. 

vim variables.tf
variable "access_key" {
     description = "Access key to AWS console"
     
}
variable "secret_key" {
     description = "Secret key to AWS console"
     
}

variable "region" {
  default     = "eu-west-3"
  type        = string
  description = "Region of the VPC"
}


variable "cidr_block" {
  default     = "10.0.0.0/16"
  type        = string
  description = "CIDR block for the VPC"
}

variable "public_subnet_cidr_blocks" {
  default     = ["10.0.0.0/24", "10.0.2.0/24"]
  type        = list
  description = "List of public subnet CIDR blocks"
}

variable "private_subnet_cidr_blocks" {
  default     = ["10.0.1.0/24", "10.0.3.0/24"]
  type        = list
  description = "List of private subnet CIDR blocks"
}

variable "availability_zones" {
  default     = ["eu-west-3a", "eu-west-3b"]
  type        = list
  description = "List of availability zones"
}

Tạo ‘terraform.tfvars’ dùng để định nghĩa access_key và secret_key. Chúng ta sẽ dùng 2 tham số này trong file ‘terraform.tfvars’

Giá trị của key này sẽ thay đổi tùy vào user IAM của bạn, bạn lưu ý thay đổi lại giá trị cho phù hợp. 

vim terraform.tfvars
access_key = "AKIAQ6GAIA5XIHHM2GJM"
secret_key = "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"

Bây giờ, bạn đã có 3 file variables.tf, terraform.tfvars, và main.tf

Tạo một VPC từ file cấu hình Terraform

Trước khi thực thi các câu lệnh sau, đảm bảo là bạn đã có 2 tham số access_key và secret_key

Câu lệnh đầu tiên chúng ta sẽ sử dụng là lệnh ‘terraform init’. Lệnh này sẽ tải về và cài đặt các Plugin trong file cấu hình. Trong trường hợp của chúng là AWS. 

terraform init

Lệnh thứ 2 là ‘terraform plan’. Lệnh này dùng để xem các thay đổi trong cấu trúc hạ tầng của chúng ta. 

terraform plan

Câu lệnh ‘terraform apply’ sẽ tạo các tài nguyên trên AWS được cấu hình trong file  main.tf. Bạn sẽ thấy dấu nhắc yêu cầu nhập vào tài nguyên muốn sử dụng ở đây.

terraform apply

Khi thực thi câu lệnh trên, bạn sẽ nhìn thấy 20 tài nguyên mới được thêm vào, và cũng sẽ thấy “0 has been destroyed” ở màn hình hiển thị.  

Bạn có thể truy cập vào console AWS VPC để kiểm tra xem VPC có được tạo cùng các cấu hình như mong muốn hay chưa. 

Xóa VPC đã tạo, cùng với các tài nguyên phụ thuộc.

Nếu như bạn không còn như cầu sử dụng VPC này nữa. Bạn sẽ dùng lệnh “terraform destroy” để xóa VPC và toàn bộ các tài nguyên phụ thuộc kèm theo. 

terraform destroy

Khi bạn thực thi câu lệnh trên, bạn sẽ nhìn thấy 20 tài nguyên đã tạo trước đó sẽ bị xóa đi. Truy cập console AWS để kiểm tra lại. 

Cuối cùng

Trong bài này, chúng tối đã hướng dẫn bạn các tạo một VPC với 4 subnet, 4 Route Table và 2 Nat Gateway, và 1 Internet Gateway. Chúng tôi đưa ra cách để xóa đi một VPC đã tạo. Chúc bạn thành công !!

        Nguồn:                  

congdonglinux.com

            Nhấn Subcribe để nhận thêm nhiều bài viết mới                              Like Facebook

                                       [maxbutton id=”2″ ]                                                           [maxbutton id=”3″ ] 

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !