Skip to content

Commit f41e429

Browse files
s2504sconst-bon
authored andcommitted
Utilize TF locals feature in order to implement instance switch option (#11)
1 parent c3d22ae commit f41e429

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Module directory
22
.terraform/
3-
3+
.idea

main.tf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,27 @@ module "label" {
2828
tags = "${var.tags}"
2929
}
3030

31+
locals {
32+
instance_count = "${var.instance_enabled ? 1 : 0}"
33+
security_group_count = "${var.create_default_security_group ? 1 : 0}"
34+
}
35+
3136
resource "aws_iam_instance_profile" "default" {
32-
count = "${var.instance_enabled}"
37+
count = "${local.instance_count}"
3338
name = "${module.label.id}"
3439
role = "${aws_iam_role.default.name}"
3540
}
3641

3742
resource "aws_iam_role" "default" {
38-
count = "${var.instance_enabled}"
43+
count = "${local.instance_count}"
3944
name = "${module.label.id}"
4045
path = "/"
4146

4247
assume_role_policy = "${data.aws_iam_policy_document.default.json}"
4348
}
4449

4550
resource "aws_security_group" "default" {
46-
count = "${var.create_default_security_group}"
51+
count = "${local.security_group_count}"
4752
name = "${module.label.id}"
4853
vpc_id = "${var.vpc_id}"
4954
description = "Instance default security group (only egress access is allowed)"
@@ -88,7 +93,7 @@ data "template_file" "user_data" {
8893
}
8994

9095
resource "aws_instance" "default" {
91-
count = "${var.instance_enabled}"
96+
count = "${local.instance_count}"
9297
ami = "${var.ec2_ami}"
9398
instance_type = "${var.instance_type}"
9499

@@ -135,15 +140,15 @@ data "aws_region" "default" {
135140
data "aws_caller_identity" "default" {}
136141

137142
resource "null_resource" "check_alarm_action" {
138-
count = "${var.instance_enabled}"
143+
count = "${local.instance_count}"
139144

140145
triggers = {
141146
action = "arn:aws:swf:${data.aws_region.default.name}:${data.aws_caller_identity.default.account_id}:${var.default_alarm_action}"
142147
}
143148
}
144149

145150
resource "aws_cloudwatch_metric_alarm" "default" {
146-
count = "${var.instance_enabled}"
151+
count = "${local.instance_count}"
147152
alarm_name = "${module.label.id}"
148153
comparison_operator = "${var.comparison_operator}"
149154
evaluation_periods = "${var.evaluation_periods}"

0 commit comments

Comments
 (0)