Skip to content

Commit 1ffc74a

Browse files
committed
wip
1 parent f7fffef commit 1ffc74a

File tree

8 files changed

+232
-0
lines changed

8 files changed

+232
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform 1.9.8
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
locals {
2+
load_balancer_user_data = templatefile("${path.module}/userdata.sh", {
3+
ecs_cluster = aws_ecs_cluster.cluster.name
4+
swap_size = "1G"
5+
role = "load_balancer"
6+
})
7+
}
8+
9+
resource "aws_instance" "load_balancer" {
10+
ami = "ami-09c79d1104c5634b4" #todo
11+
instance_type = "t4g.nano"
12+
subnet_id = data.aws_subnet.public_1a.id
13+
availability_zone = "eu-central-1a"
14+
vpc_security_group_ids = [
15+
aws_security_group.load_balancer.id,
16+
]
17+
source_dest_check = false
18+
user_data = local.load_balancer_user_data
19+
iam_instance_profile = aws_iam_instance_profile.load_balancer.name
20+
key_name = "pretix"
21+
user_data_replace_on_change = true
22+
23+
root_block_device {
24+
volume_size = 30
25+
}
26+
27+
tags = {
28+
Name = "pythonit-${terraform.workspace}-load-balancer"
29+
Role = "load_balancer"
30+
}
31+
}
32+
33+
output "load_balancer_public_ip" {
34+
value = aws_instance.load_balancer.public_ip
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
resource "aws_iam_role" "load_balancer" {
2+
name = "pythonit-${terraform.workspace}-load-balancer"
3+
assume_role_policy = data.aws_iam_policy_document.lb_assume_role.json
4+
}
5+
6+
resource "aws_iam_instance_profile" "load_balancer" {
7+
name = "pythonit-${terraform.workspace}-load-balancer"
8+
role = aws_iam_role.load_balancer.name
9+
}
10+
11+
data "aws_iam_policy_document" "lb_assume_role" {
12+
statement {
13+
effect = "Allow"
14+
15+
principals {
16+
type = "Service"
17+
identifiers = ["ec2.amazonaws.com", "ecs-tasks.amazonaws.com"]
18+
}
19+
20+
actions = ["sts:AssumeRole"]
21+
}
22+
}
23+
24+
resource "aws_iam_role_policy" "lb" {
25+
name = "pythonit-${terraform.workspace}-load-balancer-policy"
26+
role = aws_iam_role.load_balancer.id
27+
policy = data.aws_iam_policy_document.lb_role_policy.json
28+
}
29+
30+
data "aws_iam_policy_document" "lb_role_policy" {
31+
statement {
32+
effect = "Allow"
33+
actions = [
34+
"iam:PassRole",
35+
"ecs:*",
36+
"ecr:*",
37+
"ec2:DescribeInstances",
38+
]
39+
resources = [
40+
"*"
41+
]
42+
}
43+
44+
statement {
45+
effect = "Allow"
46+
actions = ["cloudwatch:PutMetricData", "logs:*"]
47+
resources = ["*"]
48+
}
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
resource "aws_security_group" "load_balancer" {
2+
name = "pythonit-${terraform.workspace}-load-balancer"
3+
description = "pythonit-${terraform.workspace} load balancer"
4+
vpc_id = data.aws_vpc.default.id
5+
}
6+
7+
resource "aws_security_group_rule" "lb_web_http" {
8+
type = "ingress"
9+
from_port = 80
10+
to_port = 80
11+
protocol = "tcp"
12+
cidr_blocks = ["0.0.0.0/0"]
13+
security_group_id = aws_security_group.load_balancer.id
14+
}
15+
16+
resource "aws_security_group_rule" "lb_ssh" {
17+
type = "ingress"
18+
from_port = 22
19+
to_port = 22
20+
protocol = "tcp"
21+
cidr_blocks = ["0.0.0.0/0"]
22+
security_group_id = aws_security_group.load_balancer.id
23+
}
24+
25+
resource "aws_security_group_rule" "lb_out_all" {
26+
type = "egress"
27+
from_port = 0
28+
to_port = 0
29+
protocol = "all"
30+
cidr_blocks = ["0.0.0.0/0"]
31+
security_group_id = aws_security_group.load_balancer.id
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "aws_cloudwatch_log_group" "cluster" {
2+
name = "/ecs/pythonit-${terraform.workspace}-cluster"
3+
retention_in_days = 3
4+
}

infrastructure/applications/cluster/security.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ resource "aws_security_group_rule" "web_http" {
2121
cidr_blocks = ["0.0.0.0/0"]
2222
security_group_id = aws_security_group.server.id
2323
}
24+
25+
resource "aws_security_group_rule" "server_ssh" {
26+
type = "ingress"
27+
from_port = 22
28+
to_port = 22
29+
protocol = "tcp"
30+
cidr_blocks = ["0.0.0.0/0"]
31+
security_group_id = aws_security_group.server.id
32+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
resource "aws_ecs_task_definition" "traefik" {
2+
family = "pythonit-${terraform.workspace}-traefik"
3+
container_definitions = jsonencode([
4+
{
5+
name = "traefik"
6+
image = "traefik:v3.1.2"
7+
memoryReservation = 200
8+
essential = true
9+
10+
environment = [
11+
{
12+
name = "TRAEFIK_PROVIDERS_ECS_CLUSTERS"
13+
value = aws_ecs_cluster.cluster.name
14+
},
15+
{
16+
name = "TRAEFIK_PROVIDERS_ECS_AUTODISCOVERCLUSTERS"
17+
value = "false",
18+
},
19+
{
20+
name = "TRAEFIK_PROVIDERS_ECS_EXPOSEDBYDEFAULT",
21+
value = "false",
22+
},
23+
{
24+
name = "TRAEFIK_ENTRYPOINTS_WEB_ADDRESS",
25+
value = ":80"
26+
},
27+
]
28+
29+
portMappings = [
30+
{
31+
containerPort = 80
32+
hostPort = 80
33+
},
34+
]
35+
36+
mountPoints = []
37+
systemControls = [
38+
{
39+
"namespace" : "net.core.somaxconn",
40+
"value" : "4096"
41+
}
42+
]
43+
44+
logConfiguration = {
45+
logDriver = "awslogs"
46+
options = {
47+
"awslogs-group" = aws_cloudwatch_log_group.cluster.name
48+
"awslogs-region" = "eu-central-1"
49+
"awslogs-stream-prefix" = "traefik"
50+
}
51+
}
52+
53+
healthCheck = {
54+
retries = 3
55+
command = [
56+
"CMD-SHELL",
57+
"echo 4"
58+
]
59+
timeout = 3
60+
interval = 10
61+
}
62+
63+
stopTimeout = 300
64+
}
65+
])
66+
67+
requires_compatibilities = []
68+
tags = {}
69+
}
70+
71+
resource "aws_ecs_service" "traefik" {
72+
name = "traefik"
73+
cluster = aws_ecs_cluster.cluster.id
74+
task_definition = aws_ecs_task_definition.traefik.arn
75+
desired_count = 1
76+
deployment_minimum_healthy_percent = 0
77+
deployment_maximum_percent = 100
78+
79+
lifecycle {
80+
ignore_changes = [
81+
capacity_provider_strategy
82+
]
83+
}
84+
85+
# placement_constraints {
86+
# type = "memberOf"
87+
# expression = "attribute:role == load_balancer"
88+
# }
89+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -x
3+
4+
# change 2
5+
echo "ECS_CLUSTER=${ecs_cluster}" > /etc/ecs/ecs.config
6+
echo "ECS_INSTANCE_ATTRIBUTES={\"role\": \"${role}\"}" >> /etc/ecs/ecs.config
7+
8+
sudo su
9+
sudo dd if=/dev/zero of=/swapfile bs=${swap_size} count=32
10+
sudo chmod 600 /swapfile
11+
sudo mkswap /swapfile
12+
sudo swapon /swapfile
13+
sudo echo "/swapfile swap swap defaults 0 0" >> /etc/fstab

0 commit comments

Comments
 (0)