Skip to content

Commit 468d56f

Browse files
author
Abdul Wahid
authored
Add support for registering multiple target groups with a service (#29)
* Add support for registering multiple target groups with a service Read more: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/register-multiple-targetgroups.html * Lint fixes * Missing comment * Update README * Use 'for_each' instead of 'count'
1 parent 1ba1e3c commit 468d56f

File tree

9 files changed

+280
-95
lines changed

9 files changed

+280
-95
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ errored.tfstate
44
crash.log
55
terraform.tfstate
66
*.tfstate*
7-
terraform.tfvars
7+
terraform.tfvars
8+
*.terraform.lock.hcl

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/umotif-public/terraform-aws-ecs-fargate?style=social)
1+
<!-- markdownlint-disable MD041 -->
2+
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/umotif-public/terraform-aws-ecs-fargate?style=social)](https://github.com/umotif-public/terraform-aws-ecs-fargate/releases/latest)
23

3-
# terraform-aws-ecs-fargate
4+
# Terraform AWS ECS Fargate
45

5-
Terraform module to create AWS ECS FARGATE services. Module support both FARGATE and FARGATE-SPOT capacity provider settings.
6+
Terraform module to create [AWS ECS FARGATE](https://aws.amazon.com/fargate/) services. Module supports both `FARGATE` and `FARGATE-SPOT` capacity provider settings.
67

78
## Terraform versions
89

9-
Terraform 0.13. Pin module version to `~> v5.0`. Submit pull-requests to `master` branch.
10+
Terraform 0.13. Pin module version to `~> v6.0`. Submit pull-requests to `master` branch.
1011

1112
## Usage
1213

@@ -30,12 +31,11 @@ resource "aws_ecs_cluster" "cluster" {
3031
3132
module "ecs-fargate" {
3233
source = "umotif-public/ecs-fargate/aws"
33-
version = "~> 5.1.0"
34+
version = "~> 6.0.0"
3435
3536
name_prefix = "ecs-fargate-example"
3637
vpc_id = "vpc-abasdasd132"
3738
private_subnet_ids = ["subnet-abasdasd132123", "subnet-abasdasd132123132"]
38-
lb_arn = "arn:aws:asdasdasdasdasdasad"
3939
4040
cluster_id = aws_ecs_cluster.cluster.id
4141
@@ -46,6 +46,13 @@ module "ecs-fargate" {
4646
task_container_port = 80
4747
task_container_assign_public_ip = true
4848
49+
target_groups = [
50+
{
51+
target_group_name = "tg-fargate-example"
52+
container_port = 80
53+
}
54+
]
55+
4956
health_check = {
5057
port = "traffic-port"
5158
path = "/"
@@ -67,6 +74,7 @@ Module is to be used with Terraform > 0.13.
6774
* [ECS Fargate](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/core)
6875
* [ECS Fargate Spot](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/fargate-spot)
6976
* [ECS Fargate with EFS](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/fargate-efs)
77+
* [ECS Service with multiple target groups](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/multiple-target-groups)
7078

7179
## Authors
7280

@@ -94,14 +102,13 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
94102
| cluster\_id | The Amazon Resource Name (ARN) that identifies the cluster. | `string` | n/a | yes |
95103
| container\_name | Optional name for the container to be used instead of name\_prefix. | `string` | `""` | no |
96104
| create\_repository\_credentials\_iam\_policy | Set to true if you are specifying `repository_credentials` variable, it will attach IAM policy with necessary permissions to task role. | `bool` | `false` | no |
97-
| deployment\_controller\_type | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS. | `string` | `"ECS"` | no |
105+
| deployment\_controller\_type | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS, EXTERNAL. Default: ECS. | `string` | `"ECS"` | no |
98106
| deployment\_maximum\_percent | The upper limit of the number of running tasks that can be running in a service during a deployment | `number` | `200` | no |
99107
| deployment\_minimum\_healthy\_percent | The lower limit of the number of running tasks that must remain running and healthy in a service during a deployment | `number` | `50` | no |
100108
| desired\_count | The number of instances of the task definitions to place and keep running. | `number` | `1` | no |
101109
| force\_new\_deployment | Enable to force a new task deployment of the service. This can be used to update tasks to use a newer Docker image with same image/tag combination (e.g. myimage:latest), roll Fargate tasks onto a newer platform version. | `bool` | `false` | no |
102110
| health\_check | A health block containing health check settings for the target group. Overrides the defaults. | `map(string)` | n/a | yes |
103111
| health\_check\_grace\_period\_seconds | Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers. | `number` | `300` | no |
104-
| lb\_arn | Arn for the LB for which the service should be attach to. | `string` | n/a | yes |
105112
| load\_balanced | Whether the task should be loadbalanced. | `bool` | `true` | no |
106113
| log\_retention\_in\_days | Number of days the logs will be retained in CloudWatch. | `number` | `30` | no |
107114
| logs\_kms\_key | The KMS key ARN to use to encrypt container logs. | `string` | `""` | no |
@@ -116,7 +123,7 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
116123
| service\_registry\_arn | ARN of aws\_service\_discovery\_service resource | `string` | `""` | no |
117124
| sg\_name\_prefix | A prefix used for Security group name. | `string` | `""` | no |
118125
| tags | A map of tags (key-value pairs) passed to resources. | `map(string)` | `{}` | no |
119-
| target\_group\_name | The name for the tasks target group | `string` | `""` | no |
126+
| target\_groups | The name of the target groups to associate with ecs service | `any` | `[]` | no |
120127
| task\_container\_assign\_public\_ip | Assigned public IP to the container. | `bool` | `false` | no |
121128
| task\_container\_command | The command that is passed to the container. | `list(string)` | `[]` | no |
122129
| task\_container\_cpu | Amount of CPU to reserve for the container. | `number` | `null` | no |

examples/core/main.tf

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,26 @@ provider "aws" {
55
#####
66
# VPC and subnets
77
#####
8-
module "vpc" {
9-
source = "terraform-aws-modules/vpc/aws"
10-
version = "~> 2.63"
11-
12-
name = "simple-vpc"
13-
14-
cidr = "10.0.0.0/16"
15-
16-
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
17-
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
18-
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
8+
data "aws_vpc" "default" {
9+
default = true
10+
}
1911

20-
enable_nat_gateway = false
12+
data "aws_subnet_ids" "all" {
13+
vpc_id = data.aws_vpc.default.id
2114
}
2215

2316
#####
2417
# ALB
2518
#####
2619
module "alb" {
2720
source = "umotif-public/alb/aws"
28-
version = "~> 1.0"
21+
version = "~> 2.0"
2922

3023
name_prefix = "alb-example"
3124
load_balancer_type = "application"
3225
internal = false
33-
vpc_id = module.vpc.vpc_id
34-
subnets = module.vpc.public_subnets
26+
vpc_id = data.aws_vpc.default.id
27+
subnets = data.aws_subnet_ids.all.ids
3528
}
3629

3730
resource "aws_lb_listener" "alb_80" {
@@ -41,7 +34,7 @@ resource "aws_lb_listener" "alb_80" {
4134

4235
default_action {
4336
type = "forward"
44-
target_group_arn = module.fargate.target_group_arn
37+
target_group_arn = module.fargate.target_group_arn[0]
4538
}
4639
}
4740

@@ -93,9 +86,8 @@ module "fargate" {
9386
name_prefix = "ecs-fargate-example"
9487
# sg_name_prefix = "my-security-group-name" # uncomment if you want to name security group with specific name
9588

96-
vpc_id = module.vpc.vpc_id
97-
private_subnet_ids = module.vpc.public_subnets
98-
lb_arn = module.alb.arn
89+
vpc_id = data.aws_vpc.default.id
90+
private_subnet_ids = data.aws_subnet_ids.all.ids
9991
cluster_id = aws_ecs_cluster.cluster.id
10092

10193
wait_for_steady_state = true
@@ -109,6 +101,12 @@ module "fargate" {
109101
task_container_port = 80
110102
task_container_assign_public_ip = true
111103

104+
target_groups = [
105+
{
106+
container_port = 80
107+
}
108+
]
109+
112110
health_check = {
113111
port = "traffic-port"
114112
path = "/"
@@ -129,7 +127,7 @@ module "fargate" {
129127
resource "aws_security_group" "allow_sg_test" {
130128
name = "allow_sg_test"
131129
description = "Allow sg inbound traffic"
132-
vpc_id = module.vpc.vpc_id
130+
vpc_id = data.aws_vpc.default.id
133131
}
134132

135133
resource "aws_security_group_rule" "test_sg_ingress" {

examples/fargate-efs/main.tf

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,26 @@ provider "aws" {
55
#####
66
# VPC and subnets
77
#####
8-
module "vpc" {
9-
source = "terraform-aws-modules/vpc/aws"
10-
version = "~> 2.63"
11-
12-
name = "simple-vpc"
13-
14-
cidr = "10.0.0.0/16"
15-
16-
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
17-
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
18-
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
8+
data "aws_vpc" "default" {
9+
default = true
10+
}
1911

20-
enable_nat_gateway = false
12+
data "aws_subnet_ids" "all" {
13+
vpc_id = data.aws_vpc.default.id
2114
}
2215

2316
#####
2417
# ALB
2518
#####
2619
module "alb" {
2720
source = "umotif-public/alb/aws"
28-
version = "~> 1.0"
21+
version = "~> 2.0"
2922

3023
name_prefix = "alb-example"
3124
load_balancer_type = "application"
3225
internal = false
33-
vpc_id = module.vpc.vpc_id
34-
subnets = module.vpc.public_subnets
26+
vpc_id = data.aws_vpc.default.id
27+
subnets = data.aws_subnet_ids.all.ids
3528
}
3629

3730
resource "aws_lb_listener" "alb_80" {
@@ -41,7 +34,7 @@ resource "aws_lb_listener" "alb_80" {
4134

4235
default_action {
4336
type = "forward"
44-
target_group_arn = module.fargate.target_group_arn
37+
target_group_arn = module.fargate.target_group_arn[0]
4538
}
4639
}
4740

@@ -99,9 +92,8 @@ module "fargate" {
9992
source = "../../"
10093

10194
name_prefix = "ecs-fargate-example"
102-
vpc_id = module.vpc.vpc_id
103-
private_subnet_ids = module.vpc.public_subnets
104-
lb_arn = module.alb.arn
95+
vpc_id = data.aws_vpc.default.id
96+
private_subnet_ids = data.aws_subnet_ids.all.ids
10597
cluster_id = aws_ecs_cluster.cluster.id
10698

10799
platform_version = "1.4.0"
@@ -113,6 +105,12 @@ module "fargate" {
113105
task_container_port = 80
114106
task_container_assign_public_ip = true
115107

108+
target_groups = [
109+
{
110+
container_port = 80
111+
}
112+
]
113+
116114
health_check = {
117115
port = "traffic-port"
118116
path = "/"

examples/fargate-spot/main.tf

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,26 @@ provider "aws" {
55
#####
66
# VPC and subnets
77
#####
8-
module "vpc" {
9-
source = "terraform-aws-modules/vpc/aws"
10-
version = "~> 2.63"
11-
12-
name = "simple-vpc"
13-
14-
cidr = "10.0.0.0/16"
15-
16-
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
17-
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
18-
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
8+
data "aws_vpc" "default" {
9+
default = true
10+
}
1911

20-
enable_nat_gateway = false
12+
data "aws_subnet_ids" "all" {
13+
vpc_id = data.aws_vpc.default.id
2114
}
2215

2316
#####
2417
# ALB
2518
#####
2619
module "alb" {
2720
source = "umotif-public/alb/aws"
28-
version = "~> 1.0"
21+
version = "~> 2.0"
2922

3023
name_prefix = "alb-example"
3124
load_balancer_type = "application"
3225
internal = false
33-
vpc_id = module.vpc.vpc_id
34-
subnets = module.vpc.public_subnets
26+
vpc_id = data.aws_vpc.default.id
27+
subnets = data.aws_subnet_ids.all.ids
3528
}
3629

3730
resource "aws_lb_listener" "alb_80" {
@@ -41,7 +34,7 @@ resource "aws_lb_listener" "alb_80" {
4134

4235
default_action {
4336
type = "forward"
44-
target_group_arn = module.fargate.target_group_arn
37+
target_group_arn = module.fargate.target_group_arn[0]
4538
}
4639
}
4740

@@ -88,10 +81,10 @@ module "fargate" {
8881
source = "../../"
8982

9083
name_prefix = "ecs-fargate-example"
91-
vpc_id = module.vpc.vpc_id
92-
private_subnet_ids = module.vpc.public_subnets
93-
lb_arn = module.alb.arn
94-
cluster_id = aws_ecs_cluster.cluster.id
84+
vpc_id = data.aws_vpc.default.id
85+
private_subnet_ids = data.aws_subnet_ids.all.ids
86+
87+
cluster_id = aws_ecs_cluster.cluster.id
9588

9689
task_container_image = "marcincuber/2048-game:latest"
9790
task_definition_cpu = 256
@@ -100,6 +93,12 @@ module "fargate" {
10093
task_container_port = 80
10194
task_container_assign_public_ip = true
10295

96+
target_groups = [
97+
{
98+
container_port = 80
99+
}
100+
]
101+
103102
health_check = {
104103
port = "traffic-port"
105104
path = "/"

0 commit comments

Comments
 (0)