Skip to content

Commit ab46de4

Browse files
author
kfc-manager
committed
refa: switch to Fargate ECS cluster
1 parent 1b63d33 commit ab46de4

30 files changed

+1212
-1319
lines changed

.github/diagrams/cluster-dark.png

864 KB
Loading

.github/diagrams/cluster-light.png

880 KB
Loading
857 KB
Loading

.github/diagrams/cluster.drawio

Lines changed: 151 additions & 0 deletions
Large diffs are not rendered by default.

.gitignore

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# Editor directories and files
2-
.vscode/*
3-
!.vscode/extensions.json
4-
.idea
5-
.DS_Store
6-
*.suo
7-
*.ntvs*
8-
*.njsproj
9-
*.sln
10-
*.sw?
2+
**/.vscode/*
3+
**/!.vscode/extensions.json
4+
**/.idea
5+
**/.DS_Store
6+
**/*.suo
7+
**/*.ntvs*
8+
**/*.njsproj
9+
**/*.sln
10+
**/*.sw?
1111

12-
# Terraform files
13-
**/.terraform/*
14-
*.tfstate
15-
*.tfstate.*
16-
crash.log
17-
crash.*.log
18-
.terraform.lock.hcl
19-
*.tfvars
20-
*.tfvars.json
21-
override.tf
22-
override.tf.json
23-
*_override.tf
24-
*_override.tf.json
25-
*tfplan*
26-
.terraformrc
27-
terraform.rc
12+
**/# Terraform files
13+
**/**/.terraform/*
14+
**/*.tfstate
15+
**/*.tfstate.*
16+
**/crash.log
17+
**/crash.*.log
18+
**/.terraform.lock.hcl
19+
**/*.tfvars
20+
**/*.tfvars.json
21+
**/override.tf
22+
**/override.tf.json
23+
**/*_override.tf
24+
**/*_override.tf.json
25+
**/*tfplan*
26+
**/.terraformrc
27+
**/terraform.rc

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Modules: Cluster
2+
3+
This is an agglomeration of Terraform modules relevant to build a Fargate ECS cluster. After creating a base ECS cluster any amount of services can be added to the cluster. With service discovery private DNS namespaces can be created for cluster internal communication between services. Services can also be autoscaled and publicly exposed by adding them to a target group of the load balancer module.
4+
5+
![Cluster visualized](.github/diagrams/cluster-transparent.png)
6+
7+
## Contents
8+
9+
- [Requirements](#requirements)
10+
- [Examples](#examples)
11+
12+
## Requirements
13+
14+
| Name | Version |
15+
| --------- | ------- |
16+
| terraform | >= 1.0 |
17+
| aws | >= 5.20 |
18+
19+
## Examples
20+
21+
```hcl
22+
module "cluster" {
23+
source = "github.com/custom-terraform-aws-modules/cluster/cluster"
24+
25+
identifier = "example-cluster"
26+
27+
log_config = {
28+
retention_in_days = 7
29+
}
30+
31+
tags = {
32+
Project = "example-project"
33+
Environment = "dev"
34+
}
35+
}
36+
37+
module "load_balancer" {
38+
source = "github.com/custom-terraform-aws-modules/cluster/load-balancer"
39+
40+
identifier = "example-load-balancer"
41+
vpc_id = "vpc-1234567890"
42+
subnets = ["subnet-1", "subnet-2", "subnet-3"]
43+
44+
target_groups = [
45+
{
46+
name = "first-target-group"
47+
host_domain = "example.com"
48+
certificate_arn = "arn:aws:acm:eu-central-1:1234567890:example"
49+
health_check_path = "/health"
50+
}
51+
]
52+
53+
tags = {
54+
Project = "example-project"
55+
Environment = "dev"
56+
}
57+
}
58+
59+
module "cache_service" {
60+
source = "github.com/custom-terraform-aws-modules/cluster/service"
61+
62+
identifier = "cache-service"
63+
cluster_id = module.cluster.id
64+
region = "eu-central-1"
65+
cpu_architecture = "ARM64"
66+
dns_namespace = "cache.local"
67+
vpc_id = "vpc-1234567890"
68+
execution_role_arn = "arn:aws:iam::1234567890:role/execution-role"
69+
container_port = 6379
70+
subnets = ["subnet-1", "subnet-2", "subnet-3"]
71+
task_count = 1
72+
task_cpu = 256
73+
task_memory = 512
74+
75+
image = {
76+
uri = "redis:latest"
77+
}
78+
79+
log_config = {
80+
retention_in_days = 7
81+
}
82+
83+
tags = {
84+
Project = "example-project"
85+
Environment = "dev"
86+
}
87+
}
88+
89+
module "web_server_service" {
90+
source = "github.com/custom-terraform-aws-modules/cluster/service"
91+
92+
identifier = "web-server-service"
93+
cluster_id = module.cluster.id
94+
region = "eu-central-1"
95+
cpu_architecture = "ARM64"
96+
vpc_id = "vpc-1234567890"
97+
execution_role_arn = "arn:aws:iam::1234567890:role/execution-role"
98+
container_port = 8000
99+
subnets = ["subnet-1", "subnet-2", "subnet-3"]
100+
security_groups = [module.cache_service.security_group]
101+
target_group = module.load_balancer.target_groups[0]
102+
task_count = 3
103+
task_cpu = 256
104+
task_memory = 512
105+
106+
autoscaling = {
107+
min_count = 3
108+
max_count = 5
109+
}
110+
111+
log_config = {
112+
retention_in_days = 7
113+
}
114+
115+
tags = {
116+
Project = "example-project"
117+
Environment = "dev"
118+
}
119+
}
120+
```

aws-fluentbit.tpl

Lines changed: 0 additions & 26 deletions
This file was deleted.

cluster/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Cluster: Cluster
2+
3+
This module creates the ECS cluster and is the foundation for the other modules of this repository to build on top of.
4+
5+
## Contents
6+
7+
- [Requirements](#requirements)
8+
- [Inputs](#inputs)
9+
- [Outputs](#outputs)
10+
11+
## Requirements
12+
13+
| Name | Version |
14+
| --------- | ------- |
15+
| terraform | >= 1.0 |
16+
| aws | >= 5.20 |
17+
18+
## Inputs
19+
20+
| Name | Description | Type | Default | Required |
21+
| ---------- | ------------------------------------------------------------------------ | ------------- | ------- | :------: |
22+
| identifier | The unique identifier to differentiate resources. | `string` | n/a | yes |
23+
| log_config | Object to define logging configuration for the ECS master to CloudWatch. | `object` | null | no |
24+
| tags | A map of tags to add to all resources. | `map(string)` | {} | no |
25+
26+
### `log_config`
27+
28+
| Name | Description | Type | Default | Required |
29+
| ----------------- | -------------------------------------------------------------------------------------------------------------------------- | -------- | ------- | :------: |
30+
| retention_in_days | Specifies the number of days the log events shall be retained. Valid values: 1, 3, 5, 7, 14, 30, 365 and 0 (never expire). | `number` | n/a | yes |
31+
32+
## Outputs
33+
34+
| Name | Description |
35+
| ------------------ | ------------------------------------------------------------------------- |
36+
| id | The ID of the ECS cluster. |
37+
| execution_role_arn | The ARN of the execution IAM role for the ECS services. |
38+
| log_group_arn | The ARN of the CloudWatch log group created for the ECS master to log to. |

cluster/main.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
resource "aws_cloudwatch_log_group" "main" {
2+
count = var.log_config != null ? 1 : 0
3+
name = var.identifier
4+
retention_in_days = var.log_config["retention_in_days"]
5+
6+
tags = var.tags
7+
}
8+
9+
resource "aws_ecs_cluster" "main" {
10+
name = var.identifier
11+
12+
dynamic "configuration" {
13+
for_each = var.log_config != null ? [1] : []
14+
content {
15+
execute_command_configuration {
16+
logging = "OVERRIDE"
17+
18+
log_configuration {
19+
cloud_watch_log_group_name = aws_cloudwatch_log_group.main[0].name
20+
}
21+
}
22+
}
23+
}
24+
25+
tags = var.tags
26+
}
27+
28+
data "aws_iam_policy_document" "assume_role" {
29+
statement {
30+
actions = ["sts:AssumeRole"]
31+
32+
principals {
33+
type = "Service"
34+
identifiers = ["ecs-tasks.amazonaws.com"]
35+
}
36+
}
37+
}
38+
39+
resource "aws_iam_role" "main" {
40+
name = "${var.identifier}-ExecutionRoleForTasks"
41+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
42+
43+
tags = var.tags
44+
}
45+
46+
resource "aws_iam_role_policy_attachment" "main" {
47+
role = aws_iam_role.main.name
48+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
49+
}

cluster/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "id" {
2+
description = "The ID of the ECS cluster."
3+
value = aws_ecs_cluster.main.id
4+
}
5+
6+
output "execution_role_arn" {
7+
description = "The ARN of the execution IAM role for the ECS services."
8+
value = aws_iam_role.main.arn
9+
}
10+
11+
output "log_group_arn" {
12+
description = "The ARN of the CloudWatch log group."
13+
value = try(aws_cloudwatch_log_group.main[0].arn, null)
14+
}

0 commit comments

Comments
 (0)