Skip to content

Commit acc9456

Browse files
SweetOpsconst-bon
authored andcommitted
Use EIP based DNS name as a public_dns output (#6)
1 parent 03fe0ce commit acc9456

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ resource "aws_ami_from_instance" "example" {
8787
| Name | Description |
8888
|:--------------------|:-------------------------------------------------------------------|
8989
| `id` | Disambiguated ID |
90-
| `public_dns` | Normalized name |
91-
| `public_ip` | Normalized namespace |
9290
| `private_dns` | Normalized name |
9391
| `private_ip` | Normalized namespace |
92+
| `public_ip` | Public IP of instance (or EIP ) |
93+
| `public_dns` | Public DNS of instance (or DNS of EIP) |
9494
| `ssh_key_pair` | Name of used AWS SSH key |
9595
| `security_group_id` | ID on the new AWS Security Group associated with creating instance |
9696
| `role` | Name of AWS IAM Role associated with creating instance |

main.tf

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,21 @@ resource "aws_instance" "default" {
106106
}
107107

108108
resource "aws_eip" "default" {
109-
count = "${signum(length(var.associate_public_ip_address)) == 1 ? 1 : 0}"
109+
count = "${var.associate_public_ip_address ? 1 : 0}"
110110
instance = "${aws_instance.default.id}"
111111
vpc = true
112112
}
113113

114114
# Apply the provisioner module for this resource
115115
module "ansible" {
116-
source = "git::https://github.com/cloudposse/tf_ansible.git?ref=tags/0.3.4"
116+
source = "git::https://github.com/cloudposse/tf_ansible.git?ref=tags/0.3.6"
117117
arguments = "${var.ansible_arguments}"
118-
envs = "${compact(concat(var.ansible_envs, list("host=${var.associate_public_ip_address ? aws_instance.default.public_dns : aws_instance.default.private_dns }")))}"
118+
envs = "${compact(concat(var.ansible_envs, list("host=${var.associate_public_ip_address ? join("", aws_eip.default.*.public_ip) : aws_instance.default.private_ip }")))}"
119119
playbook = "${var.ansible_playbook}"
120120
dry_run = "${var.ansible_dry_run}"
121121
}
122122

123123
# Restart dead or hung instance
124-
125124
data "aws_region" "default" {
126125
current = true
127126
}
@@ -153,3 +152,11 @@ resource "aws_cloudwatch_metric_alarm" "default" {
153152
"${null_resource.check_alarm_action.triggers.action}",
154153
]
155154
}
155+
156+
resource "null_resource" "eip" {
157+
count = "${var.associate_public_ip_address ? 1 : 0}"
158+
159+
triggers {
160+
public_dns = "ec2-${replace(aws_eip.default.public_ip, ".", "-")}.${data.aws_region.default.name == "us-east-1" ? "compute-1" : "${data.aws_region.default.name}.compute"}.amazonaws.com"
161+
}
162+
}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "public_ip" {
2-
value = "${aws_eip.default.public_ip}"
2+
value = "${coalesce(join("", aws_eip.default.*.public_ip), aws_instance.default.public_ip)}"
33
}
44

55
output "private_ip" {
@@ -11,7 +11,7 @@ output "private_dns" {
1111
}
1212

1313
output "public_dns" {
14-
value = "${aws_instance.default.public_dns}"
14+
value = "${coalesce(join("", null_resource.eip.*.triggers.public_dns), aws_instance.default.public_dns)}"
1515
}
1616

1717
output "id" {

0 commit comments

Comments
 (0)