ignore amazon-linux ami release-candidate versions
[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 # hack filter to ignore .rc- release candidate amis
45 name_regex = "amzn-ami-hvm-[^r]*-gp2"
46 }
47
48 data "aws_region" "current" {
49 current = true
50 }
51 data "template_file" "user_data" {
52 template = "${file("${path.module}/user-data.tpl")}"
53 vars {
54 region = "${data.aws_region.current.name}"
55 app_name = "${var.module}"
56 stack = "${var.stack}"
57 phase = "${var.phase}"
58 country = "${var.country}"
59 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}"
60 acct_name = "${var.acct_name}"
61 }
62 }
63
64 resource "aws_launch_configuration" "default" {
65 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}-"
66 image_id = "${length(var.ami) > 0 ? var.ami : data.aws_ami.amazon_linux.image_id}"
67 instance_type = "${var.instance_type}"
68 iam_instance_profile = "${aws_iam_instance_profile.default.name}"
69 key_name = "${var.key_name}"
70 security_groups = ["${concat(var.security_group_ids, list(aws_security_group.default.id))}"]
71 associate_public_ip_address = "${var.public_ips}"
72 user_data = "${data.template_file.user_data.rendered}"
73 ephemeral_block_device {
74 virtual_name = "ephemeral0"
75 device_name = "/dev/sdb"
76 }
77 lifecycle {
78 create_before_destroy = true
79 }
80 }
81
82 resource "aws_autoscaling_group" "default" {
83 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}"
84 launch_configuration = "${aws_launch_configuration.default.name}"
85 vpc_zone_identifier = ["${var.subnet_ids}"]
86 min_size = "${var.min_size}"
87 max_size = "${var.max_size > 0 ? var.max_size : length(var.subnet_ids)}"
88 default_cooldown = 10
89 health_check_type = "EC2"
90 health_check_grace_period = "${var.health_check_grace_period}"
91 load_balancers = ["${var.elbs}"]
92 lifecycle {
93 create_before_destroy = true
94 }
95 tag {
96 propagate_at_launch = true
97 key = "module"
98 value = "${var.module}"
99 }
100 tag {
101 propagate_at_launch = true
102 key = "stack"
103 value = "${var.stack}"
104 }
105 tag {
106 propagate_at_launch = true
107 key = "country"
108 value = "${var.country}"
109 }
110 tag {
111 propagate_at_launch = true
112 key = "phase"
113 value = "${var.phase}"
114 }
115 }
116
117 resource "aws_autoscaling_notification" "default" {
118 count = "${length(var.notification_arns)}"
119 group_names = ["${aws_autoscaling_group.default.name}"]
120 topic_arn = "${element(var.notification_arn, count.index)}"
121 notifications = [
122 "autoscaling:EC2_INSTANCE_LAUNCH",
123 "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
124 "autoscaling:EC2_INSTANCE_TERMINATE",
125 "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"
126 ]
127 }