Skip to content

Commit

Permalink
fix: target group lifecycle policy (#115)
Browse files Browse the repository at this point in the history
* fix: target group lifecycle policy

* enhanced example for testing target group

---------

Co-authored-by: Moritz Zimmer <[email protected]>
  • Loading branch information
major0 and moritzzimmer authored Jun 22, 2023
1 parent a950b20 commit 8446492
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 4 additions & 0 deletions alb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ resource "aws_alb_target_group" "main" {
matcher = lookup(health_check.value, "matcher", null)
}
}

lifecycle {
create_before_destroy = true
}
}

resource "aws_alb_listener_rule" "public" {
Expand Down
4 changes: 2 additions & 2 deletions examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Note that this example may create resources which cost money. Run `terraform des
|------|--------|---------|
| <a name="module_alb_security_group_public"></a> [alb\_security\_group\_public](#module\_alb\_security\_group\_public) | registry.terraform.io/terraform-aws-modules/security-group/aws | >= 4.17 |
| <a name="module_service"></a> [service](#module\_service) | ../../ | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | registry.terraform.io/terraform-aws-modules/vpc/aws | >= 3.18 |
| <a name="module_vpc_endpoints"></a> [vpc\_endpoints](#module\_vpc\_endpoints) | registry.terraform.io/terraform-aws-modules/vpc/aws//modules/vpc-endpoints | >= 3.18 |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | registry.terraform.io/terraform-aws-modules/vpc/aws | >= 4.0 |
| <a name="module_vpc_endpoints"></a> [vpc\_endpoints](#module\_vpc\_endpoints) | registry.terraform.io/terraform-aws-modules/vpc/aws//modules/vpc-endpoints | >= 4.0 |

## Resources

Expand Down
13 changes: 6 additions & 7 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
locals {
container_port = 8000
image_tag = "production"
service_name = "${random_pet.this.id}-service"
}

resource "random_pet" "this" {
length = 2
length = 1
}

resource "aws_ecs_cluster" "this" {
name = "${random_pet.this.id}-cluster"
name = random_pet.this.id
}

module "vpc" {
source = "registry.terraform.io/terraform-aws-modules/vpc/aws"
version = ">= 3.18"
version = ">= 4.0"

azs = slice(data.aws_availability_zones.available.names, 0, 3)
cidr = "10.0.0.0/16"
Expand All @@ -35,7 +34,7 @@ module "vpc" {
// see https://docs.aws.amazon.com/AmazonECR/latest/userguide/vpc-endpoints.html for necessary endpoints to run Fargate tasks
module "vpc_endpoints" {
source = "registry.terraform.io/terraform-aws-modules/vpc/aws//modules/vpc-endpoints"
version = ">= 3.18"
version = ">= 4.0"

security_group_ids = [data.aws_security_group.default.id]
vpc_id = module.vpc.vpc_id
Expand Down Expand Up @@ -118,7 +117,7 @@ module "service" {
desired_count = 1
ecr_force_delete = true
memory = 512
service_name = local.service_name
service_name = random_pet.this.id
vpc_id = module.vpc.vpc_id
ecr_image_tag = local.image_tag

Expand Down Expand Up @@ -152,7 +151,7 @@ module "service" {
// add a target group to route ALB traffic to this service
target_groups = [
{
name = "${local.service_name}-public"
name_prefix = "${substr(random_pet.this.id, 0, 5)}-"
backend_protocol = "HTTP"
backend_port = local.container_port
load_balancer_arn = aws_lb_listener.http.load_balancer_arn
Expand Down

0 comments on commit 8446492

Please sign in to comment.