/
main.tf
102 lines (86 loc) · 2.95 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# ------------------------------------------------------------------------------
# Resources
# ------------------------------------------------------------------------------
resource "aws_iam_role" "main" {
name = "${var.name_prefix}-role"
assume_role_policy = "${data.aws_iam_policy_document.main.json}"
}
data "aws_iam_policy_document" "main" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_instance_profile" "main" {
name = "${var.name_prefix}-profile"
role = "${aws_iam_role.main.name}"
}
resource "aws_iam_role_policy" "main" {
name = "${var.name_prefix}-permissions"
role = "${aws_iam_role.main.id}"
policy = "${var.instance_policy}"
}
resource "aws_security_group" "main" {
name = "${var.name_prefix}-sg"
description = "Terraformed security group."
vpc_id = "${var.vpc_id}"
tags = "${merge(var.tags, map("Name", "${var.name_prefix}-sg"))}"
}
resource "aws_security_group_rule" "egress" {
security_group_id = "${aws_security_group.main.id}"
type = "egress"
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_launch_configuration" "main" {
name_prefix = "${var.name_prefix}-asg-"
instance_type = "${var.instance_type}"
iam_instance_profile = "${aws_iam_instance_profile.main.name}"
security_groups = ["${aws_security_group.main.id}"]
image_id = "${var.instance_ami}"
key_name = "${var.instance_key}"
user_data = "${var.user_data}"
root_block_device {
volume_type = "gp2"
volume_size = "${var.instance_volume_size}"
delete_on_termination = true
}
lifecycle {
create_before_destroy = true
}
}
locals {
asg_tags = "${merge(var.tags, map("Name", "${var.name_prefix}"))}"
}
data "null_data_source" "autoscaling" {
count = "${length(local.asg_tags)}"
inputs = {
Key = "${element(keys(local.asg_tags), count.index)}"
Value = "${element(values(local.asg_tags), count.index)}"
PropagateAtLaunch = "TRUE"
}
}
resource "aws_cloudformation_stack" "main" {
depends_on = ["aws_launch_configuration.main"]
name = "${var.name_prefix}-asg"
template_body = "${data.template_file.main.rendered}"
}
data "template_file" "main" {
template = "${file("${path.module}/cloudformation.yml")}"
vars {
launch_configuration = "${aws_launch_configuration.main.name}"
health_check_type = "${var.health_check_type}"
await_signal = "${var.await_signal}"
pause_time = "${var.pause_time}"
min_size = "${var.min_size}"
max_size = "${var.max_size}"
subnets = "${jsonencode(var.subnet_ids)}"
tags = "${jsonencode(data.null_data_source.autoscaling.*.outputs)}"
}
}