各位邦友,如果各位有跟著做的話,目前已經完成下圖的「上半部」囉,為了完整說明SSH的設定,另外想更進一步熟悉Terraform更多的語法,今天就來繼續完成下圖紅色框框
部份的架構。
把這個檔案跟之前的sample.tf放在一起即可(相同資料夾)
data "aws_vpc" "default" {
default = true
}
resource "aws_route_table" "RT_private" {
vpc_id = "${data.aws_vpc.default.id}"
tags {
Name = "RT_private"
}
}
resource "aws_route_table_association" "RT_association_private" {
subnet_id = "${aws_subnet.private_subnet.id}"
route_table_id = "${aws_route_table.RT_private.id}"
}
resource "aws_subnet" "private_subnet" {
# 172.31.0.0/16
vpc_id = "${data.aws_vpc.default.id}"
availability_zone = "ap-northeast-1a"
# 172.31.48.0/24`)
cidr_block = "${cidrsubnet(data.aws_vpc.default.cidr_block, 8, 48)}"
}
resource "aws_instance" "private_subnet_instance" {
count = "3"
ami = "ami-0689a2637ab83e607"
instance_type = "t2.micro"
key_name = "private_subnet_key"
subnet_id = "${aws_subnet.private_subnet.id}"
vpc_security_group_ids = [
"${aws_security_group.bastion_to_private.id}"
]
tags {
Name = "private_subnet_instance_${count.index}"
}
}
resource "aws_security_group" "bastion_to_private" {
name = "bastion_to_private"
description = "ssh demo"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [
"${aws_security_group.bastion_from_bright.id}"
]
}
egress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [
"${aws_security_group.bastion_from_bright.id}"
]
}
}
data "aws_vpc" "default" {...}
:
true
resource "aws_route_table" "RT_private" {...}
:
"${data.aws_vpc.default.id}"
${data
...開頭)resource "aws_route_table_association" "RT_association_private" {...}
:
resource "aws_subnet" "private_subnet" {...}
:
cidr_block="${cidrsubnet(data.aws_vpc.default.cidr_block, 8, 48)}"
172.31.0.0/16
>>> 172.31.48.0/24
)resource "aws_instance" "private_subnet_instance" {...}
:
resource "aws_security_group" "bastion_to_private" {...}
:
之前介紹過了,簡單說,就是bastion(public subnet)instance 跟 hosts(private subnet)intances只開放22port
terraform apply
小編不截圖囉,各位看倌自己試試看…
待續…