From 8255d8c58762e02a9e9ac70d6811c86c5d5a060a Mon Sep 17 00:00:00 2001 From: Cam Jackson Date: Sat, 4 Nov 2017 16:18:24 +0800 Subject: [PATCH] Pass instance IP around instead of ID - Explanation: https://github.com/hashicorp/terraform/issues/16559 --- terraform/apps/core.tf | 11 +++++++++-- terraform/apps/docker-node-app/data.tf | 5 ++++- terraform/apps/docker-node-app/docker.tf | 2 +- .../event-forwarder/event-forwarder.tf | 2 +- terraform/apps/docker-node-app/networking.tf | 2 +- terraform/apps/docker-node-app/variables.tf | 4 ++-- terraform/apps/group-mailer.tf | 2 +- terraform/apps/mailer.tf | 2 +- terraform/apps/variables.tf | 4 ++-- terraform/base/outputs.tf | 4 ++-- terraform/main.tf | 2 +- 11 files changed, 25 insertions(+), 15 deletions(-) diff --git a/terraform/apps/core.tf b/terraform/apps/core.tf index 1c6e0d8..b93d73e 100644 --- a/terraform/apps/core.tf +++ b/terraform/apps/core.tf @@ -3,7 +3,7 @@ module "core_app" { name = "core" docker_image = "rabblerouser/core" port = "${var.app_ports["core"]}" - aws_instance_id = "${var.aws_instance_id}" + aws_instance_ip = "${var.aws_instance_ip}" alb_listener_arn = "${var.alb_listener_arn}" alb_listener_rule_priority = "1" docker_credentials = "${var.docker_credentials}" @@ -14,10 +14,17 @@ module "core_app" { env = ["SESSION_SECRET=${random_id.session_secret.hex}"] } +data "aws_instance" "instance" { + filter { + name = "ip-address" + values = ["${var.aws_instance_ip}"] + } +} + resource "random_id" "session_secret" { keepers = { # Generate a new session secret when instance ID changes - ec2_instance_id = "${var.aws_instance_id}" + ec2_instance_id = "${data.aws_instance.instance.id}" } byte_length = 32 } diff --git a/terraform/apps/docker-node-app/data.tf b/terraform/apps/docker-node-app/data.tf index 8b65cb1..b73ab31 100644 --- a/terraform/apps/docker-node-app/data.tf +++ b/terraform/apps/docker-node-app/data.tf @@ -6,7 +6,10 @@ data "aws_vpc" "default_vpc" { } data "aws_instance" "instance" { - instance_id = "${var.aws_instance_id}" + filter { + name = "ip-address" + values = ["${var.aws_instance_ip}"] + } } data "aws_lb_listener" "alb_listener" { diff --git a/terraform/apps/docker-node-app/docker.tf b/terraform/apps/docker-node-app/docker.tf index 761019b..1282d48 100644 --- a/terraform/apps/docker-node-app/docker.tf +++ b/terraform/apps/docker-node-app/docker.tf @@ -1,6 +1,6 @@ provider "docker" { version = "~> 0.1.0" - host = "tcp://${data.aws_instance.instance.public_ip}:2376" + host = "tcp://${var.aws_instance_ip}:2376" key_material = "${var.docker_credentials["key"]}" ca_material = "${var.docker_credentials["ca"]}" cert_material = "${var.docker_credentials["cert"]}" diff --git a/terraform/apps/docker-node-app/event-forwarder/event-forwarder.tf b/terraform/apps/docker-node-app/event-forwarder/event-forwarder.tf index 2ffdedd..7c84772 100644 --- a/terraform/apps/docker-node-app/event-forwarder/event-forwarder.tf +++ b/terraform/apps/docker-node-app/event-forwarder/event-forwarder.tf @@ -18,7 +18,7 @@ data "aws_s3_bucket_object" "event_forwarder_zip" { resource "random_id" "auth_token" { keepers = { - # Generate a new token when the lambda code updates or the EC2 instance changes + # Generate a new token when the lambda code updates lambda_zip_version = "${data.aws_s3_bucket_object.event_forwarder_zip.version_id}" } diff --git a/terraform/apps/docker-node-app/networking.tf b/terraform/apps/docker-node-app/networking.tf index 5a54f49..5796f71 100644 --- a/terraform/apps/docker-node-app/networking.tf +++ b/terraform/apps/docker-node-app/networking.tf @@ -35,5 +35,5 @@ resource "aws_lb_target_group" "alb_target_group" { resource "aws_lb_target_group_attachment" "alb_target_attachment" { target_group_arn = "${aws_lb_target_group.alb_target_group.arn}" - target_id = "${var.aws_instance_id}" + target_id = "${data.aws_instance.instance.id}" } diff --git a/terraform/apps/docker-node-app/variables.tf b/terraform/apps/docker-node-app/variables.tf index c195574..b4b84ca 100644 --- a/terraform/apps/docker-node-app/variables.tf +++ b/terraform/apps/docker-node-app/variables.tf @@ -19,8 +19,8 @@ variable "env" { default = [] } -variable "aws_instance_id" { - description = "The ID of the EC2 instance where the app should be deployed" +variable "aws_instance_ip" { + description = "The public IP address of the EC2 instance where this app should be deployed" type = "string" } diff --git a/terraform/apps/group-mailer.tf b/terraform/apps/group-mailer.tf index cedca83..14d2f8c 100644 --- a/terraform/apps/group-mailer.tf +++ b/terraform/apps/group-mailer.tf @@ -3,7 +3,7 @@ module "group_mailer_app" { name = "group-mailer" docker_image = "rabblerouser/group-mailer" port = "${var.app_ports["group_mailer"]}" - aws_instance_id = "${var.aws_instance_id}" + aws_instance_ip = "${var.aws_instance_ip}" alb_listener_arn = "${var.alb_listener_arn}" alb_listener_rule_priority = "3" docker_credentials = "${var.docker_credentials}" diff --git a/terraform/apps/mailer.tf b/terraform/apps/mailer.tf index 9ad5c72..1cb2bd2 100644 --- a/terraform/apps/mailer.tf +++ b/terraform/apps/mailer.tf @@ -3,7 +3,7 @@ module "mailer_app" { name = "mailer" docker_image = "rabblerouser/mailer" port = "${var.app_ports["mailer"]}" - aws_instance_id = "${var.aws_instance_id}" + aws_instance_ip = "${var.aws_instance_ip}" alb_listener_arn = "${var.alb_listener_arn}" alb_listener_rule_priority = "2" docker_credentials = "${var.docker_credentials}" diff --git a/terraform/apps/variables.tf b/terraform/apps/variables.tf index d2f23e3..72244aa 100644 --- a/terraform/apps/variables.tf +++ b/terraform/apps/variables.tf @@ -13,8 +13,8 @@ variable "app_ports" { type = "map" } -variable "aws_instance_id" { - description = "The ID of the EC2 instance where this app should be deployed" +variable "aws_instance_ip" { + description = "The public IP address of the EC2 instance where this app should be deployed" type = "string" } diff --git a/terraform/base/outputs.tf b/terraform/base/outputs.tf index dbab446..a63206f 100644 --- a/terraform/base/outputs.tf +++ b/terraform/base/outputs.tf @@ -2,8 +2,8 @@ output "host_ip" { value = "${aws_instance.web.public_ip}" } -output "aws_instance_id" { - value = "${aws_instance.web.id}" +output "aws_instance_ip" { + value = "${aws_instance.web.public_ip}" } output "alb_listener_arn" { diff --git a/terraform/main.tf b/terraform/main.tf index c990e8e..7f55594 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -55,7 +55,7 @@ module apps { route53_zone_id = "${local.route53_zone_id}" app_ports = "${local.app_ports}" - aws_instance_id = "${module.base.aws_instance_id}" + aws_instance_ip = "${module.base.aws_instance_ip}" alb_listener_arn = "${module.base.alb_listener_arn}" docker_credentials = "${module.base.docker_credentials}" stream_name = "${module.base.stream_name}"