Repository navigation
Expand file tree
/
Copy pathtest1.py
More file actions
209 lines (186 loc) · 5.91 KB
/
Copy pathtest1.py
File metadata and controls
209 lines (186 loc) · 5.91 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pulumi
import pulumi_eks as eks
import pulumi_aws as aws
import pulumi_kubernetes as k8s
import json
cluster_name = "eks-demo"
cluster_tag = f"kubernetes.io/cluster/{cluster_name}"
public_subnet_cidrs = ["172.32.0.0/20", "172.32.16.0/20"]
# Use 2 AZs for our cluster
avail_zones = ["ap-northeast-2a", "ap-northeast-2c"]
# Create VPC for EKS Cluster
vpc = aws.ec2.Vpc(
"eks-vpc",
cidr_block="172.32.0.0/16",
tags={
'Name': 'eks-vpc',
}
)
igw = aws.ec2.InternetGateway(
"eks-igw",
vpc_id=vpc.id,
tags={
'Name': 'eks-igw',
}
)
route_table = aws.ec2.RouteTable(
"eks-route-table",
vpc_id=vpc.id,
routes=[
{
"cidr_block": "0.0.0.0/0",
"gateway_id": igw.id
}
],
tags={
'Name': 'eks-rt',
}
)
public_subnet_ids = []
# Create public subnets that will be used for the AWS Load Balancer Controller
"""
Public Subnets should be resource tagged with: kubernetes.io/role/elb: 1
Private Subnets should be tagged with: kubernetes.io/role/internal-elb: 1
Both private and public subnets should be tagged with: kubernetes.io/cluster/${your-cluster-name}: owned
https://stackoverflow.com/questions/66039501/eks-alb-is-not-to-able-to-auto-discover-subnets
"""
for zone, public_subnet_cidr in zip(avail_zones, public_subnet_cidrs):
public_subnet = aws.ec2.Subnet(
f"eks-public-subnet-{zone}",
assign_ipv6_address_on_creation=False,
vpc_id=vpc.id,
map_public_ip_on_launch=True,
cidr_block=public_subnet_cidr,
availability_zone=zone,
tags={
# Custom tags for subnets
"Name": f"eks-public-subnet-{zone}",
cluster_tag: "owned",
"kubernetes.io/role/elb": "1",
f"kubernetes.io/cluster/{cluster_name}": "owned",
}
)
aws.ec2.RouteTableAssociation(
f"eks-public-rta-{zone}",
route_table_id=route_table.id,
subnet_id=public_subnet.id,
)
public_subnet_ids.append(public_subnet.id)
# Create an EKS cluster.
cluster = eks.Cluster(
cluster_name,
name=cluster_name,
vpc_id=vpc.id,
instance_type="t2.medium",
desired_capacity=2,
min_size=1,
max_size=2,
provider_credential_opts={'profile_name': 'user1'},
public_subnet_ids=public_subnet_ids,
create_oidc_provider=True,
)
# Export the cluster's kubeconfig.
pulumi.export("kubeconfig", cluster.kubeconfig)
# pulumi stack output kubeconfig > ../.kube/config (to your directory)
"""
Add IAM Policy.
This policy needs to be attached to the EKS worker nodes
so that these nodes have the proper permissions to communicate
with the Amazon Load Balancers.
"""
# aws_lb_ns = "aws-lb-controller"
# service_account_name = f"system:serviceaccount:{aws_lb_ns}:aws-lb-controller-serviceaccount"
# oidc_arn = cluster.core.oidc_provider.arn
# oidc_url = cluster.core.oidc_provider.url
# # Create IAM role for AWS LB controller service account
# iam_role = aws.iam.Role(
# "aws-loadbalancer-controller-role",
# assume_role_policy=pulumi.Output.all(oidc_arn, oidc_url).apply(
# lambda args: json.dumps(
# {
# "Version": "2012-10-17",
# "Statement": [
# {
# "Effect": "Allow",
# "Principal": {
# "Federated": args[0],
# },
# "Action": "sts:AssumeRoleWithWebIdentity",
# "Condition": {
# "StringEquals": {f"{args[1]}:sub": service_account_name},
# },
# }
# ],
# }
# )
# ),
# )
# with open("files/iam_policy.json") as policy_file:
# policy_doc = policy_file.read()
# iam_policy = aws.iam.Policy(
# "aws-loadbalancer-controller-policy",
# policy=policy_doc,
# opts=pulumi.ResourceOptions(parent=iam_role),
# )
# # Attach IAM Policy to IAM Role
# aws.iam.PolicyAttachment(
# "aws-loadbalancer-controller-attachment",
# policy_arn=iam_policy.arn,
# roles=[iam_role.name],
# opts=pulumi.ResourceOptions(parent=iam_role),
# )
# provider = k8s.Provider("provider", kubeconfig=cluster.kubeconfig)
# namespace = k8s.core.v1.Namespace(
# f"{aws_lb_ns}-ns",
# metadata={
# "name": aws_lb_ns,
# "labels": {
# "app.kubernetes.io/name": "aws-load-balancer-controller",
# }
# },
# opts=pulumi.ResourceOptions(
# provider=provider,
# parent=provider,
# )
# )
# service_account = k8s.core.v1.ServiceAccount(
# "aws-lb-controller-sa",
# metadata={
# "name": "aws-lb-controller-serviceaccount",
# "namespace": namespace.metadata["name"],
# "annotations": {
# "eks.amazonaws.com/role-arn": iam_role.arn.apply(lambda arn: arn)
# }
# }
# )
# # This transformation is needed to remove the status field from the CRD
# # otherwise the Chart fails to deploy
# def remove_status(obj, opts):
# if obj["kind"] == "CustomResourceDefinition":
# del obj["status"]
# k8s.helm.v3.Chart(
# "lb", k8s.helm.v3.ChartOpts(
# chart="aws-load-balancer-controller",
# version="1.2.0",
# fetch_opts=k8s.helm.v3.FetchOpts(
# repo="https://aws.github.io/eks-charts"
# ),
# namespace=namespace.metadata["name"],
# values={
# "region": "ap-northeast-2",
# "serviceAccount": {
# "name": "aws-lb-controller-serviceaccount",
# "create": False,
# },
# "vpcId": cluster.eks_cluster.vpc_config.vpc_id,
# "clusterName": cluster.eks_cluster.name,
# "podLabels": {
# "stack": pulumi.get_stack(),
# "app": "aws-lb-controller"
# }
# },
# transformations=[remove_status]
# ), pulumi.ResourceOptions(
# provider=provider, parent=namespace
# )
# )