使用 Terraform 在 Amazon VPC 服務建立資源

做法

新增 variables.tf 檔。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
variable "aws_region" {
type = string
description = "AWS Region"
}

variable "app_name" {
type = string
description = "Application Name"
}

variable "app_environment" {
type = string
description = "Application Environment"
}

新增 terraform.tfvars 檔。

1
2
3
aws_region      = "ap-northeast-1"
app_name = "my-project"
app_environment = "staging"

新增 vpc.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
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.19.0"

name = "ipv6"
cidr = "10.0.0.0/16"

azs = ["ap-northeast-1a", "ap-northeast-1c"]
public_subnets = ["10.0.0.0/19", "10.0.32.0/19"]
private_subnets = ["10.0.64.0/19", "10.0.96.0/19"]
database_subnets = ["10.0.128.0/19", "10.0.160.0/19"]

enable_nat_gateway = true

create_database_subnet_route_table = true
create_database_internet_gateway_route = true

enable_ipv6 = true
assign_ipv6_address_on_creation = true

private_subnet_assign_ipv6_address_on_creation = false

public_subnet_ipv6_prefixes = [0, 1]
private_subnet_ipv6_prefixes = [2, 3]
database_subnet_ipv6_prefixes = [4, 5]

tags = {
Name = "${var.app_name}-vpc"
Environment = var.app_environment
}
}

參考資料