-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
196 lines (161 loc) · 4.28 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = "1.7.5"
}
# Configure the AWS Provider
provider "aws" {
region = "eu-west-1"
}
locals {
name_prefix = "caprakurs"
tags = {
project = "${local.name_prefix}-k8s"
}
key_name = "${local.name_prefix}-pk"
number_of_nodes = 1
}
resource "tls_private_key" "kurs_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = local.key_name
public_key = tls_private_key.kurs_key.public_key_openssh
}
output "pubkey" {
value = aws_key_pair.generated_key.public_key
}
resource "aws_vpc" "this" {
cidr_block = "10.10.0.0/16"
enable_dns_hostnames = true
tags = merge(local.tags, {
Name = "${local.name_prefix}-vpc"
})
}
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.this.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
}
}
resource "aws_security_group" "instance_security" {
name = "${local.name_prefix}-sg"
vpc_id = aws_vpc.this.id
ingress {
from_port = 0
to_port = 0
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "all"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_route_table_association" "public" {
route_table_id = aws_route_table.public.id
subnet_id = aws_subnet.public.id
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.this.id
cidr_block = "10.10.1.0/24"
availability_zone = "eu-west-1a"
map_public_ip_on_launch = true
tags = {
Name = "${local.name_prefix}-public-subnet"
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"] # Canonical
}
# Create IAM Role
resource "aws_iam_role" "ssm_role" {
name = "${local.name_prefix}-ec2-roles"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Principal = {
Service = "ec2.amazonaws.com"
},
Action = "sts:AssumeRole"
}
]
})
}
# Attach AmazonSSMManagedInstanceCore Policy to the Role
resource "aws_iam_role_policy_attachment" "ssm_policy_attachment" {
role = aws_iam_role.ssm_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
# Create IAM Instance Profile
resource "aws_iam_instance_profile" "ssm_instance_profile" {
name = "SSMInstanceProfile"
role = aws_iam_role.ssm_role.name
}
resource "aws_instance" "nodes" {
for_each = toset([for k in range(local.number_of_nodes) : tostring(k)])
vpc_security_group_ids = [aws_security_group.instance_security.id]
key_name = aws_key_pair.generated_key.key_name
ami = data.aws_ami.ubuntu.id
instance_type = "t3.medium"
subnet_id = aws_subnet.public.id
iam_instance_profile = aws_iam_instance_profile.ssm_instance_profile.name
associate_public_ip_address = true
root_block_device {
volume_type = "gp3"
volume_size = 50 # Adjust to the size (GB) you want
# iops = 3000 # Optional: If you want to specify custom IOPS for gp3
# throughput = 125 # Optional: If you want to specify custom throughput for gp3
}
tags = {
Name = "${local.name_prefix}-node${each.value}"
}
}
output "private_key" {
value = tls_private_key.kurs_key.private_key_pem
sensitive = true
}
resource "local_sensitive_file" "priv_pem_key" {
filename = "kurs_priv.pem"
directory_permission = "700"
file_permission = "600"
content = tls_private_key.kurs_key.private_key_pem
}
output "dns" {
value = jsonencode({
"nodes" : [for node in aws_instance.nodes : node.public_dns]
})
}
resource "local_file" "dns" {
depends_on = [aws_instance.nodes]
filename = "dns.json"
content = jsonencode({
"nodes" : {for k, node in aws_instance.nodes : node.tags.Name => node.public_dns}
})
}