-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
143 lines (108 loc) · 3.47 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
}
locals {
k8s = var.deploy_type == "k8s"
full_name = "${var.project_name}-${terraform.workspace}"
k8s_cluster_name = "${local.full_name}-eks-cluster"
vpc_tags = local.k8s ? tomap({
"kubernetes.io/cluster/${local.k8s_cluster_name}" = "shared"
}) : {}
public_subnet_tags = local.k8s ? tomap({
"kubernetes.io/cluster/${local.k8s_cluster_name}" = "shared"
"kubernetes.io/role/elb" = "1"
}) : {}
private_subnet_tags = local.k8s ? tomap({
"kubernetes.io/cluster/${local.k8s_cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = "1"
}) : {}
}
module "network" {
source = "./modules/network"
project_name = var.project_name
cidr_block = var.vpc_cidr_block
azs_count = var.azs_count
vpc_tags = local.vpc_tags
public_subnet_tags = local.public_subnet_tags
private_subnet_tags = local.private_subnet_tags
}
module "db" {
source = "./modules/db"
project_name = var.project_name
env_type = var.env_type
engine = var.db_engine
engine_version = var.db_engine_version
instance_class = var.db_instance_class
name = var.db_name
username = var.db_username
password = var.db_password
vpc_id = module.network.vpc_id
subnet_ids = module.network.db_subnet_ids
depends_on = [ module.network ]
}
module "vm" {
count = var.deploy_type == "vm" ? 1 : 0
source = "./modules/vm"
project_name = var.project_name
env_type = var.env_type
image = var.docker_image
env = merge(var.env, module.db.env)
enable_https = var.enable_https
domain_name = var.domain_name
vpc_id = module.network.vpc_id
public_subnet_ids = module.network.public_subnet_ids
private_subnet_ids = module.network.private_subnet_ids
db_access_security_group_id = module.db.rds_access_sg_id
depends_on = [ module.network, module.db ]
}
module "k8s" {
source = "./modules/k8s"
create_k8s = local.k8s
cluster_name = local.k8s_cluster_name
env_type = var.env_type
vpc_id = module.network.vpc_id
subnet_ids = module.network.private_subnet_ids
db_access_security_group_id = module.db.rds_access_sg_id
aws_region = var.region
db_host = module.db.rds_hostname
db_port = module.db.rds_port
db_service_name = var.db_engine
depends_on = [ module.network, module.db ]
}
data "aws_eks_cluster" "cluster" {
count = local.k8s ? 1 : 0
name = module.k8s.cluster_id
}
data "aws_eks_cluster_auth" "cluster" {
count = local.k8s ? 1 : 0
name = module.k8s.cluster_id
}
provider "kubernetes" {
host = element(concat(data.aws_eks_cluster.cluster[*].endpoint, [""]), 0)
cluster_ca_certificate = base64decode(element(concat(data.aws_eks_cluster.cluster[*].certificate_authority.0.data, [""]), 0))
token = element(concat(data.aws_eks_cluster_auth.cluster[*].token, [""]), 0)
}
provider "helm" {
kubernetes {
host = element(concat(data.aws_eks_cluster.cluster[*].endpoint, [""]), 0)
cluster_ca_certificate = base64decode(element(concat(data.aws_eks_cluster.cluster[*].certificate_authority.0.data, [""]), 0))
token = element(concat(data.aws_eks_cluster_auth.cluster[*].token, [""]), 0)
}
}
module "k8s_app" {
count = local.k8s ? 1 : 0
source = "./modules/k8s_app"
project_name = var.project_name
db_host = module.db.rds_hostname
db_port = module.db.rds_port
db_service_name = var.db_engine
image = var.docker_image
env = merge(var.env, module.db.env)
cluster_name = local.k8s_cluster_name
depends_on = [ module.k8s ]
}