Skip to content

Commit

Permalink
Utilize TF locals feature in order to implement instance switch opt…
Browse files Browse the repository at this point in the history
…ion (#11)
  • Loading branch information
s2504s authored and const-bon committed Oct 12, 2017
1 parent c3d22ae commit f41e429
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Module directory
.terraform/

.idea
17 changes: 11 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@ module "label" {
tags = "${var.tags}"
}

locals {
instance_count = "${var.instance_enabled ? 1 : 0}"
security_group_count = "${var.create_default_security_group ? 1 : 0}"
}

resource "aws_iam_instance_profile" "default" {
count = "${var.instance_enabled}"
count = "${local.instance_count}"
name = "${module.label.id}"
role = "${aws_iam_role.default.name}"
}

resource "aws_iam_role" "default" {
count = "${var.instance_enabled}"
count = "${local.instance_count}"
name = "${module.label.id}"
path = "/"

assume_role_policy = "${data.aws_iam_policy_document.default.json}"
}

resource "aws_security_group" "default" {
count = "${var.create_default_security_group}"
count = "${local.security_group_count}"
name = "${module.label.id}"
vpc_id = "${var.vpc_id}"
description = "Instance default security group (only egress access is allowed)"
Expand Down Expand Up @@ -88,7 +93,7 @@ data "template_file" "user_data" {
}

resource "aws_instance" "default" {
count = "${var.instance_enabled}"
count = "${local.instance_count}"
ami = "${var.ec2_ami}"
instance_type = "${var.instance_type}"

Expand Down Expand Up @@ -135,15 +140,15 @@ data "aws_region" "default" {
data "aws_caller_identity" "default" {}

resource "null_resource" "check_alarm_action" {
count = "${var.instance_enabled}"
count = "${local.instance_count}"

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

resource "aws_cloudwatch_metric_alarm" "default" {
count = "${var.instance_enabled}"
count = "${local.instance_count}"
alarm_name = "${module.label.id}"
comparison_operator = "${var.comparison_operator}"
evaluation_periods = "${var.evaluation_periods}"
Expand Down

0 comments on commit f41e429

Please sign in to comment.