Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find LB listener by the LB ARN #2110

Closed

Conversation

tomelliff
Copy link
Contributor

Right now the aws_lb_listener data source isn't particularly useful unless you somehow know the ARN of the listener.
Being able to search by the load balancer ARN means you can chain the aws_lb data source to find the LB listener from an LB name.

As an example we can now do something like this:

variable "lb_name" {
  default = "foo"
}

variable "vpc" {
  default = "my_vpc"
}

variable "listener_rule_priority" {
  default = 99
}

data "aws_vpc" "vpc" {
  tags {
    Name = "${var.vpc}"
  }
}

data "aws_lb" "load_balancer" {
  name = "${var.lb_name}"
}

data "aws_lb_listener" "listener" {
  load_balancer_arn = "${data.aws_lb.load_balancer.arn}"
}

resource "aws_lb_listener_rule" "host_based_routing" {
  listener_arn = "${data.aws_lb_listener.listener.arn}"
  priority     = "${var.listener_rule_priority}"

  action {
    type             = "forward"
    target_group_arn = "${aws_lb_target_group.service.arn}"
  }

  condition {
    field  = "host-header"
    values = ["my-service.*.terraform.io"]
  }
}

resource "aws_lb_target_group" "service" {
  name     = "dynamic"
  port     = 80
  protocol = "HTTP"
  vpc_id   = "${data.aws_vpc.vpc.id}"
}

In particular this will allow me to go from creating an ALB with every single ECS service I deploy to a single one per ECS cluster (separated by environments) because now I can easily create a listener rule to forward to the service's target group.

In reality I don't really want to have to provide the priority statically with each service so I intend to add some more functionality to the AWS provider at a later date that fetches the highest priority rule for a load balancer listener and then just increment from there. There is an issue for this already at #1574

Note that if a load balancer has multiple listeners this will error when searching by load_balancer_arn.

Tom Elliff added 2 commits October 31, 2017 15:46
Right now the aws_lb_listener data source isn't particularly useful unless you somehow know the ARN of the listener.
Being able to search by the load balancer ARN means you can chain the aws_lb data source to find the LB listener from an LB name.

Note that if a load balancer has multiple listeners this will error when searching by load_balancer_arn.
@tomelliff
Copy link
Contributor Author

tomelliff commented Oct 31, 2017

For now I've left it so that if you search for a listener by the load balancer's ARN and the load balancer has multiple listeners then the data source should error.

I was thinking about expanding it so you could filter by port but I don't have a use case for that right now and am happy with it working just for a single listener (in my case 443/HTTPS). If it's thought that this would be useful to have before this is merged then I can take a look at it but I'd say this is worth merging as is for now and the other work can be done later if there is a need for it.

I was also a little unsure with the pretty much duplicated config for the test. I was hoping I could split out the setup of resources in one config and then apply a second config with just the data source with either lookup by listener arn or load balancer arn but I don't know if that's possible in the acceptance tests at all? When I've looked at other tests around data sources some of them double apply so that the resources are created first and then the data source reads them in the next step but they still include all the config in the first apply.

@radeksimko radeksimko added enhancement Requests to existing resources that expand the functionality or scope. upstream-terraform Addresses functionality related to the Terraform core binary. labels Oct 31, 2017
@radeksimko radeksimko added the size/L Managed by automation to categorize the size of a PR. label Nov 15, 2017
@tomelliff
Copy link
Contributor Author

I was hoping to pick up the second part of this to close #1574 some time over Christmas if I get some free time but would like to know if there's any useful feedback on this part first and ideally get it merged? Is there anything more needed here?

It would cut a fair chunk out of our AWS bill by allowing us to massively reduce the amount of load balancers we have due to having lots of services and then lots of dynamic review environments plus reduce the time to create them (vast majority of the creation time is in the load balancer creation time) which would help a bit.

@bflad bflad added the service/elbv2 Issues and PRs that pertain to the elbv2 service. label Jan 28, 2018
@bflad
Copy link
Contributor

bflad commented Feb 1, 2018

Hi @tomelliff, looks like this functionality (with port filtering as well) was just merged into master in #2886 and will be released in v1.9.0 of the AWS provider. I'm very sorry that your PR here got lost in the shuffle to get a proper review when it should have sooner, along with getting you proper credit. 😢 We just completed labelling the backlog of issues and PRs to hopefully prevent this from happening in the future.

@bflad bflad closed this Feb 1, 2018
@tomelliff
Copy link
Contributor Author

@bflad No worries, just glad the functionality is in. Combined with #3219 (or adding the max listener to the ALB data source) means I can reduce a lot of ALBs in my setup and make it much quicker to provide dynamic environments.

@ghost
Copy link

ghost commented Apr 8, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/elbv2 Issues and PRs that pertain to the elbv2 service. size/L Managed by automation to categorize the size of a PR. upstream-terraform Addresses functionality related to the Terraform core binary.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants