Skip to content

Instantly share code, notes, and snippets.

@ezalejski
Created September 21, 2015 12:20
Show Gist options
  • Save ezalejski/cdea8c146ed23e9dc55d to your computer and use it in GitHub Desktop.
Save ezalejski/cdea8c146ed23e9dc55d to your computer and use it in GitHub Desktop.
resource "aws_instance" "lb" {
disable_api_termination = false
count = "${var.lb_instances_count}"
ami = "${var.lb_ami_id}"
instance_type = "${var.lb_ami_type}"
tags {
Name = "${format("lb%d-%s", count.index+1, var.environment)}"
}
vpc_security_group_ids = [ "${aws_security_group.main.id}", "${aws_security_group.lb.id}"]
subnet_id = "${aws_subnet.a.id}"
associate_public_ip_address = true
user_data = "${element(template_file.user_data_ubuntu_lb.*.rendered, count.index)}"
}
resource "aws_eip" "lb" {
count = "${var.lb_instances_count}"
instance = "${element(aws_instance.lb.*.id, count.index)}"
vpc = true
}
resource "cloudflare_record" "lb" {
domain = "${var.cloudflare_domain}"
count = "${var.lb_instances_count}"
name = "${format("lb%d-%s", count.index+1, var.environment)}"
value = "${element(aws_eip.lb.*.public_ip, count.index)}"
type = "A"
ttl = "120"
}
resource "aws_route53_record" "lb" {
zone_id = "${aws_route53_zone.internal.zone_id}"
name = "${format("lb%d-%s", count.index+1, var.environment)}"
type = "A"
count = "${var.lb_instances_count}"
ttl = 60
records = ["${element(aws_instance.lb.*.private_ip, count.index)}"]
}
resource "aws_security_group" "lb" {
name = "lb-${var.environment}"
description = "lb-${var.environment} security group"
vpc_id = "${aws_vpc.main.id}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["${var.office_ip}", "${var.eryk_ip}"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = ["${aws_security_group.elb-main.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "template_file" "user_data_ubuntu_lb" {
filename = "../cloud-config-ubuntu.yaml"
count = "${var.lb_instances_count}"
vars {
hostname = "${format("lb%d-%s", count.index+1, var.environment)}"
domain = "${var.internal_domain}"
environment = "${var.environment}"
role = "lb"
branch = "${var.chef_branch}"
}
}
output "lb_public_ips" {
value = "${join(",", aws_instance.lb.*.public_ip)}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment