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

Commit 2b82522

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 2b82522

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

CHANGELOG.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22

33
### Summary
44

5+
### Modules
6+
7+
### Examples
8+
9+
10+
# v0.9.9
11+
12+
### Summary
13+
14+
New feature for `asg` module and bugfix for `tf-cloud-credentials` module.
515

616
### Modules
717

18+
* `asg`: Allow ASG instances to have additional EBS block devices
19+
* `tf-cloud-credentials`: Switch the module to use a datasource to lookup an
20+
existing TF Cloud workspace instead of trying to create one.
821

922
### Examples
1023

24+
* No changes.
25+
1126

1227
# v0.9.8
1328

@@ -20,7 +35,8 @@
2035
* `iam-users`: fixed error from zipmap in outputs when a user gets deleted
2136
from user list
2237
* `tf-cloud-credential`: minor interpolation cleanup, added module to tests
23-
* `setup-meta-infrastructure`: Parameterize password length and age for iam password policy.
38+
* `setup-meta-infrastructure`: Parameterize password length and age for iam
39+
password policy.
2440
* `iam-instance-profile`: Add role ID ouput for IAM instance profile module.
2541

2642
### Examples
@@ -87,7 +103,6 @@
87103
You will need to _manually_ remove the conflicting route (which was created
88104
by the old inline route), for example in the AWS console, and then re-apply
89105
to add it back.
90-
* `tf-cloud-credentials`: Switches the module to use a datasource to lookup an existing TF Cloud workspace instead of trying to create one.
91106

92107
### Examples
93108

modules/single-node-asg/main.tf

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,63 @@ 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
84+
ami = var.ami
85+
elb_names = var.load_balancers
86+
key_name = var.key_name
6387
# 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
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
7195
# the prefix and suffix names are combined in
7296
# the `asg` module to create the full name
73-
name_prefix = var.name_prefix
74-
name_suffix = "${var.name_suffix}-${local.az}"
75-
97+
name_prefix = var.name_prefix
98+
name_suffix = "${var.name_suffix}-${local.az}"
7699
root_volume_type = var.root_volume_type
77100
root_volume_size = var.root_volume_size
78101
security_group_ids = var.security_group_ids
79102
subnet_ids = [var.subnet_id]
80103

81104
user_data = <<END_INIT
82105
#!/bin/bash
106+
apt update
83107
${var.init_prefix}
108+
${module.init-install-awscli.init_snippet}
109+
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
110+
sleep 1
111+
done
84112
${module.init-attach-ebs.init_snippet}
85113
${var.init_suffix}
86114
END_INIT
@@ -89,7 +117,13 @@ END_INIT
89117

90118
# Render init snippet - boxed module to attach the EBS volume to the node
91119
module "init-attach-ebs" {
92-
source = "../init-snippet-attach-ebs-volume"
93-
region = var.region
120+
source = "../init-snippet-attach-ebs-volume"
121+
region = var.region
94122
volume_id = module.service-data.volume_id
95123
}
124+
125+
module "init-install-awscli" {
126+
source = "../init-snippet-install-awscli"
127+
}
128+
129+

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 = var.assign_eip ? 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)