-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmain.tf
174 lines (146 loc) · 5.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# ------------------------------------------------------------------------
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
# -------------------------------------------------------------------------
terraform {
required_version = "=1.1.6"
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = ">= 1.7.0"
}
}
backend "s3" {
bucket = "adot-op-cluster-terraform-statefile"
key = "eks_adot_operator_cluster/terraform.tfstate"
region = "us-west-2"
}
}
module "common" {
source = "../common"
}
provider "aws" {
region = var.region
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.11.0"
name = "${var.eks_cluster_name}-vpc"
cidr = "10.0.0.0/16"
azs = ["${var.region}a", "${var.region}b", "${var.region}c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
single_nat_gateway = true
}
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "17.24.0"
cluster_version = "1.21"
cluster_name = var.eks_cluster_name
vpc_id = module.vpc.vpc_id
subnets = module.vpc.public_subnets
enable_irsa = true
cluster_endpoint_public_access = true
workers_role_name = aws_iam_role.eks_adot_operator_role.name
manage_worker_iam_resources = false
cluster_enabled_log_types = ["api", "audit", "authenticator", "controllerManager", "scheduler"]
worker_groups = [
{
instance_type = "m5.large"
asg_max_size = 1
iam_instance_profile_name = aws_iam_instance_profile.instance_profile.name
}
]
depends_on = [
aws_iam_instance_profile.instance_profile,
aws_iam_role.eks_adot_operator_role
]
}
resource "aws_iam_instance_profile" "instance_profile" {
name = "${var.eks_cluster_name}-instance-profile"
role = aws_iam_role.eks_adot_operator_role.name
}
resource "aws_iam_role" "eks_adot_operator_role" {
name = "${var.eks_cluster_name}-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = "EKSWorkerAssumeRole"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
managed_policy_arns = [
"arn:aws:iam::aws:policy/AmazonPrometheusRemoteWriteAccess",
"arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess",
"arn:aws:iam::aws:policy/CloudWatchAgentAdminPolicy",
"arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess",
"arn:aws:iam::aws:policy/AWSAppMeshEnvoyAccess",
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
]
}
data "aws_eks_cluster" "eks" {
name = module.eks.cluster_id
}
data "aws_eks_cluster_auth" "eks" {
name = module.eks.cluster_id
}
provider "kubernetes" {
host = data.aws_eks_cluster.eks.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.eks.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.eks.token
}
provider "kubectl" {
// Note: copy from eks module. Please avoid use shorted-lived tokens when running locally.
// For more information: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs#exec-plugins
host = data.aws_eks_cluster.eks.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.eks.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.eks.token
load_config_file = false
}
provider "helm" {
kubernetes {
host = data.aws_eks_cluster.eks.endpoint
cluster_ca_certificate = base64decode(data.aws_eks_cluster.eks.certificate_authority[0].data)
token = data.aws_eks_cluster_auth.eks.token
}
}
resource "helm_release" "adot-operator-cert-manager" {
name = "cert-manager"
namespace = "cert-manager"
repository = "https://charts.jetstack.io"
chart = "cert-manager"
version = "v1.4.3"
create_namespace = true
set {
name = "installCRDs"
value = "true"
}
provisioner "local-exec" {
# We need to set up a 20 seconds sleep to avoid the certificate signed by unknown authority issue which appears occasionally.
command = <<-EOT
kubectl wait --kubeconfig=${module.eks.kubeconfig_filename} --timeout=5m --for=condition=available deployment cert-manager -n cert-manager
kubectl wait --kubeconfig=${module.eks.kubeconfig_filename} --timeout=5m --for=condition=available deployment cert-manager-webhook -n cert-manager
sleep 20s
EOT
}
}