1+ # 1. Create VPC.
2+ data "aws_availability_zones" "available" {}
3+
4+ module "vpc" {
5+ source = " terraform-aws-modules/vpc/aws"
6+ version = " 5.0.0"
7+
8+ name = var. cluster_name
9+ cidr = " 10.0.0.0/16"
10+
11+ azs = data. aws_availability_zones . available . names
12+ private_subnets = [" 10.0.1.0/24" , " 10.0.2.0/24" , " 10.0.3.0/24" ]
13+ public_subnets = [" 10.0.101.0/24" , " 10.0.102.0/24" , " 10.0.103.0/24" ]
14+ enable_dns_hostnames = true
15+ enable_dns_support = true
16+ enable_nat_gateway = true
17+ single_nat_gateway = true
18+
19+
20+ tags = {
21+ " kubernetes.io/cluster/${var.cluster_name}" = " shared"
22+ }
23+
24+ private_subnet_tags = {
25+ " kubernetes.io/cluster/${var.cluster_name}" = " shared"
26+ " kubernetes.io/role/internal-elb" = 1
27+ " cast.ai/routable" = " true"
28+ }
29+ }
30+
31+
132resource "aws_security_group" "cast_ai_vpc_service" {
233 name = " SG used by NGINX proxy VMs"
334 vpc_id = module. vpc . vpc_id
@@ -16,35 +47,65 @@ resource "aws_security_group" "cast_ai_vpc_service" {
1647 ]
1748}
1849
50+ locals {
51+ gateway_endpoints = [
52+ " s3"
53+ ]
54+ }
55+
56+ resource "aws_vpc_endpoint" "gateway" {
57+ for_each = toset (local. gateway_endpoints )
58+ vpc_id = module. vpc . vpc_id
59+ service_name = " com.amazonaws.${ var . cluster_region } .${ each . value } "
60+ vpc_endpoint_type = " Gateway"
61+ route_table_ids = module. vpc . private_route_table_ids
62+ tags = {
63+ Name = " ${ var . cluster_name } -${ each . value } -vpce"
64+ }
65+
66+ depends_on = [
67+ module . vpc
68+ ]
69+ }
70+
1971locals {
2072 interface_endpoints = [
73+ " ec2" ,
74+ " ec2messages" ,
75+ " ssm" ,
76+ " ssmmessages" ,
77+ " monitoring" ,
78+ " logs" ,
2179 " ecr.api" ,
2280 " ecr.dkr" ,
23- " logs" ,
24- " sts"
81+ " secretsmanager" ,
82+ " sts" ,
83+ " ecs-agent" ,
84+ " ecs-telemetry"
2585 ]
2686}
2787
28- resource "aws_vpc_endpoint" "interface_endpoints" {
29- for_each = toset (local. interface_endpoints )
30-
88+ resource "aws_vpc_endpoint" "interface" {
89+ for_each = toset (local. interface_endpoints )
3190 vpc_id = module. vpc . vpc_id
32- service_name = " com.amazonaws.${ var . cluster_region } .${ each . key } "
91+ service_name = " com.amazonaws.${ var . cluster_region } .${ each . value } "
3392 vpc_endpoint_type = " Interface"
3493 subnet_ids = module. vpc . private_subnets
94+ security_group_ids = [aws_security_group . vpc_endpoint_sg . id ]
3595 private_dns_enabled = true
36- security_group_ids = [aws_security_group . vpce_sg . id ]
37- }
96+ tags = {
97+ Name = " ${ var . cluster_name } -${ each . value } -vpce"
98+ }
3899
39- resource "aws_vpc_endpoint" "s3" {
40- vpc_id = module. vpc . vpc_id
41- service_name = " com.amazonaws.${ var . cluster_region } .s3"
42- route_table_ids = module. vpc . private_route_table_ids
100+ depends_on = [
101+ module . vpc
102+ ]
43103}
44104
45- resource "aws_security_group" "vpce_sg" {
46- name = " vpc-endpoints-sg"
47- description = " Allow access to VPC interface endpoints"
105+
106+ resource "aws_security_group" "vpc_endpoint_sg" {
107+ name = " ${ var . cluster_name } -vpce-sg"
108+ description = " SG for VPC interface endpoints"
48109 vpc_id = module. vpc . vpc_id
49110
50111 ingress {
@@ -60,15 +121,18 @@ resource "aws_security_group" "vpce_sg" {
60121 protocol = " -1"
61122 cidr_blocks = [" 0.0.0.0/0" ]
62123 }
124+
125+ tags = {
126+ Name = " ${ var . cluster_name } -vpce-sg"
127+ }
63128}
64129
65130resource "aws_vpc_endpoint" "cast_ai_rest_api" {
66- vpc_id = module. vpc . vpc_id
67- service_name = var. rest_api_service_name
68- vpc_endpoint_type = " Interface"
69- subnet_ids = module. vpc . private_subnets
70- security_group_ids = [aws_security_group . cast_ai_vpc_service . id ]
71- private_dns_enabled = true
131+ vpc_id = module. vpc . vpc_id
132+ service_name = var. rest_api_service_name
133+ vpc_endpoint_type = " Interface"
134+ subnet_ids = module. vpc . private_subnets
135+ security_group_ids = [aws_security_group . cast_ai_vpc_service . id ]
72136
73137 depends_on = [
74138 module . vpc
@@ -140,4 +204,3 @@ resource "aws_vpc_endpoint" "cast_ai_telemetry" {
140204 ]
141205}
142206
143-
0 commit comments