Skip to content

Commit 68c4170

Browse files
committed
More
1 parent 93b6d40 commit 68c4170

File tree

27 files changed

+222
-764
lines changed

27 files changed

+222
-764
lines changed

infrastructure/applications/applications.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ locals {
1212

1313
module "pretix" {
1414
source = "./pretix"
15-
count = local.deploy_pretix ? 1 : 0
15+
count = 1
1616
ecs_arm_ami = local.ecs_arm_ami
17+
server_ip = module.cluster.server_ip
18+
cluster_id = module.cluster.cluster_id
19+
logs_group_name = module.cluster.logs_group_name
1720
}
1821

1922
module "pycon_backend" {
2023
source = "./pycon_backend"
2124
ecs_arm_ami = local.ecs_arm_ami
2225
cluster_id = module.cluster.cluster_id
23-
service_connect_namespace = module.cluster.service_connect_namespace
2426
security_group_id = module.cluster.security_group_id
2527
server_ip = module.cluster.server_ip
28+
logs_group_name = module.cluster.logs_group_name
2629

2730
providers = {
2831
aws = aws
@@ -54,3 +57,7 @@ module "cluster" {
5457
aws.us = aws.us
5558
}
5659
}
60+
61+
output "server_public_ip" {
62+
value = module.cluster.server_public_ip
63+
}

infrastructure/components/cloudfront/main.tf renamed to infrastructure/applications/cluster/cloudfront.tf

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
1+
locals {
2+
pycon_web_domain = local.is_prod ? "admin.pycon.it" : "${terraform.workspace}-admin.pycon.it"
3+
pretix_web_domain = local.is_prod ? "tickets.pycon.it" : "${terraform.workspace}-tickets.pycon.it"
4+
}
5+
6+
data "aws_cloudfront_origin_request_policy" "all_viewer" {
7+
name = "Managed-AllViewer"
8+
}
9+
110
data "aws_cloudfront_cache_policy" "caching_disabled" {
211
name = "Managed-CachingDisabled"
312
}
413

5-
data "aws_cloudfront_origin_request_policy" "all_viewer_except_host_header" {
6-
name = "Managed-AllViewerExceptHostHeader"
14+
data "aws_acm_certificate" "cert" {
15+
domain = "*.pycon.it"
16+
statuses = ["ISSUED"]
17+
provider = aws.us
718
}
819

920
resource "aws_cloudfront_distribution" "application" {
1021
enabled = true
1122
is_ipv6_enabled = true
12-
comment = "${terraform.workspace}-${var.application}"
23+
comment = "${terraform.workspace} server"
1324
wait_for_deployment = false
14-
aliases = [var.domain]
25+
aliases = [
26+
local.pycon_web_domain,
27+
local.pretix_web_domain
28+
]
1529

1630
origin {
17-
domain_name = var.origin_url
31+
domain_name = aws_eip.server.public_dns
1832
origin_id = "default"
1933

2034
custom_origin_config {
21-
origin_protocol_policy = "https-only"
35+
origin_protocol_policy = "http-only"
2236
http_port = "80"
2337
https_port = "443"
2438
origin_ssl_protocols = ["TLSv1"]
@@ -29,7 +43,7 @@ resource "aws_cloudfront_distribution" "application" {
2943
cloudfront_default_certificate = false
3044
minimum_protocol_version = "TLSv1"
3145
ssl_support_method = "sni-only"
32-
acm_certificate_arn = var.certificate_arn
46+
acm_certificate_arn = data.aws_acm_certificate.cert.arn
3347
}
3448

3549
default_cache_behavior {
@@ -38,16 +52,10 @@ resource "aws_cloudfront_distribution" "application" {
3852
target_origin_id = "default"
3953

4054
cache_policy_id = data.aws_cloudfront_cache_policy.caching_disabled.id
41-
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer_except_host_header.id
55+
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer.id
4256

4357
viewer_protocol_policy = "redirect-to-https"
4458
compress = true
45-
46-
lambda_function_association {
47-
event_type = "viewer-request"
48-
lambda_arn = var.forward_host_header_lambda_arn
49-
include_body = false
50-
}
5159
}
5260

5361
restrictions {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
data "aws_route53_zone" "zone" {
2+
name = "pycon.it"
3+
}
4+
5+
resource "aws_route53_record" "web_pycon" {
6+
zone_id = data.aws_route53_zone.zone.zone_id
7+
name = local.pycon_web_domain
8+
type = "A"
9+
10+
alias {
11+
name = aws_cloudfront_distribution.application.domain_name
12+
zone_id = aws_cloudfront_distribution.application.hosted_zone_id
13+
evaluate_target_health = false
14+
}
15+
}
16+
17+
resource "aws_route53_record" "web_tickets" {
18+
zone_id = data.aws_route53_zone.zone.zone_id
19+
name = local.pretix_web_domain
20+
type = "A"
21+
22+
alias {
23+
name = aws_cloudfront_distribution.application.domain_name
24+
zone_id = aws_cloudfront_distribution.application.hosted_zone_id
25+
evaluate_target_health = false
26+
}
27+
}

infrastructure/applications/cluster/iam.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,24 @@ data "aws_iam_policy_document" "server_role_policy" {
5858
"arn:aws:s3:::${terraform.workspace}-pretix-media/*",
5959
]
6060
}
61+
62+
statement {
63+
actions = [
64+
"sns:CreatePlatformEndpoint",
65+
"sns:Publish"
66+
]
67+
resources = ["*"]
68+
effect = "Allow"
69+
}
70+
71+
statement {
72+
actions = [
73+
"sqs:SendMessage",
74+
"sqs:DeleteMessage",
75+
"sqs:GetQueueAttributes",
76+
"sqs:ReceiveMessage",
77+
]
78+
resources = ["*"]
79+
effect = "Allow"
80+
}
6181
}

infrastructure/applications/cluster/load_balancer_task.tf

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ resource "aws_ecs_task_definition" "traefik" {
2525
name = "TRAEFIK_ENTRYPOINTS_WEB_ADDRESS",
2626
value = ":80"
2727
},
28+
{
29+
name = "TRAEFIK_LOG_LEVEL",
30+
value = "DEBUG"
31+
}
2832
]
2933

3034
portMappings = [
@@ -76,10 +80,4 @@ resource "aws_ecs_service" "traefik" {
7680
desired_count = 1
7781
deployment_minimum_healthy_percent = 0
7882
deployment_maximum_percent = 100
79-
80-
lifecycle {
81-
ignore_changes = [
82-
capacity_provider_strategy
83-
]
84-
}
8583
}

infrastructure/applications/cluster/logs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ resource "aws_cloudwatch_log_group" "cluster" {
22
name = "/ecs/pythonit-${terraform.workspace}-cluster"
33
retention_in_days = 3
44
}
5+
6+
7+
output "logs_group_name" {
8+
value = aws_cloudwatch_log_group.cluster.name
9+
}

infrastructure/applications/cluster/main.tf

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
is_prod = terraform.workspace == "production"
3+
}
4+
15
resource "aws_ecs_cluster" "cluster" {
26
name = "pythonit-${terraform.workspace}"
37
}
@@ -6,15 +10,6 @@ output "cluster_id" {
610
value = aws_ecs_cluster.cluster.id
711
}
812

9-
resource "aws_service_discovery_http_namespace" "cluster" {
10-
name = "pythonit-${terraform.workspace}"
11-
description = "pythonit-${terraform.workspace} service discovery namespace"
12-
}
13-
14-
output "service_connect_namespace" {
15-
value = aws_service_discovery_http_namespace.cluster.arn
16-
}
17-
1813
resource "aws_ecs_account_setting_default" "trunking" {
1914
name = "awsvpcTrunking"
2015
value = "enabled"

infrastructure/applications/cluster/server.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
locals {
22
server_user_data = templatefile("${path.module}/userdata.sh", {
33
ecs_cluster = aws_ecs_cluster.cluster.name
4-
swap_size = "1G"
4+
swap_size = "4G"
55
})
66
}
77

@@ -11,8 +11,8 @@ resource "aws_eip" "server" {
1111
}
1212

1313
resource "aws_instance" "server" {
14-
ami = "ami-01c0b647efcf28a90"
15-
instance_type = "t4g.small"
14+
ami = "ami-0d683ccb0045afce1"
15+
instance_type = "t4g.large"
1616
subnet_id = data.aws_subnet.public_1a.id
1717
availability_zone = "eu-central-1a"
1818
vpc_security_group_ids = [
@@ -53,3 +53,7 @@ resource "aws_volume_attachment" "redis_data_attachment" {
5353
output "server_ip" {
5454
value = aws_instance.server.private_ip
5555
}
56+
57+
output "server_public_ip" {
58+
value = aws_eip.server.public_ip
59+
}

infrastructure/applications/cluster/userdata.sh

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,76 @@ swapon /swapfile
1010
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
1111

1212
mkdir /redis-data -p
13-
echo '/dev/nvme1n1 /redis-data xfs defaults 0 0' >> /etc/fstab
13+
echo '/dev/nvme1n1 /redis-data xfs defaults,nofail 0 2' >> /etc/fstab
1414
mount -a
15+
16+
# Reclaim unused Docker disk space
17+
cat << "EOF" > /usr/local/bin/claimspace.sh
18+
#!/bin/bash
19+
# Run fstrim on the host OS periodically to reclaim the unused container data blocks
20+
docker ps -q | xargs docker inspect --format='{{ .State.Pid }}' | xargs -IZ sudo fstrim /proc/Z/root/
21+
exit $?
22+
EOF
23+
24+
# Run pretix cron
25+
cat << "EOF" > /usr/local/bin/pretixcron.sh
26+
#!/bin/bash
27+
docker exec `docker ps --no-trunc -q --filter="name=.*pretix.*" | head -n 1` pretix cron
28+
exit 0
29+
EOF
30+
31+
chmod +x /usr/local/bin/claimspace.sh
32+
chmod +x /usr/local/bin/pretixcron.sh
33+
34+
cat << "EOF" > /etc/systemd/system/claimspace.service
35+
[Unit]
36+
Description=Run fstrim on Docker containers
37+
38+
[Service]
39+
Type=oneshot
40+
ExecStart=/usr/local/bin/claimspace.sh
41+
42+
[Install]
43+
WantedBy=multi-user.target
44+
EOF
45+
46+
cat << "EOF" > /etc/systemd/system/pretixcron.service
47+
[Unit]
48+
Description=Run Pretix cron job
49+
50+
[Service]
51+
Type=oneshot
52+
ExecStart=/usr/local/bin/pretixcron.sh
53+
54+
[Install]
55+
WantedBy=multi-user.target
56+
EOF
57+
58+
cat << "EOF" > /etc/systemd/system/claimspace.timer
59+
[Unit]
60+
Description=Run fstrim on Docker containers daily
61+
62+
[Timer]
63+
OnCalendar=daily
64+
Persistent=true
65+
66+
[Install]
67+
WantedBy=timers.target
68+
EOF
69+
70+
cat << "EOF" > /etc/systemd/system/pretixcron.timer
71+
[Unit]
72+
Description=Run Pretix cron job
73+
74+
[Timer]
75+
OnCalendar=*:15,45
76+
Persistent=true
77+
78+
[Install]
79+
WantedBy=timers.target
80+
EOF
81+
82+
systemctl daemon-reload
83+
84+
systemctl enable --now claimspace.timer
85+
systemctl enable --now pretixcron.timer

infrastructure/applications/database/db.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ resource "aws_db_instance" "database" {
1919
allow_major_version_upgrade = true
2020
engine_version = "14.12"
2121
instance_class = "db.t4g.micro"
22-
db_name = "${local.normalized_workspace}backend"
22+
db_name = local.is_prod ? "${local.normalized_workspace}backend" : "pycon"
2323
username = "root"
2424
password = module.common_secrets.value.database_password
2525
multi_az = "false"

0 commit comments

Comments
 (0)