Skip to content

Commit 61c1fa3

Browse files
s2504sconst-bon
authored andcommitted
Inherit aws_instance inputs and outputs (#15)
1 parent 500302f commit 61c1fa3

File tree

9 files changed

+426
-192
lines changed

9 files changed

+426
-192
lines changed

README.md

Lines changed: 86 additions & 51 deletions
Large diffs are not rendered by default.

cloud_watch_alarm.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Restart dead or hung instance
2+
3+
resource "null_resource" "check_alarm_action" {
4+
count = "${local.instance_count}"
5+
6+
triggers = {
7+
action = "arn:aws:swf:${local.region}:${data.aws_caller_identity.default.account_id}:${var.default_alarm_action}"
8+
}
9+
}
10+
11+
resource "aws_cloudwatch_metric_alarm" "default" {
12+
count = "${local.instance_count}"
13+
alarm_name = "${module.label.id}"
14+
comparison_operator = "${var.comparison_operator}"
15+
evaluation_periods = "${var.evaluation_periods}"
16+
metric_name = "${var.metric_name}"
17+
namespace = "${var.metric_namespace}"
18+
period = "${var.applying_period}"
19+
statistic = "${var.statistic_level}"
20+
threshold = "${var.metric_threshold}"
21+
depends_on = ["null_resource.check_alarm_action"]
22+
23+
dimensions {
24+
InstanceId = "${aws_instance.default.id}"
25+
}
26+
27+
alarm_actions = [
28+
"${null_resource.check_alarm_action.triggers.action}",
29+
]
30+
}

eni.tf

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
locals {
2-
additional_ips_count = "${var.associate_public_ip_address && var.instance_enabled && var.additional_ips_count != "0" ? var.additional_ips_count : 0}"
2+
additional_ips_count = "${var.associate_public_ip_address && var.instance_enabled && var.additional_ips_count > 0 ? var.additional_ips_count : 0}"
33
}
44

55
resource "aws_network_interface" "additional" {
@@ -10,11 +10,7 @@ resource "aws_network_interface" "additional" {
1010
"${compact(concat(list(var.create_default_security_group ? join("", aws_security_group.default.*.id) : ""), var.security_groups))}",
1111
]
1212

13-
tags {
14-
Name = "${module.label.id}"
15-
Namespace = "${var.namespace}"
16-
Stage = "${var.stage}"
17-
}
13+
tags = "${module.label.tags}"
1814
}
1915

2016
resource "aws_network_interface_attachment" "additional" {

main.tf

Lines changed: 88 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
locals {
2+
instance_count = "${var.instance_enabled ? 1 : 0}"
3+
security_group_count = "${var.create_default_security_group ? 1 : 0}"
4+
region = "${var.region != "" ? var.region : data.aws_region.default.name}"
5+
root_iops = "${var.root_volume_type == "io1" ? var.root_iops : "0"}"
6+
ebs_iops = "${var.ebs_volume_type == "io1" ? var.ebs_iops : "0"}"
7+
availability_zone = "${var.availability_zone != "" ? var.availability_zone : data.aws_subnet.default.availability_zone}"
8+
ami = "${var.ami != "" ? var.ami : data.aws_ami.default.image_id}"
9+
root_volume_type = "${var.root_volume_type != "" ? var.root_volume_type : data.aws_ami.info.root_device_type}"
10+
}
11+
12+
data "aws_caller_identity" "default" {}
13+
14+
data "aws_region" "default" {
15+
current = "true"
16+
}
17+
18+
data "aws_subnet" "default" {
19+
id = "${var.subnet}"
20+
}
21+
122
data "aws_iam_policy_document" "default" {
223
statement {
324
sid = ""
@@ -15,6 +36,29 @@ data "aws_iam_policy_document" "default" {
1536
}
1637
}
1738

39+
data "aws_ami" "default" {
40+
most_recent = "true"
41+
42+
filter {
43+
name = "name"
44+
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
45+
}
46+
47+
filter {
48+
name = "virtualization-type"
49+
values = ["hvm"]
50+
}
51+
52+
owners = ["099720109477"]
53+
}
54+
55+
data "aws_ami" "info" {
56+
filter {
57+
name = "image-id"
58+
values = ["${local.ami}"]
59+
}
60+
}
61+
1862
# Apply the tf_label module for this resource
1963
module "label" {
2064
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.1"
@@ -23,12 +67,8 @@ module "label" {
2367
name = "${var.name}"
2468
attributes = "${var.attributes}"
2569
delimiter = "${var.delimiter}"
26-
tags = "${var.tags}"
27-
}
28-
29-
locals {
30-
instance_count = "${var.instance_enabled ? 1 : 0}"
31-
security_group_count = "${var.create_default_security_group ? 1 : 0}"
70+
tags = "${merge(map("AZ", "${local.availability_zone}"), var.tags)}"
71+
enabled = "${local.instance_count ? "true" : "false"}"
3272
}
3373

3474
resource "aws_iam_instance_profile" "default" {
@@ -38,40 +78,12 @@ resource "aws_iam_instance_profile" "default" {
3878
}
3979

4080
resource "aws_iam_role" "default" {
41-
count = "${local.instance_count}"
42-
name = "${module.label.id}"
43-
path = "/"
44-
81+
count = "${local.instance_count}"
82+
name = "${module.label.id}"
83+
path = "/"
4584
assume_role_policy = "${data.aws_iam_policy_document.default.json}"
4685
}
4786

48-
resource "aws_security_group" "default" {
49-
count = "${local.security_group_count}"
50-
name = "${module.label.id}"
51-
vpc_id = "${var.vpc_id}"
52-
description = "Instance default security group (only egress access is allowed)"
53-
54-
tags {
55-
Name = "${module.label.id}"
56-
Namespace = "${var.namespace}"
57-
Stage = "${var.stage}"
58-
}
59-
60-
egress {
61-
protocol = "-1"
62-
from_port = 0
63-
to_port = 0
64-
65-
cidr_blocks = [
66-
"0.0.0.0/0",
67-
]
68-
}
69-
70-
lifecycle {
71-
create_before_destroy = true
72-
}
73-
}
74-
7587
# Apply the tf_github_authorized_keys module for this resource
7688
module "github_authorized_keys" {
7789
source = "git::https://github.com/cloudposse/terraform-template-user-data-github-authorized-keys.git?ref=tags/0.1.2"
@@ -80,39 +92,36 @@ module "github_authorized_keys" {
8092
github_team = "${var.github_team}"
8193
}
8294

83-
data "template_file" "user_data" {
84-
template = "${file("${path.module}/user_data.sh")}"
85-
86-
vars {
87-
user_data = "${join("\n", compact(concat(var.user_data, list(module.github_authorized_keys.user_data))))}"
88-
welcome_message = "${var.welcome_message}"
89-
ssh_user = "${var.ssh_user}"
90-
}
91-
}
92-
9395
resource "aws_instance" "default" {
94-
count = "${local.instance_count}"
95-
ami = "${var.ec2_ami}"
96-
instance_type = "${var.instance_type}"
97-
98-
user_data = "${data.template_file.user_data.rendered}"
96+
count = "${local.instance_count}"
97+
ami = "${local.ami}"
98+
availability_zone = "${local.availability_zone}"
99+
instance_type = "${var.instance_type}"
100+
ebs_optimized = "${var.ebs_optimized}"
101+
disable_api_termination = "${var.disable_api_termination}"
102+
user_data = "${data.template_file.user_data.rendered}"
103+
iam_instance_profile = "${aws_iam_instance_profile.default.name}"
104+
associate_public_ip_address = "${var.associate_public_ip_address}"
105+
key_name = "${var.ssh_key_pair}"
106+
subnet_id = "${var.subnet}"
107+
monitoring = "${var.monitoring}"
108+
private_ip = "${var.private_ip}"
109+
source_dest_check = "${var.source_dest_check}"
110+
ipv6_address_count = "${var.ipv6_address_count}"
111+
ipv6_addresses = "${var.ipv6_addresses}"
99112

100113
vpc_security_group_ids = [
101114
"${compact(concat(list(var.create_default_security_group ? join("", aws_security_group.default.*.id) : ""), var.security_groups))}",
102115
]
103116

104-
iam_instance_profile = "${aws_iam_instance_profile.default.name}"
105-
associate_public_ip_address = "${var.associate_public_ip_address}"
106-
107-
key_name = "${var.ssh_key_pair}"
108-
109-
subnet_id = "${var.subnet}"
110-
111-
tags {
112-
Name = "${module.label.id}"
113-
Namespace = "${var.namespace}"
114-
Stage = "${var.stage}"
117+
root_block_device {
118+
volume_type = "${local.root_volume_type}"
119+
volume_size = "${var.root_volume_size}"
120+
iops = "${local.root_iops}"
121+
delete_on_termination = "${var.delete_on_termination}"
115122
}
123+
124+
tags = "${module.label.tags}"
116125
}
117126

118127
resource "aws_eip" "default" {
@@ -121,46 +130,26 @@ resource "aws_eip" "default" {
121130
vpc = "true"
122131
}
123132

124-
# Restart dead or hung instance
125-
data "aws_region" "default" {
126-
current = true
127-
}
128-
129-
data "aws_caller_identity" "default" {}
130-
131-
resource "null_resource" "check_alarm_action" {
132-
count = "${local.instance_count}"
133+
resource "null_resource" "eip" {
134+
count = "${var.associate_public_ip_address && var.instance_enabled ? 1 : 0}"
133135

134-
triggers = {
135-
action = "arn:aws:swf:${data.aws_region.default.name}:${data.aws_caller_identity.default.account_id}:${var.default_alarm_action}"
136+
triggers {
137+
public_dns = "ec2-${replace(aws_eip.default.public_ip, ".", "-")}.${local.region == "us-east-1" ? "compute-1" : "${local.region}.compute"}.amazonaws.com"
136138
}
137139
}
138140

139-
resource "aws_cloudwatch_metric_alarm" "default" {
140-
count = "${local.instance_count}"
141-
alarm_name = "${module.label.id}"
142-
comparison_operator = "${var.comparison_operator}"
143-
evaluation_periods = "${var.evaluation_periods}"
144-
metric_name = "${var.metric_name}"
145-
namespace = "${var.metric_namespace}"
146-
period = "${var.applying_period}"
147-
statistic = "${var.statistic_level}"
148-
threshold = "${var.metric_threshold}"
149-
depends_on = ["null_resource.check_alarm_action"]
150-
151-
dimensions {
152-
InstanceId = "${aws_instance.default.id}"
153-
}
154-
155-
alarm_actions = [
156-
"${null_resource.check_alarm_action.triggers.action}",
157-
]
141+
resource "aws_ebs_volume" "default" {
142+
count = "${var.ebs_volume_count}"
143+
availability_zone = "${local.availability_zone}"
144+
size = "${var.ebs_volume_size}"
145+
iops = "${local.ebs_iops}"
146+
type = "${var.ebs_volume_type}"
147+
tags = "${module.label.tags}"
158148
}
159149

160-
resource "null_resource" "eip" {
161-
count = "${var.associate_public_ip_address && var.instance_enabled ? 1 : 0}"
162-
163-
triggers {
164-
public_dns = "ec2-${replace(aws_eip.default.public_ip, ".", "-")}.${data.aws_region.default.name == "us-east-1" ? "compute-1" : "${data.aws_region.default.name}.compute"}.amazonaws.com"
165-
}
150+
resource "aws_volume_attachment" "default" {
151+
count = "${var.ebs_volume_count}"
152+
device_name = "${element(var.ebs_device_name, count.index)}"
153+
volume_id = "${element(aws_ebs_volume.default.*.id, count.index)}"
154+
instance_id = "${aws_instance.default.id}"
166155
}

outputs.tf

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,64 @@
11
output "public_ip" {
2-
value = "${coalesce(join("", aws_eip.default.*.public_ip), aws_instance.default.public_ip)}"
2+
description = "Public IP of instance (or EIP )"
3+
value = "${coalesce(join("", aws_eip.default.*.public_ip), aws_instance.default.public_ip)}"
34
}
45

56
output "private_ip" {
6-
value = "${join("", aws_instance.default.*.private_ip)}"
7+
description = "Private IP of instance"
8+
value = "${join("", aws_instance.default.*.private_ip)}"
79
}
810

911
output "private_dns" {
10-
value = "${join("", aws_instance.default.*.private_dns)}"
12+
description = "Private DNS of instance"
13+
value = "${join("", aws_instance.default.*.private_dns)}"
1114
}
1215

1316
output "public_dns" {
14-
value = "${coalesce(join("", null_resource.eip.*.triggers.public_dns), aws_instance.default.public_dns)}"
17+
description = "Public DNS of instance (or DNS of EIP)"
18+
value = "${coalesce(join("", null_resource.eip.*.triggers.public_dns), aws_instance.default.public_dns)}"
1519
}
1620

1721
output "id" {
18-
value = "${join("", aws_instance.default.*.id)}"
22+
description = "Disambiguated ID"
23+
value = "${join("", aws_instance.default.*.id)}"
1924
}
2025

2126
output "ssh_key_pair" {
22-
value = "${var.ssh_key_pair}"
27+
description = "Name of used AWS SSH key"
28+
value = "${var.ssh_key_pair}"
2329
}
2430

2531
output "security_group_ids" {
26-
value = "${compact(concat(list(var.create_default_security_group ? join("", aws_security_group.default.*.id) : ""), var.security_groups))}"
32+
description = "ID on the new AWS Security Group associated with creating instance"
33+
value = "${compact(concat(list(var.create_default_security_group ? join("", aws_security_group.default.*.id) : ""), var.security_groups))}"
2734
}
2835

2936
output "role" {
30-
value = "${join("", aws_iam_role.default.*.name)}"
37+
description = "Name of AWS IAM Role associated with creating instance"
38+
value = "${join("", aws_iam_role.default.*.name)}"
3139
}
3240

3341
output "alarm" {
34-
value = "${join("", aws_cloudwatch_metric_alarm.default.*.id)}"
42+
description = "CloudWatch Alarm ID"
43+
value = "${join("", aws_cloudwatch_metric_alarm.default.*.id)}"
3544
}
3645

3746
output "additional_eni_ids" {
38-
value = "${zipmap(aws_network_interface.additional.*.id, aws_eip.additional.*.public_ip)}"
47+
description = "Map of ENI with EIP"
48+
value = "${zipmap(aws_network_interface.additional.*.id, aws_eip.additional.*.public_ip)}"
49+
}
50+
51+
output "ebs_ids" {
52+
description = "ID of EBSs"
53+
value = "${aws_ebs_volume.default.*.id}"
54+
}
55+
56+
output "primary_network_interface_id" {
57+
description = "ID of the instance's primary network interface"
58+
value = "${aws_instance.default.primary_network_interface_id}"
59+
}
60+
61+
output "network_interface_id" {
62+
description = "ID of the network interface that was created with the instance"
63+
value = "${aws_instance.default.network_interface_id}"
3964
}

security_group.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
resource "aws_security_group" "default" {
2+
count = "${local.security_group_count}"
3+
name = "${module.label.id}"
4+
vpc_id = "${var.vpc_id}"
5+
description = "Instance default security group (only egress access is allowed)"
6+
tags = "${module.label.tags}"
7+
8+
lifecycle {
9+
create_before_destroy = true
10+
}
11+
}
12+
13+
resource "aws_security_group_rule" "egress" {
14+
type = "egress"
15+
from_port = 0
16+
to_port = 65535
17+
protocol = "-1"
18+
cidr_blocks = ["0.0.0.0/0"]
19+
security_group_id = "${aws_security_group.default.id}"
20+
}
21+
22+
resource "aws_security_group_rule" "ingress" {
23+
count = "${length(compact(var.allowed_ports))}"
24+
type = "ingress"
25+
from_port = "${element(var.allowed_ports, count.index)}"
26+
to_port = "${element(var.allowed_ports, count.index)}"
27+
protocol = "tcp"
28+
cidr_blocks = ["0.0.0.0/0"]
29+
security_group_id = "${aws_security_group.default.id}"
30+
}

0 commit comments

Comments
 (0)