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

Commit 8474938

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.
1 parent b1b7348 commit 8474938

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

modules/single-node-asg/main.tf

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,69 @@ 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+
}
58+
59+
resource "aws_iam_role_policy_attachment" "associate_eip" {
60+
role = module.instance_profile.iam_role_name
61+
policy_arn = aws_iam_policy.associate_eip_policy.arn
62+
}
63+
64+
resource "aws_iam_policy" "associate_eip_policy" {
65+
name = "associate_address"
66+
policy = data.aws_iam_policy_document.associate_eip_policy_doc.json
67+
}
68+
69+
data "aws_iam_policy_document" "associate_eip_policy_doc" {
70+
statement {
71+
sid = ""
72+
effect = "Allow"
73+
actions = [
74+
"ec2:AssociateAddress"
75+
]
76+
resources = ["*"]
77+
}
78+
}
79+
5580
# Create an ASG with just 1 EC2 instance
5681
module "server" {
5782
source = "../asg"
5883

59-
ami = var.ami
60-
elb_names = var.load_balancers
61-
key_name = var.key_name
84+
ami = var.ami
85+
elb_names = var.load_balancers
86+
key_name = var.key_name
6287
# The IAM Instance Profile w/ attach_ebs role
63-
iam_profile = module.instance_profile.iam_profile_id
64-
instance_type = var.instance_type
65-
# 1 EC2 instance <> 1 EBS volume
66-
max_nodes = 1
67-
min_nodes = 1
68-
placement_group = var.placement_group
69-
public_ip = var.public_ip
88+
iam_profile = module.instance_profile.iam_profile_id
89+
instance_type = var.instance_type
90+
# 1 EC2 instance <> 1 EBS volume
91+
max_nodes = 1
92+
min_nodes = 1
93+
placement_group = var.placement_group
94+
public_ip = var.public_ip
7095
# the prefix and suffix names are combined in
7196
# the `asg` module to create the full name
72-
name_prefix = var.name_prefix
73-
name_suffix = "${var.name_suffix}-${local.az}"
74-
97+
name_prefix = var.name_prefix
98+
name_suffix = "${var.name_suffix}-${local.az}"
7599
root_volume_type = var.root_volume_type
76100
root_volume_size = var.root_volume_size
77101
security_group_ids = var.security_group_ids
78102
subnet_ids = [var.subnet_id]
79103

80104
user_data = <<END_INIT
81105
#!/bin/bash
106+
<<<<<<< HEAD
82107
# exec > /tmp/init.log
83108
# exec 2> /tmp/init-err.log
84109
# set -x
110+
=======
111+
apt update
112+
>>>>>>> 2b82522... New function: single-node-asg module supports binding EIP by itself.
85113
${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
86118
${module.init-attach-ebs.init_snippet}
87119
${var.init_suffix}
88120
END_INIT
@@ -95,3 +127,9 @@ module "init-attach-ebs" {
95127
region = var.region
96128
volume_id = module.service-data.volume_id
97129
}
130+
131+
module "init-install-awscli" {
132+
source = "../init-snippet-install-awscli"
133+
}
134+
135+

modules/single-node-asg/outputs.tf

Lines changed: 4 additions & 0 deletions
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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ variable "data_volume_size" {
5656
variable "data_volume_encrypted" {
5757
default = true
5858
description = "Boolean, whether or not to encrypt the EBS block device"
59-
type = string
59+
type = bool
6060
}
6161

6262
variable "data_volume_kms_key_id" {
@@ -92,7 +92,7 @@ variable "init_suffix" {
9292
variable "public_ip" {
9393
default = true
9494
description = "Boolean flag to enable/disable `map_public_ip_on_launch` in the launch configuration"
95-
type = string
95+
type = bool
9696
}
9797

9898
variable "subnet_id" {
@@ -115,3 +115,9 @@ variable "load_balancers" {
115115
description = "The list of load balancers names to pass to the ASG module"
116116
type = list(string)
117117
}
118+
119+
variable "assign_eip" {
120+
default = false
121+
description = "Whether or not associating an EIP with the node."
122+
type = bool
123+
}

0 commit comments

Comments
 (0)