0%

AWS_VPC-LAB2 By Terraform

AWS_VPC-LAB2 By Terraform

本篇使用Terraform 工具重新部署 AWS_VPC-LAB2內容。如下示意圖

以下步驟與 AWS_VPC-LAB2一樣, 只是換成Terraform 工具操作,延續此篇的內容AWS_VPC-LAB1_By-Terraform,本篇只是增加以下步驟,若要完整使用本篇Terraform HCL code 來建構Lab2 ,建議先使用 AWS_VPC-LAB1_By-Terraform建構好Lab1。

  • 建立Private Subnet
  • 建立Routing Table
  • 將Routing Table與Private Subnet建立關聯
  • 建立Security Group
  • 建立EC2
  • 測試
  • 清理Lab所建立的所有元件

開始部署之前

本篇不是Terraform 基礎教學,若不清楚Terraform可以看參考資料。

  • 安裝Terraform 很簡單可以看官網-Install Terraform

  • 使用AWS API之前都需要先到IAM 設定申請一組keySECRET_ACCESS_KEY

    因為只是LAB,為了方便執行通常都會將申請好的key放置在shell script

    myauth_key.sh

    1
    2
    3
    #!/bin/bash
    export AWS_ACCESS_KEY_ID='your key id'
    export AWS_SECRET_ACCESS_KEY='your secret key'

    在執行任何AWS API或Terraform之前先執行 myauth_key.sh

    1
    source ./myhome-auth_key.sh
  • 完整Terraform Hcl code 在此88gocode/AWS-LAB-Terraform

    1
    2
    3
    git clone https://github.com/88gocode/AWS-LAB-Terraform.git
    cd AWS-LAB-Terraform
    git checkout lab2

    先宣告變數

    以下開始是Terraform hcl 語法。此節將VPC , subnet 等需要網段及登入 EC2 需要Key的名稱等參數, 用變數方式載入便於其他資源物件利用。

vars.tf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
variable "AWS_REGION" {
default = "ap-northeast-3"
}

variable "AWS_AZ" {
default = "ap-northeast-3a"
}

variable "vpc_cidr_lab0" {
default = "10.1.0.0/16"
description = "the vpc cidr"
}

variable "public_subnet_cidr_lab0" {
default = "10.1.1.0/24"
description = "The cidr of the public subnet"
}

variable "private_subnet_cidr_lab0" {
default = "10.1.2.0/24"
description = "The cidr of the private subnet"
}

variable "key_name" {
default = "my-test"
}

佈建雲端 IaaS 使用Terraform 好處是它提供多樣Provider : 像是 AWS,GCP,Azure,aliyun,DigitalOcean等等
以下是宣告是使用AWS Provider
provider.tf

1
2
3
4
provider "aws" {
profile = "default"
region = var.AWS_REGION
}

建立Private Subnet

  • 選擇VPC : VPC-LAB1
  • Subnet 名稱: AZ1-Private-Subnet
    • 選定AZ區域: 選擇日本大阪一個AZ(ap-northeast-3a)
    • 分配網段: 10.1.2.0/24

subnet.tf

1
2
3
4
5
6
7
8
9
10
11
# Create Private Subnet
resource "aws_subnet" "az1-private-subnet" {
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnet_cidr_lab0
availability_zone = var.AWS_AZ
map_public_ip_on_launch = "false"
tags = {
Name = "AZ1-Private-Subnet"

}
}

建立Routing Table

route.tf

1
2
3
4
5
6
7
8
9
10
11
12
13
# Create Private  Route Table
resource "aws_route_table" "main-private-rt" {
vpc_id = aws_vpc.main.id
tags = {
Name = "AZ1-Private-Rtable"
}
}

# route associations private (將路由表與Subnet建立關聯)
resource "aws_route_table_association" "main-private-rt-az1" {
subnet_id = aws_subnet.az1-private-subnet.id
route_table_id = aws_route_table.main-private-rt.id
}

建立Security Group(SG)

建立SG 名為 AZ1_Private_sg1,新增兩筆傳入規則

  • 任何來源 10.1.1.0/24 ,tcp port 22 都允許

  • 任何來源 0.0.0.0/0 ,ICMP-IPv4

sg.tf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
resource "aws_security_group" "sg2" {
name = "main_sg2"
vpc_id = aws_vpc.main.id

ingress {
from_port = "22"
to_port = "22"
protocol = "TCP"
cidr_blocks = ["10.1.1.0/24"]

}

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

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

tags = {
Name = "AZ1_Private_sg1"
}

}

AWS的 SG 是屬於StateFul,都是綁定在ENI(Elastic Network Interface: 網卡)上(相當於Linux Iptable角色);NACL 屬於 Stateless,都是綁定上 Subnet

建立EC2

這邊EC2簡單建立, Tag Name為 AZ1-Private-EC2_1 , Subnet: AZ1-Private-Subnet ,security group: AZ1_Private_sg1,ssh-key: my-test,AMI: 透過data source 獲取AMI ID (可以看上篇 AWS_VPC-LAB1_By-Terraform 獲取AMI的部分),其他預設即可

**ec2.tf **

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# For Private Subnet
resource "aws_instance" "example2" {
ami = data.aws_ami.example.id
instance_type = "t2.micro"
# the VPC subnet
subnet_id = aws_subnet.az1-private-subnet.id
# the security group
vpc_security_group_ids = [aws_security_group.sg2.id]
# the public SSH key
key_name = var.key_name
tags = {
Name = "AZ1-Private-EC2_1"
}
}

執行 Terraform

完整目錄架構如下:

1
2
3
4
5
6
7
8
9
.
├── ec2.tf
├── myDatasource.tf
├── provider.tf
├── route.tf
├── sg.tf
├── subnet.tf
├── vars.tf
└── vpc.tf

第一次執行Terraform,需要初始化先下載對應的Provider (本篇是 AWS),完成後,於所在目錄底下有一個隱藏檔 .terraform

1
terraform init

可以查看剛寫的 terraform 語法是否正確

1
terraform validate

執行測試計畫,可以看所列的參數

1
terraform plan

開始部署

1
terraform apply  -auto-approve

建立好 EC2, 查看分配Private IP,及EC2 Public IP

測試

  • 透過public ip 使用 ssh 加上 key 的方式連進來

    1
    2
    ssh-add  my-test.pem
    ssh -A [email protected]
  • AZ1-Pub-EC2_1 上 ping AZ1-Private-EC2_1 (Public Subnet 是否可以與 Private Subnet 相連接)

  • 測試 於 AZ1-Pub-EC2_1 上 ssh Login AZ1-Private-EC2_1

  • 於AZ1-Private-EC2_1, 測試是否能聯外

清理

AWS 的價值貴於方便與彈性,使用多少算多少錢,因此這個只是個LAB ,不用時記得將他清理刪除。
使用Terraform 只要下以下指令即可清理完成。

1
terraform destroy

參考資料

歡迎關注我的其它發布渠道