initial commit of replacement infrastructure automation
[awsible] / infrastructure / modules / tf_aws_asg_stack / main.tf
1 resource "aws_security_group" "default" {
2 vpc_id = "${var.vpc_id}"
3 name = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack}-self"
4 description = "${var.module}${length(var.stack) > 0 ? "-" : ""}${var.stack} self-access"
5 }
6 resource "aws_security_group_rule" "default-out-all" {
7 security_group_id = "${aws_security_group.default.id}"
8 type = "egress"
9 from_port = 0
10 to_port = 0
11 protocol = "all"
12 cidr_blocks = [ "0.0.0.0/0" ]
13 }
14 resource "aws_security_group_rule" "default-in-self" {
15 security_group_id = "${aws_security_group.default.id}"
16 type = "ingress"
17 from_port = 0
18 to_port = 0
19 protocol = "all"
20 self = true
21 }
22 resource "aws_security_group_rule" "default-in-elb" {
23 count = "${length(var.elb_sg_ids)}"
24 security_group_id = "${aws_security_group.default.id}"
25 type = "ingress"
26 from_port = 0
27 to_port = 0
28 protocol = "all"
29 source_security_group_id = "${element(var.elb_sg_ids, count.index)}"
30 }
31
32 data "aws_ami" "amazon_linux" {
33 count = "${length(var.ami) > 0 ? 0 : 1}"
34 most_recent = true
35 owners = ["amazon"]
36 filter {
37 name = "name"
38 values = ["amzn-ami-hvm-*-gp2"]
39 }
40 filter {
41 name = "root-device-type"
42 values = ["ebs"]
43 }
44 }
45
46 data "aws_region" "current" {
47 current = true
48 }
49 data "template_file" "user_data" {
50 template = "${file("${path.module}/user-data.tpl")}"
51 vars {
52 region = "${data.aws_region.current.name}"
53 app_name = "${var.module}"
54 stack = "${var.stack}"
55 phase = "${var.phase}"
56 country = "${var.country}"
57 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}"
58 acct_name = "${var.acct_name}"
59 }
60 }
61
62 resource "aws_launch_configuration" "default" {
63 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}-"
64 image_id = "${length(var.ami) > 0 ? var.ami : data.aws_ami.amazon_linux.image_id}"
65 instance_type = "${var.instance_type}"
66 iam_instance_profile = "${aws_iam_instance_profile.default.name}"
67 key_name = "${var.key_name}"
68 security_groups = ["${concat(var.security_group_ids, list(aws_security_group.default.id))}"]
69 associate_public_ip_address = "${var.public_ips}"
70 user_data = "${data.template_file.user_data.rendered}"
71 ephemeral_block_device {
72 virtual_name = "ephemeral0"
73 device_name = "/dev/sdb"
74 }
75 lifecycle {
76 create_before_destroy = true
77 }
78 }
79
80 resource "aws_autoscaling_group" "default" {
81 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}"
82 launch_configuration = "${aws_launch_configuration.default.name}"
83 vpc_zone_identifier = ["${var.subnet_ids}"]
84 min_size = "${var.min_size}"
85 max_size = "${var.max_size > 0 ? var.max_size : length(var.subnet_ids)}"
86 default_cooldown = 10
87 health_check_type = "EC2"
88 health_check_grace_period = "${var.health_check_grace_period}"
89 load_balancers = ["${var.elbs}"]
90 lifecycle {
91 create_before_destroy = true
92 }
93 tag {
94 propagate_at_launch = true
95 key = "module"
96 value = "${var.module}"
97 }
98 tag {
99 propagate_at_launch = true
100 key = "stack"
101 value = "${var.stack}"
102 }
103 tag {
104 propagate_at_launch = true
105 key = "country"
106 value = "${var.country}"
107 }
108 tag {
109 propagate_at_launch = true
110 key = "phase"
111 value = "${var.phase}"
112 }
113 }
114
115 resource "aws_autoscaling_notification" "default" {
116 count = "${length(var.notification_arns)}"
117 group_names = ["${aws_autoscaling_group.default.name}"]
118 topic_arn = "${element(var.notification_arn, count.index)}"
119 notifications = [
120 "autoscaling:EC2_INSTANCE_LAUNCH",
121 "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
122 "autoscaling:EC2_INSTANCE_TERMINATE",
123 "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
124 ]
125 }