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" ] }