Skip to content

Commit f7fffef

Browse files
committed
ECS Attempt 3
1 parent 6f23daa commit f7fffef

File tree

8 files changed

+212
-0
lines changed

8 files changed

+212
-0
lines changed

infrastructure/applications/applications.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ module "emails" {
4040
aws.us = aws.us
4141
}
4242
}
43+
44+
module "cluster" {
45+
source = "./cluster"
46+
47+
providers = {
48+
aws = aws
49+
aws.us = aws.us
50+
}
51+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
resource "aws_autoscaling_group" "server" {
2+
name = "pythonit-${terraform.workspace}-server"
3+
vpc_zone_identifier = [data.aws_subnet.public_1a.id]
4+
desired_capacity = 1
5+
max_size = 1
6+
min_size = 1
7+
termination_policies = ["OldestInstance"]
8+
protect_from_scale_in = true
9+
10+
instance_refresh {
11+
strategy = "Rolling"
12+
preferences {
13+
min_healthy_percentage = 100
14+
max_healthy_percentage = 110
15+
scale_in_protected_instances = "Refresh"
16+
instance_warmup = 30
17+
}
18+
}
19+
20+
launch_template {
21+
id = aws_launch_template.pythonit.id
22+
version = aws_launch_template.pythonit.latest_version
23+
}
24+
25+
tag {
26+
key = "Name"
27+
value = "pythonit-${terraform.workspace}-server"
28+
propagate_at_launch = true
29+
}
30+
31+
tag {
32+
key = "AmazonECSManaged"
33+
value = true
34+
propagate_at_launch = true
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
resource "aws_iam_instance_profile" "server" {
2+
name = "pythonit-${terraform.workspace}-server"
3+
role = aws_iam_role.server.name
4+
}
5+
6+
resource "aws_iam_role" "server" {
7+
name = "pythonit-${terraform.workspace}-server-role"
8+
assume_role_policy = data.aws_iam_policy_document.server_assume_role.json
9+
}
10+
11+
resource "aws_iam_role_policy" "server" {
12+
name = "pythonit-${terraform.workspace}-server-policy"
13+
role = aws_iam_role.server.id
14+
policy = data.aws_iam_policy_document.server_role_policy.json
15+
}
16+
17+
data "aws_iam_policy_document" "server_assume_role" {
18+
statement {
19+
effect = "Allow"
20+
21+
principals {
22+
type = "Service"
23+
identifiers = ["ec2.amazonaws.com", "ecs-tasks.amazonaws.com"]
24+
}
25+
26+
actions = ["sts:AssumeRole"]
27+
}
28+
}
29+
30+
data "aws_iam_policy_document" "server_role_policy" {
31+
statement {
32+
effect = "Allow"
33+
actions = [
34+
"iam:PassRole",
35+
"ses:*",
36+
"ecs:*",
37+
"ecr:*",
38+
"ec2:DescribeInstances",
39+
]
40+
resources = [
41+
"*"
42+
]
43+
}
44+
45+
statement {
46+
effect = "Allow"
47+
actions = ["cloudwatch:PutMetricData", "logs:*"]
48+
resources = ["*"]
49+
}
50+
51+
statement {
52+
effect = "Allow"
53+
actions = ["s3:*"]
54+
resources = [
55+
"arn:aws:s3:::${terraform.workspace}-pycon-backend-media",
56+
"arn:aws:s3:::${terraform.workspace}-pycon-backend-media/*",
57+
"arn:aws:s3:::${terraform.workspace}-pretix-media",
58+
"arn:aws:s3:::${terraform.workspace}-pretix-media/*",
59+
]
60+
}
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
resource "aws_launch_template" "pythonit" {
2+
name = "pythonit-${terraform.workspace}-server"
3+
image_id = "ami-0bd650c1ca04cc1a4" #todo
4+
instance_type = "t4g.small"
5+
# user_data = base64encode(data.template_file.server_user_data.rendered)
6+
key_name = "pretix"
7+
8+
iam_instance_profile {
9+
name = aws_iam_instance_profile.server.name
10+
}
11+
12+
# block_device_mappings {
13+
# device_name = data.aws_ami.ecs.root_device_name
14+
15+
# ebs {
16+
# volume_size = 20
17+
# }
18+
# }
19+
20+
network_interfaces {
21+
associate_public_ip_address = true
22+
security_groups = [
23+
# data.aws_security_group.rds.id,
24+
# data.aws_security_group.lambda.id,
25+
# data.aws_security_group.tempone.id,
26+
aws_security_group.server.id,
27+
]
28+
subnet_id = data.aws_subnet.public_1a.id
29+
}
30+
}

infrastructure/applications/cluster/load_balancer.tf

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
resource "aws_ecs_cluster" "cluster" {
2+
name = "pythonit-${terraform.workspace}"
3+
}
4+
5+
resource "aws_ecs_capacity_provider" "ec2" {
6+
name = "pythonit-${terraform.workspace}-ec2"
7+
8+
auto_scaling_group_provider {
9+
auto_scaling_group_arn = aws_autoscaling_group.server.arn
10+
managed_termination_protection = "ENABLED"
11+
12+
managed_scaling {
13+
maximum_scaling_step_size = 2
14+
minimum_scaling_step_size = 1
15+
status = "ENABLED"
16+
target_capacity = 1
17+
instance_warmup_period = 60
18+
}
19+
}
20+
}
21+
22+
resource "aws_ecs_cluster_capacity_providers" "server" {
23+
cluster_name = aws_ecs_cluster.cluster.name
24+
capacity_providers = [
25+
aws_ecs_capacity_provider.ec2.name,
26+
]
27+
28+
default_capacity_provider_strategy {
29+
base = 1
30+
weight = 100
31+
capacity_provider = aws_ecs_capacity_provider.ec2.name
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "aws_security_group" "server" {
2+
name = "${terraform.workspace}-server"
3+
description = "${terraform.workspace} server"
4+
vpc_id = data.aws_vpc.default.id
5+
}
6+
7+
resource "aws_security_group_rule" "out_all" {
8+
type = "egress"
9+
from_port = 0
10+
to_port = 0
11+
protocol = "all"
12+
cidr_blocks = ["0.0.0.0/0"]
13+
security_group_id = aws_security_group.server.id
14+
}
15+
16+
resource "aws_security_group_rule" "web_http" {
17+
type = "ingress"
18+
from_port = 80
19+
to_port = 80
20+
protocol = "tcp"
21+
cidr_blocks = ["0.0.0.0/0"]
22+
security_group_id = aws_security_group.server.id
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
data "aws_vpc" "default" {
2+
filter {
3+
name = "tag:Name"
4+
values = ["pythonit-vpc"]
5+
}
6+
}
7+
8+
data "aws_subnet" "public_1a" {
9+
vpc_id = data.aws_vpc.default.id
10+
11+
filter {
12+
name = "tag:Type"
13+
values = ["public"]
14+
}
15+
16+
filter {
17+
name = "tag:AZ"
18+
values = ["eu-central-1a"]
19+
}
20+
}

0 commit comments

Comments
 (0)