Skip to content

Commit b0a30db

Browse files
authored
Merge pull request #6 from tbobm/feat/allow-to-toggle-addons
feat: allow to toggle addons on and off
2 parents 439a728 + 2039a92 commit b0a30db

File tree

10 files changed

+150
-56
lines changed

10 files changed

+150
-56
lines changed

README.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Simple Terraform module to deploy an ECS task using AWS Fargate including addons
44

55
## Example usage
66

7+
### Bootstrapped setup
8+
79
```hcl
810
module "ecr" {
911
source = "tbobm/ecs/aws"
@@ -19,6 +21,36 @@ module "ecr" {
1921
}
2022
```
2123

24+
### Restricted setup
25+
26+
Simply setup an ECS Cluster and Service based on `container.image`.
27+
28+
```hcl
29+
module "ecr" {
30+
source = "tbobm/ecs/aws"
31+
version = "0.0.1"
32+
33+
container = {
34+
image = "particuleio/helloworld"
35+
}
36+
37+
vpc = {
38+
id = "vpc-xxxxxxxx"
39+
}
40+
addons = {
41+
iam = {
42+
enabled = false
43+
}
44+
ecr = {
45+
enabled = false
46+
}
47+
loadbalancer = {
48+
enabled = false
49+
}
50+
}
51+
}
52+
```
53+
2254
## Doc generation
2355

2456
Code formatting and documentation for variables and outputs is generated using
@@ -60,27 +92,28 @@ No modules.
6092

6193
| Name | Type |
6294
|------|------|
63-
| [aws_ecr_repository.repository](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
64-
| [aws_ecr_repository_policy.policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository_policy) | resource |
65-
| [aws_ecs_cluster.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster) | resource |
66-
| [aws_ecs_service.service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
67-
| [aws_ecs_task_definition.task](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
95+
| [aws_ecr_repository.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
96+
| [aws_ecr_repository_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository_policy) | resource |
97+
| [aws_ecs_cluster.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster) | resource |
98+
| [aws_ecs_service.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
99+
| [aws_ecs_task_definition.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
68100
| [aws_iam_access_key.publisher](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_access_key) | resource |
69-
| [aws_iam_role.fargate](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
70-
| [aws_iam_role_policy.fargate](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
101+
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
102+
| [aws_iam_role_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
71103
| [aws_iam_user.publisher](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
72104
| [aws_iam_user_policy.publisher](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy) | resource |
73-
| [aws_lb.alb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb) | resource |
74-
| [aws_lb_listener.front_end](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource |
75-
| [aws_lb_target_group.group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
76-
| [aws_subnet.subnets](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source |
77-
| [aws_subnet_ids.subnets](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
78-
| [aws_vpc.vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
105+
| [aws_lb.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb) | resource |
106+
| [aws_lb_listener.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource |
107+
| [aws_lb_target_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
108+
| [aws_subnet.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source |
109+
| [aws_subnet_ids.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
110+
| [aws_vpc.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
79111

80112
## Inputs
81113

82114
| Name | Description | Type | Default | Required |
83115
|------|-------------|------|---------|:--------:|
116+
| <a name="input_addons"></a> [addons](#input\_addons) | Configuration of each addon that can be toggles on and off | `any` | `{}` | no |
84117
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region | `string` | `"eu-west-3"` | no |
85118
| <a name="input_container"></a> [container](#input\_container) | Container configuration to deploy | `any` | `{}` | no |
86119
| <a name="input_ecr_values"></a> [ecr\_values](#input\_ecr\_values) | AWS ECR configuration | `any` | `{}` | no |
@@ -92,6 +125,7 @@ No modules.
92125

93126
| Name | Description |
94127
|------|-------------|
128+
| <a name="output_addons"></a> [addons](#output\_addons) | The Addons configuration |
95129
| <a name="output_app_url"></a> [app\_url](#output\_app\_url) | The public ALB DNS |
96130
| <a name="output_aws_region"></a> [aws\_region](#output\_aws\_region) | The AWS region used |
97131
| <a name="output_container_name"></a> [container\_name](#output\_container\_name) | Container name for the ECS task |

ecr.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
resource "aws_ecr_repository" "repository" {
1+
resource "aws_ecr_repository" "this" {
2+
count = local.addons.ecr.enable ? 1 : 0
3+
24
name = local.ecr["repository_name"]
35
image_tag_mutability = "MUTABLE"
46
}
57

6-
resource "aws_ecr_repository_policy" "policy" {
7-
repository = aws_ecr_repository.repository.name
8+
resource "aws_ecr_repository_policy" "this" {
9+
count = local.addons.ecr.enable ? 1 : 0
10+
11+
repository = aws_ecr_repository.this.0.name
812
policy = <<EOF
913
{
1014
"Version": "2008-10-17",

ecs.tf

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resource "aws_ecs_cluster" "cluster" {
1+
resource "aws_ecs_cluster" "this" {
22
name = local.ecs["cluster_name"]
33
capacity_providers = ["FARGATE"]
44

@@ -8,12 +8,12 @@ resource "aws_ecs_cluster" "cluster" {
88
}
99
}
1010

11-
resource "aws_ecs_task_definition" "task" {
11+
resource "aws_ecs_task_definition" "this" {
1212
family = "service"
1313
requires_compatibilities = [
1414
"FARGATE",
1515
]
16-
execution_role_arn = aws_iam_role.fargate.arn
16+
execution_role_arn = aws_iam_role.this.arn
1717
network_mode = "awsvpc"
1818
cpu = 256
1919
memory = 512
@@ -33,21 +33,25 @@ resource "aws_ecs_task_definition" "task" {
3333
])
3434
}
3535

36-
resource "aws_ecs_service" "service" {
36+
resource "aws_ecs_service" "this" {
3737
name = local.ecs.service_name
38-
cluster = aws_ecs_cluster.cluster.id
39-
task_definition = aws_ecs_task_definition.task.arn
38+
cluster = aws_ecs_cluster.this.id
39+
task_definition = aws_ecs_task_definition.this.arn
4040
desired_count = 1
4141

4242
network_configuration {
43-
subnets = data.aws_subnet.subnets.*.id
43+
subnets = data.aws_subnet_ids.this.ids
4444
assign_public_ip = true
4545
}
4646

47-
load_balancer {
48-
target_group_arn = aws_lb_target_group.group.arn
49-
container_name = local.container.name
50-
container_port = 80
47+
dynamic "load_balancer" {
48+
for_each = local.addons.loadbalancer.enable ? [1] : []
49+
50+
content {
51+
target_group_arn = aws_lb_target_group.this.0.arn
52+
container_name = local.container.name
53+
container_port = 80
54+
}
5155
}
5256
deployment_controller {
5357
type = "ECS"

examples/main.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module "ecs" {
2-
source = "tbobm/ecs/aws"
3-
version = "0.0.1"
2+
source = "../"
43

54
container = {
65
image = var.container.image
@@ -9,6 +8,18 @@ module "ecs" {
98
vpc = {
109
id = var.vpc.id
1110
}
11+
12+
addons = {
13+
loadbalancer = {
14+
enable = false
15+
}
16+
ecr = {
17+
enable = false
18+
}
19+
iam = {
20+
enable = false
21+
}
22+
}
1223
}
1324

1425
provider "aws" {
@@ -35,3 +46,7 @@ output "this" {
3546
sensitive = true
3647
description = "The ECS module outputs."
3748
}
49+
50+
output "addons" {
51+
value = module.ecs.addons
52+
}

iam.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
resource "aws_iam_user" "publisher" {
2+
count = local.addons.iam.enable ? 1 : 0
3+
24
name = "ecr-publisher"
35
path = "/serviceaccounts/"
46
}
57

6-
resource "aws_iam_role" "fargate" {
8+
resource "aws_iam_role" "this" {
79
name = "fargate-role"
810
path = "/serviceaccounts/"
911
assume_role_policy = jsonencode({
@@ -26,8 +28,9 @@ resource "aws_iam_role" "fargate" {
2628

2729

2830
resource "aws_iam_user_policy" "publisher" {
29-
name = "ecr-publisher"
30-
user = aws_iam_user.publisher.name
31+
count = local.addons.iam.enable ? 1 : 0
32+
name = "ecr-publisher"
33+
user = aws_iam_user.publisher.0.name
3134

3235
policy = <<EOF
3336
{
@@ -61,12 +64,13 @@ EOF
6164
}
6265

6366
resource "aws_iam_access_key" "publisher" {
64-
user = aws_iam_user.publisher.name
67+
count = local.addons.iam.enable ? 1 : 0
68+
user = aws_iam_user.publisher.0.name
6569
}
6670

67-
resource "aws_iam_role_policy" "fargate" {
71+
resource "aws_iam_role_policy" "this" {
6872
name = "fargate-execution-role"
69-
role = aws_iam_role.fargate.id
73+
role = aws_iam_role.this.id
7074

7175
policy = <<EOF
7276
{

lb.tf

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
resource "aws_lb" "alb" {
1+
resource "aws_lb" "this" {
2+
count = local.addons.loadbalancer.enable == true ? 1 : 0
3+
24
name = local.lb["name"]
35
internal = local.lb["internal"]
46
load_balancer_type = "application"
5-
subnets = data.aws_subnet.subnets.*.id
7+
subnets = data.aws_subnet.this.*.id
68
}
79

8-
resource "aws_lb_target_group" "group" {
10+
resource "aws_lb_target_group" "this" {
11+
count = local.addons.loadbalancer.enable ? 1 : 0
12+
913
name = local.lb.target_group["name"]
1014
port = local.lb.target_group["port"]
1115
protocol = local.lb.target_group["protocol"]
12-
vpc_id = data.aws_vpc.vpc.id
16+
vpc_id = data.aws_vpc.this.id
1317
target_type = "ip"
1418

15-
depends_on = [aws_lb.alb]
19+
depends_on = [aws_lb.this.0]
1620
}
1721

18-
resource "aws_lb_listener" "front_end" {
19-
load_balancer_arn = aws_lb.alb.arn
22+
resource "aws_lb_listener" "this" {
23+
count = local.addons.loadbalancer.enable ? 1 : 0
24+
25+
load_balancer_arn = aws_lb.this.0.arn
2026
port = "80"
2127
protocol = "HTTP"
2228

2329
default_action {
2430
type = "forward"
25-
target_group_arn = aws_lb_target_group.group.arn
31+
target_group_arn = aws_lb_target_group.this.0.arn
2632
}
2733
}

locals.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ locals {
3434
ports = [80]
3535
}
3636
container = merge(local.container_defaults, var.container)
37+
38+
addons_defaults = {
39+
loadbalancer = {
40+
enable = true
41+
}
42+
ecr = {
43+
enable = true
44+
}
45+
iam = {
46+
enable = true
47+
}
48+
}
49+
addons = {
50+
for key, defaults in local.addons_defaults :
51+
key => merge(defaults, lookup(var.addons, key, {}))
52+
}
3753
}

output.tf

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,47 @@ output "aws_region" {
44
}
55

66
output "app_url" {
7-
value = aws_lb.alb.dns_name
7+
value = local.addons.loadbalancer.enable ? aws_lb.this.0.dns_name : ""
88
description = "The public ALB DNS"
99
}
1010

1111
output "publisher_access_key" {
12-
value = aws_iam_access_key.publisher.id
12+
value = local.addons.iam.enable ? aws_iam_access_key.publisher.0.id : ""
1313
description = "AWS_ACCESS_KEY to publish to ECR"
1414
}
1515

1616
output "publisher_secret_key" {
17-
value = aws_iam_access_key.publisher.secret
17+
value = local.addons.iam.enable ? aws_iam_access_key.publisher.0.secret : ""
1818
description = "AWS_SECRET_ACCESS_KEY to upload to the ECR"
1919
sensitive = true
2020
}
2121

2222
output "ecr_url" {
23-
value = aws_ecr_repository.repository.repository_url
23+
value = local.addons.ecr.enable ? aws_ecr_repository.this.0.repository_url : ""
2424
description = "The ECR repository URL"
2525
}
2626

2727
output "ecr_repository_name" {
28-
value = aws_ecr_repository.repository.name
28+
value = local.addons.ecr.enable ? aws_ecr_repository.this.0.name : ""
2929
description = "The ECR repository name"
3030
}
3131

3232
output "ecs_cluster" {
33-
value = aws_ecs_cluster.cluster.name
33+
value = aws_ecs_cluster.this.name
3434
description = "The ECS cluster name"
3535
}
3636

3737
output "ecs_service" {
38-
value = aws_ecs_service.service.name
38+
value = aws_ecs_service.this.name
3939
description = "The ECS service name"
4040
}
4141

4242
output "container_name" {
4343
value = local.container.name
4444
description = "Container name for the ECS task"
4545
}
46+
47+
output "addons" {
48+
value = local.addons
49+
description = "The Addons configuration"
50+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ variable "container" {
3333
default = {}
3434
description = "Container configuration to deploy"
3535
}
36+
37+
variable "addons" {
38+
type = any
39+
default = {}
40+
description = "Configuration of each addon that can be toggles on and off"
41+
}

vpc.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
data "aws_vpc" "vpc" {
1+
data "aws_vpc" "this" {
22
id = local.use_default_vpc ? null : local.vpc["id"]
33
default = local.use_default_vpc
44
}
55

6-
data "aws_subnet_ids" "subnets" {
7-
vpc_id = data.aws_vpc.vpc.id
6+
data "aws_subnet_ids" "this" {
7+
vpc_id = data.aws_vpc.this.id
88
}
99

10-
data "aws_subnet" "subnets" {
11-
for_each = data.aws_subnet_ids.subnets.ids
12-
vpc_id = data.aws_vpc.vpc.id
10+
data "aws_subnet" "this" {
11+
for_each = data.aws_subnet_ids.this.ids
12+
vpc_id = data.aws_vpc.this.id
1313
id = each.value
1414
# availability_zone = each.value
1515
}

0 commit comments

Comments
 (0)