-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.tf
More file actions
150 lines (119 loc) · 3.45 KB
/
bootstrap.tf
File metadata and controls
150 lines (119 loc) · 3.45 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
provider "aws" {
profile = var.AWS_PROFILE
region = var.AWS_REGION
}
resource "aws_key_pair" "ssh_key" {
key_name = "${var.cluster}-access-key"
public_key = file(var.public_key)
}
resource "aws_security_group" "default_group" {
name = "security-group"
vpc_id = aws_vpc.cluster_vpc.id
ingress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = [ "0.0.0.0/0" ]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = var.cluster
}
}
resource "aws_vpc" "cluster_vpc" {
cidr_block = var.cluster_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
"kubernetes.io/cluster/${var.cluster}" = "owned"
Name = var.cluster
}
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.cluster_vpc.id
cidr_block = var.public_cidr
tags = {
"kubernetes.io/cluster/${var.cluster}" = "owned"
Name = "${var.cluster}-public"
}
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.cluster_vpc.id
cidr_block = var.private_cidr
tags = {
"kubernetes.io/cluster/${var.cluster}" = "owned"
Name = "${var.cluster}-private"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.cluster_vpc.id
tags = {
Name = var.cluster
}
}
resource "aws_iam_role_policy" "master" {
name = "${var.cluster}-master"
role = aws_iam_role.ec2_role.id
policy = file("cloud-local-up/master.json")
}
resource "aws_iam_role" "ec2_role" {
name = "${var.cluster}-ec2_role"
assume_role_policy = file("cloud-local-up/iam_role.json")
tags = {
Name = var.cluster
}
}
resource "aws_iam_instance_profile" "bootstrap_profile" {
name = "${var.cluster}-bootstrap_profile"
role = aws_iam_role.ec2_role.name
}
resource "aws_iam_policy" "node" {
name = "${var.cluster}-node-policy"
description = "Node IAM policy for cloud-provider"
policy = file("cloud-local-up/node.json")
}
resource "aws_instance" "host" {
key_name = aws_key_pair.ssh_key.key_name
ami = var.ami
instance_type = var.instance_type
subnet_id = aws_subnet.public.id
iam_instance_profile = aws_iam_instance_profile.bootstrap_profile.id
associate_public_ip_address = true
user_data = format(file("cloud-local-up/user_data.sh"), var.AWS_ACCESS_KEY_ID, var.AWS_SECRET_ACCESS_KEY, var.AWS_REGION, var.private_cidr, aws_iam_policy.node.arn)
tags = {
"kubernetes.io/cluster/${var.cluster}" = "owned"
Name = var.cluster
}
vpc_security_group_ids = [ aws_security_group.default_group.id ]
depends_on = [ aws_vpc.cluster_vpc, aws_internet_gateway.gw ]
connection {
type = "ssh"
user = "root"
private_key = file(var.private_key)
host = self.public_ip
}
ebs_block_device {
device_name = "/dev/sda1"
volume_type = "gp2"
volume_size = 30
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_vpc.cluster_vpc.main_route_table_id
}
resource "aws_route" "default_route" {
route_table_id = aws_vpc.cluster_vpc.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
output "ssh_access" {
value = "ssh fedora@${aws_instance.host.public_ip} -i ${var.private_key}"
depends_on = [aws_instance.host]
description = "The public IP address of the EC2 instance. To connect use `$ ssh fedora@<output-ip-addr> -i <your_private_key>`"
}