-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
97 lines (78 loc) · 2.82 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
module "label" {
source = "git::https://github.com/brdelphus/terraform-null-label.git?ref=master"
stage = "${var.stage}"
name = "${var.name}"
namespace = "${var.namespace}"
attributes = "${var.attributes}"
}
data "template_file" "cloud_init_cloudwatch_agent" {
template = "${file("${path.module}/templates/cloud_init.yaml")}"
vars = {
cloudwatch_agent_configuration = "${var.metrics_config == "standard" ? base64encode(data.template_file.cloudwatch_agent_configuration_standard.rendered) : base64encode(data.template_file.cloudwatch_agent_configuration_advanced.rendered)}"
}
}
data "template_file" "cloudwatch_agent_configuration_advanced" {
template = "${file("${path.module}/templates/cloudwatch_agent_configuration_advanced.json")}"
vars = {
aggregation_dimensions = "${jsonencode(var.aggregation_dimensions)}"
cpu_resources = "${var.cpu_resources}"
disk_resources = "${jsonencode(var.disk_resources)}"
metrics_collection_interval = "${var.metrics_collection_interval}"
}
}
data "template_file" "cloudwatch_agent_configuration_standard" {
template = "${file("${path.module}/templates/cloudwatch_agent_configuration_standard.json")}"
vars = {
aggregation_dimensions = "${jsonencode(var.aggregation_dimensions)}"
cpu_resources = "${var.cpu_resources}"
disk_resources = "${jsonencode(var.disk_resources)}"
metrics_collection_interval = "${var.metrics_collection_interval}"
}
}
data "template_cloudinit_config" "cloud_init_merged" {
gzip = true
base64_encode = true
part {
filename = "userdata_part_cloudwatch.cfg"
content = "${data.template_file.cloud_init_cloudwatch_agent.rendered}"
content_type = "text/cloud-config"
}
part {
filename = "userdata_part_caller.cfg"
content = "${var.userdata_part_content}"
content_type = "${var.userdata_part_content_type}"
merge_type = "${var.userdata_part_merge_type}"
}
}
data "aws_iam_policy_document" "ec2_cloudwatch" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ec2_cloudwatch" {
name = "${module.label.id}"
assume_role_policy = "${data.aws_iam_policy_document.ec2_cloudwatch.json}"
tags = {
Name = "${module.label.id}"
}
}
data "aws_iam_policy_document" "wildcard_cloudwatch_agent" {
statement {
effect = "Allow"
actions = [
"ec2:DescribeTags",
"cloudwatch:PutMetricData",
]
resources = ["*"]
}
}
resource "aws_iam_role_policy" "wildcard_cloudwatch_agent" {
name = "${module.label.id}"
role = "${aws_iam_role.ec2_cloudwatch.id}"
policy = "${data.aws_iam_policy_document.wildcard_cloudwatch_agent.json}"
}