X-Git-Url: http://git.squeep.com/?p=awsible;a=blobdiff_plain;f=infrastructure%2Fmodules%2Ftf_aws_asg_stack%2Fmain.tf;fp=infrastructure%2Fmodules%2Ftf_aws_asg_stack%2Fmain.tf;h=6a50ea3444eca43e7734d45a8c79808568bc1ca4;hp=0000000000000000000000000000000000000000;hb=8576668075ca95e44481d9c9ed29d7e6af024bdc;hpb=933c48ff1e134168de3aaa2d20e4d43c13d04928 diff --git a/infrastructure/modules/tf_aws_asg_stack/main.tf b/infrastructure/modules/tf_aws_asg_stack/main.tf new file mode 100644 index 0000000..6a50ea3 --- /dev/null +++ b/infrastructure/modules/tf_aws_asg_stack/main.tf @@ -0,0 +1,125 @@ +resource "aws_security_group" "default" { + vpc_id = "${var.vpc_id}" + name = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack}-self" + description = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack} self-access" +} +resource "aws_security_group_rule" "default-out-all" { + security_group_id = "${aws_security_group.default.id}" + type = "egress" + from_port = 0 + to_port = 0 + protocol = "all" + cidr_blocks = [ "0.0.0.0/0" ] +} +resource "aws_security_group_rule" "default-in-self" { + security_group_id = "${aws_security_group.default.id}" + type = "ingress" + from_port = 0 + to_port = 0 + protocol = "all" + self = true +} +resource "aws_security_group_rule" "default-in-elb" { + count = "${length(var.elb_sg_ids)}" + security_group_id = "${aws_security_group.default.id}" + type = "ingress" + from_port = 0 + to_port = 0 + protocol = "all" + source_security_group_id = "${element(var.elb_sg_ids, count.index)}" +} + +data "aws_ami" "amazon_linux" { + count = "${length(var.ami) > 0 ? 0 : 1}" + most_recent = true + owners = ["amazon"] + filter { + name = "name" + values = ["amzn-ami-hvm-*-gp2"] + } + filter { + name = "root-device-type" + values = ["ebs"] + } +} + +data "aws_region" "current" { + current = true +} +data "template_file" "user_data" { + template = "${file("${path.module}/user-data.tpl")}" + vars { + region = "${data.aws_region.current.name}" + app_name = "${var.module}" + stack = "${var.stack}" + phase = "${var.phase}" + country = "${var.country}" + cluster = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack}${length(var.country) > 0 ? "-c0" : ""}${var.country}${length(var.phase) > 0 ? "-d0" : ""}${var.phase}${length(var.suffix) > 0 ? "-" : ""}${var.suffix}" + acct_name = "${var.acct_name}" + } +} + +resource "aws_launch_configuration" "default" { + name_prefix = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack}${length(var.country) > 0 ? "-c0" : ""}${var.country}${length(var.phase) > 0 ? "-d0" : ""}${var.phase}${length(var.suffix) > 0 ? "-" : ""}${var.suffix}-" + image_id = "${length(var.ami) > 0 ? var.ami : data.aws_ami.amazon_linux.image_id}" + instance_type = "${var.instance_type}" + iam_instance_profile = "${aws_iam_instance_profile.default.name}" + key_name = "${var.key_name}" + security_groups = ["${concat(var.security_group_ids, list(aws_security_group.default.id))}"] + associate_public_ip_address = "${var.public_ips}" + user_data = "${data.template_file.user_data.rendered}" + ephemeral_block_device { + virtual_name = "ephemeral0" + device_name = "/dev/sdb" + } + lifecycle { + create_before_destroy = true + } +} + +resource "aws_autoscaling_group" "default" { + name = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack}${length(var.country) > 0 ? "-c0" : ""}${var.country}${length(var.phase) > 0 ? "-d0" : ""}${var.phase}${length(var.suffix) > 0 ? "-" : ""}${var.suffix}" + launch_configuration = "${aws_launch_configuration.default.name}" + vpc_zone_identifier = ["${var.subnet_ids}"] + min_size = "${var.min_size}" + max_size = "${var.max_size > 0 ? var.max_size : length(var.subnet_ids)}" + default_cooldown = 10 + health_check_type = "EC2" + health_check_grace_period = "${var.health_check_grace_period}" + load_balancers = ["${var.elbs}"] + lifecycle { + create_before_destroy = true + } + tag { + propagate_at_launch = true + key = "module" + value = "${var.module}" + } + tag { + propagate_at_launch = true + key = "stack" + value = "${var.stack}" + } + tag { + propagate_at_launch = true + key = "country" + value = "${var.country}" + } + tag { + propagate_at_launch = true + key = "phase" + value = "${var.phase}" + } +} + +resource "aws_autoscaling_notification" "default" { + count = "${length(var.notification_arns)}" + group_names = ["${aws_autoscaling_group.default.name}"] + topic_arn = "${element(var.notification_arn, count.index)}" + notifications = [ + "autoscaling:EC2_INSTANCE_LAUNCH", + "autoscaling:EC2_INSTANCE_LAUNCH_ERROR", + "autoscaling:EC2_INSTANCE_TERMINATE", + "autoscaling:EC2_INSTANCE_TERMINATE_ERROR" + ] +}