Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 8e9e53e

Browse files
committed
New function: single-node-asg module supports binding EIP by itself.
Since it is single node, binding an EIP to the instance is possible. And it eases other things since the public interface is constant. Add assign_eip variable to single-node-asg. If turns it on, an EIP will be allocated, and assocated with the instance. Scope VPC is specified in case the account does not have a default VPC.
1 parent a7b028e commit 8e9e53e

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

modules/single-node-asg/main.tf

+38-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ module "service-data" {
5252
iam_instance_profile_role_name = module.instance_profile.iam_role_name
5353
}
5454

55+
resource "aws_eip" "eip" {
56+
count = var.assign_eip ? 1 : 0
57+
vpc = true
58+
}
59+
60+
resource "aws_iam_role_policy_attachment" "associate_eip" {
61+
role = module.instance_profile.iam_role_name
62+
policy_arn = aws_iam_policy.associate_eip_policy.arn
63+
}
64+
65+
resource "aws_iam_policy" "associate_eip_policy" {
66+
name = "associate_address"
67+
policy = data.aws_iam_policy_document.associate_eip_policy_doc.json
68+
}
69+
70+
data "aws_iam_policy_document" "associate_eip_policy_doc" {
71+
statement {
72+
sid = ""
73+
effect = "Allow"
74+
actions = [
75+
"ec2:AssociateAddress"
76+
]
77+
resources = ["*"]
78+
}
79+
}
80+
5581
# Create an ASG with just 1 EC2 instance
5682
module "server" {
5783
source = "../asg"
@@ -66,12 +92,11 @@ module "server" {
6692
max_nodes = 1
6793
min_nodes = 1
6894
placement_group = var.placement_group
69-
public_ip = var.public_ip
95+
public_ip = var.assign_eip ? false : var.public_ip
7096
# the prefix and suffix names are combined in
7197
# the `asg` module to create the full name
72-
name_prefix = var.name_prefix
73-
name_suffix = "${var.name_suffix}-${local.az}"
74-
98+
name_prefix = var.name_prefix
99+
name_suffix = "${var.name_suffix}-${local.az}"
75100
root_volume_type = var.root_volume_type
76101
root_volume_size = var.root_volume_size
77102
security_group_ids = var.security_group_ids
@@ -84,7 +109,12 @@ module "server" {
84109
# exec > /tmp/init.log
85110
# exec 2> /tmp/init-err.log
86111
# set -x
112+
apt update
87113
${var.init_prefix}
114+
${module.init-install-awscli.init_snippet}
115+
while ! ${var.assign_eip ? "aws ec2 associate-address --instance-id \"$(ec2metadata --instance-id)\" --region \"${var.region}\" --allocation-id \"${element(aws_eip.eip.*.id, 0)}\"" : "true"}; do
116+
sleep 1
117+
done
88118
${module.init-attach-ebs.init_snippet}
89119
${var.init_suffix}
90120
END_INIT
@@ -97,3 +127,7 @@ module "init-attach-ebs" {
97127
region = var.region
98128
volume_id = module.service-data.volume_id
99129
}
130+
131+
module "init-install-awscli" {
132+
source = "../init-snippet-install-awscli"
133+
}

modules/single-node-asg/outputs.tf

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ output "data_volume_name_tag" {
1212
value = "${local.data_volume_name_prefix}-${local.az}"
1313
description = "Name tag value for attached data volume"
1414
}
15+
16+
output "eip_address" {
17+
value = var.assign_eip ? aws_eip.eip.*[0].public_ip : ""
18+
}

modules/single-node-asg/variables.tf

+8-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ variable "data_volume_size" {
6262
variable "data_volume_encrypted" {
6363
default = true
6464
description = "Boolean, whether or not to encrypt the EBS block device"
65-
type = string
65+
type = bool
6666
}
6767

6868
variable "data_volume_kms_key_id" {
@@ -98,7 +98,7 @@ variable "init_suffix" {
9898
variable "public_ip" {
9999
default = true
100100
description = "Boolean flag to enable/disable `map_public_ip_on_launch` in the launch configuration"
101-
type = string
101+
type = bool
102102
}
103103

104104
variable "subnet_id" {
@@ -121,3 +121,9 @@ variable "load_balancers" {
121121
description = "The list of load balancers names to pass to the ASG module"
122122
type = list(string)
123123
}
124+
125+
variable "assign_eip" {
126+
default = false
127+
description = "Whether or not associating an EIP with the node."
128+
type = bool
129+
}

0 commit comments

Comments
 (0)