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

Commit 269149c

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 10779bd commit 269149c

File tree

4 files changed

+61
-22
lines changed

4 files changed

+61
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
### Modules
7-
7+
* `single-node-asg`: New function: support associating EIP.
88

99
### Examples
1010

modules/single-node-asg/main.tf

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,59 @@ 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-
azs = [local.az]
61-
elb_names = var.load_balancers
62-
key_name = var.key_name
63-
# The IAM Instance Profile w/ attach_ebs role
64-
iam_profile = module.instance_profile.iam_profile_id
65-
instance_type = var.instance_type
66-
# 1 EC2 instance <> 1 EBS volume
67-
max_nodes = 1
68-
min_nodes = 1
69-
placement_group = var.placement_group
70-
public_ip = var.public_ip
71-
# the prefix and suffix names are combined in
72-
# the `asg` module to create the full name
73-
name_prefix = var.name_prefix
74-
name_suffix = "${var.name_suffix}-${local.az}"
75-
84+
ami = var.ami
85+
elb_names = var.load_balancers
86+
key_name = var.key_name
87+
iam_profile = module.instance_profile.iam_profile_id
88+
instance_type = var.instance_type
89+
max_nodes = 1
90+
min_nodes = 1
91+
placement_group = var.placement_group
92+
public_ip = var.public_ip
93+
name_prefix = var.name_prefix
94+
name_suffix = "${var.name_suffix}-${local.az}"
7695
root_volume_type = var.root_volume_type
7796
root_volume_size = var.root_volume_size
7897
security_group_ids = var.security_group_ids
7998
subnet_ids = [var.subnet_id]
8099

81100
user_data = <<END_INIT
82101
#!/bin/bash
102+
apt update
83103
${var.init_prefix}
104+
${module.init-install-awscli.init_snippet}
105+
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
106+
sleep 1
107+
done
84108
${module.init-attach-ebs.init_snippet}
85109
${var.init_suffix}
86110
END_INIT
@@ -89,7 +113,13 @@ END_INIT
89113

90114
# Render init snippet - boxed module to attach the EBS volume to the node
91115
module "init-attach-ebs" {
92-
source = "../init-snippet-attach-ebs-volume"
93-
region = var.region
116+
source = "../init-snippet-attach-ebs-volume"
117+
region = var.region
94118
volume_id = module.service-data.volume_id
95119
}
120+
121+
module "init-install-awscli" {
122+
source = "../init-snippet-install-awscli"
123+
}
124+
125+

modules/single-node-asg/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ output "asg_iam_role_name" {
77
value = module.instance_profile.iam_role_name
88
description = "`name` exported from the Service Data `aws_iam_role`"
99
}
10+
11+
output "eip_address" {
12+
value = aws_eip.eip.*[0].public_ip
13+
}

modules/single-node-asg/variables.tf

Lines changed: 7 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" {
@@ -116,3 +116,8 @@ variable "load_balancers" {
116116
type = list(string)
117117
}
118118

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)