Skip to content

Commit 32046c6

Browse files
committed
feat(infra): bootstrap terraform configuration
Signed-off-by: Theo Bob Massard <tbobm@protonmail.com>
1 parent 1567c9e commit 32046c6

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

terraform/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# ECS Migration task
2+
3+
Bootstrap an ECS task definition used as a migration job by Github Actions.
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Requirements
7+
8+
No requirements.
9+
10+
## Providers
11+
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
15+
16+
## Modules
17+
18+
No modules.
19+
20+
## Resources
21+
22+
| Name | Type |
23+
|------|------|
24+
| [aws_ecs_task.run_migration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task) | resource |
25+
| [aws_ecs_task_definition.db_migration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
26+
27+
## Inputs
28+
29+
| Name | Description | Type | Default | Required |
30+
|------|-------------|------|---------|:--------:|
31+
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the ECS Cluster where the ECS Task will be scheduled | `string` | n/a | yes |
32+
| <a name="input_container_image"></a> [container\_image](#input\_container\_image) | Container image used by the ECS Task for the Atlas container (from ECR) | `string` | n/a | yes |
33+
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | List of security group IDs to attach to the ECS Task | `list(string)` | n/a | yes |
34+
| <a name="input_subnet_ids"></a> [subnet\_ids](#input\_subnet\_ids) | List of subnet IDs to attach to the ECS Task | `list(string)` | n/a | yes |
35+
| <a name="input_task_cpu"></a> [task\_cpu](#input\_task\_cpu) | CPU allocation to the ECS Task | `number` | `512` | no |
36+
| <a name="input_task_execution_role_arn"></a> [task\_execution\_role\_arn](#input\_task\_execution\_role\_arn) | ARN of the IAM Task Execution Role which will be used by the ECS Task | `string` | n/a | yes |
37+
| <a name="input_task_memory"></a> [task\_memory](#input\_task\_memory) | RAM of the ECS Task | `number` | `1024` | no |
38+
39+
## Outputs
40+
41+
| Name | Description |
42+
|------|-------------|
43+
| <a name="output_migration_task_definition_arn"></a> [migration\_task\_definition\_arn](#output\_migration\_task\_definition\_arn) | ARN of the migration ECS Task |
44+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

terraform/main.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
resource "aws_ecs_task_definition" "db_migration" {
2+
family = "db-migration"
3+
requires_compatibilities = ["FARGATE"]
4+
network_mode = "awsvpc"
5+
cpu = var.task_cpu
6+
memory = var.task_memory
7+
execution_role_arn = var.task_execution_role_arn
8+
task_role_arn = var.task_execution_role_arn
9+
10+
container_definitions = jsonencode([
11+
{
12+
name = "atlas-migrate"
13+
image = var.container_image
14+
essential = true
15+
command = ["atlas", "migrate", "apply", "--env", "local"]
16+
environment = [
17+
{
18+
name = "ATLAS_LOG_LEVEL"
19+
value = "debug"
20+
}
21+
]
22+
}
23+
])
24+
}
25+
26+
resource "aws_ecs_task" "run_migration" {
27+
cluster = var.cluster_name
28+
task_definition = aws_ecs_task_definition.db_migration.arn
29+
launch_type = "FARGATE"
30+
31+
network_configuration {
32+
subnets = var.subnet_ids
33+
security_groups = var.security_group_ids
34+
assign_public_ip = true
35+
}
36+
37+
lifecycle {
38+
ignore_changes = [desired_count] # One-shot
39+
}
40+
}

terraform/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
output "migration_task_definition_arn" {
2+
value = aws_ecs_task_definition.db_migration.arn
3+
description = "ARN of the migration ECS Task"
4+
}
5+

terraform/variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "cluster_name" {
2+
description = "Name of the ECS Cluster where the ECS Task will be scheduled"
3+
type = string
4+
}
5+
6+
variable "subnet_ids" {
7+
description = "List of subnet IDs to attach to the ECS Task"
8+
type = list(string)
9+
}
10+
11+
variable "security_group_ids" {
12+
description = "List of security group IDs to attach to the ECS Task"
13+
type = list(string)
14+
}
15+
16+
variable "container_image" {
17+
description = "Container image used by the ECS Task for the Atlas container (from ECR)"
18+
type = string
19+
}
20+
21+
variable "task_execution_role_arn" {
22+
description = "ARN of the IAM Task Execution Role which will be used by the ECS Task"
23+
type = string
24+
}
25+
26+
variable "task_cpu" {
27+
description = "CPU allocation to the ECS Task"
28+
default = 512
29+
}
30+
31+
variable "task_memory" {
32+
description = "RAM of the ECS Task"
33+
default = 1024
34+
}

0 commit comments

Comments
 (0)