Skip to content

Instantly share code, notes, and snippets.

@hgfranco
Created November 5, 2018 17:13
Show Gist options
  • Select an option

  • Save hgfranco/9ec382feadc40ef5ae07ee19091c0bb9 to your computer and use it in GitHub Desktop.

Select an option

Save hgfranco/9ec382feadc40ef5ae07ee19091c0bb9 to your computer and use it in GitHub Desktop.

Revisions

  1. hgfranco created this gist Nov 5, 2018.
    72 changes: 72 additions & 0 deletions Terraform - create ALB
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    # Create the ALB
    resource "aws_alb" "nexus_alb" {
    name = "nexus-alb"
    internal = true
    load_balancer_type = "application"
    security_groups = ["${aws_security_group.alb.id}"]
    subnets = ["${data.aws_subnet_ids.nexus_vpc_subnets.ids}"]

    access_logs {
    bucket = "${aws_s3_bucket.nexus-s3-bucket-logs.bucket}"
    prefix = "nexus-lb"
    enabled = true
    }
    }

    // Provides a target group to use with our ALB
    resource "aws_alb_target_group" "nexus_alb_target_group" {
    name = "${var.target_group_name}"
    port = "${var.target_group_port}"
    protocol = "HTTP"
    vpc_id = "${var.vpc_id}"
    tags {
    name = "${var.target_group_name}"
    }

    health_check {
    healthy_threshold = 3
    unhealthy_threshold = 10
    timeout = 5
    interval = 10
    path = "${var.target_group_path}"
    port = "${var.target_group_port}"
    }
    }


    // Listens for traffic on a specific port
    resource "aws_alb_listener" "nexus_alb_listener" {
    load_balancer_arn = "${aws_alb.nexus_alb.arn}"
    port = "${var.alb_listener_port}"
    protocol = "${var.alb_listener_protocol}"

    default_action {
    target_group_arn = "${aws_alb_target_group.nexus_alb_target_group.arn}"
    type = "forward"
    }
    }


    // Provides the ALB listen rules
    resource "aws_alb_listener_rule" "listener_rule" {
    depends_on = ["aws_alb_target_group.nexus_alb_target_group"]
    listener_arn = "${aws_alb_listener.nexus_alb_listener.arn}"
    priority = "${var.listener_rule_priority}"
    action {
    type = "forward"
    target_group_arn = "${aws_alb_target_group.nexus_alb_target_group.id}"
    }
    condition {
    field = "path-pattern"
    values = ["${var.nexus_alb_path}"]
    }
    }

    // Create a CNAME for the ALB
    resource "aws_route53_record" "www" {
    zone_id = "${var.route_53_zone_id}"
    name = "nexus-repo.${var.route_53_zone}"
    type = "CNAME"
    ttl = "300"
    records = ["${aws_alb.nexus_alb.dns_name}"]
    }