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

Supporting assigning EIP to single node ASG instance. #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions modules/single-node-asg/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ module "service-data" {
iam_instance_profile_role_name = module.instance_profile.iam_role_name
}

resource "aws_eip" "eip" {
count = var.assign_eip ? 1 : 0
vpc = true
}

resource "aws_iam_role_policy_attachment" "associate_eip" {
role = module.instance_profile.iam_role_name
policy_arn = aws_iam_policy.associate_eip_policy.arn
}

resource "aws_iam_policy" "associate_eip_policy" {
name = "associate_address"
policy = data.aws_iam_policy_document.associate_eip_policy_doc.json
}

data "aws_iam_policy_document" "associate_eip_policy_doc" {
statement {
sid = ""
effect = "Allow"
actions = [
"ec2:AssociateAddress"
]
resources = ["*"]
}
}

# Create an ASG with just 1 EC2 instance
module "server" {
source = "../asg"
Expand All @@ -66,12 +92,11 @@ module "server" {
max_nodes = 1
min_nodes = 1
placement_group = var.placement_group
public_ip = var.public_ip
public_ip = var.assign_eip ? false : var.public_ip
# the prefix and suffix names are combined in
# the `asg` module to create the full name
Magicloud marked this conversation as resolved.
Show resolved Hide resolved
name_prefix = var.name_prefix
name_suffix = "${var.name_suffix}-${local.az}"

name_prefix = var.name_prefix
name_suffix = "${var.name_suffix}-${local.az}"
root_volume_type = var.root_volume_type
root_volume_size = var.root_volume_size
security_group_ids = var.security_group_ids
Expand All @@ -84,7 +109,12 @@ module "server" {
# exec > /tmp/init.log
# exec 2> /tmp/init-err.log
# set -x
apt update
${var.init_prefix}
${module.init-install-awscli.init_snippet}
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
sleep 1
done
${module.init-attach-ebs.init_snippet}
${var.init_suffix}
END_INIT
Expand All @@ -97,3 +127,7 @@ module "init-attach-ebs" {
region = var.region
volume_id = module.service-data.volume_id
}

module "init-install-awscli" {
source = "../init-snippet-install-awscli"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this snippet make the module ubuntu-only? or would this still work with centos?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Ubuntu only. I think it won't be hard to support CentOS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Magicloud will we support CentOS?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Magicloud any update on this?

4 changes: 4 additions & 0 deletions modules/single-node-asg/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ output "data_volume_name_tag" {
value = "${local.data_volume_name_prefix}-${local.az}"
description = "Name tag value for attached data volume"
}

output "eip_address" {
value = var.assign_eip ? aws_eip.eip.*[0].public_ip : ""
}
10 changes: 8 additions & 2 deletions modules/single-node-asg/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ variable "data_volume_size" {
variable "data_volume_encrypted" {
default = true
description = "Boolean, whether or not to encrypt the EBS block device"
type = string
type = bool
}

variable "data_volume_kms_key_id" {
Expand Down Expand Up @@ -98,7 +98,7 @@ variable "init_suffix" {
variable "public_ip" {
default = true
description = "Boolean flag to enable/disable `map_public_ip_on_launch` in the launch configuration"
type = string
type = bool
}

variable "subnet_id" {
Expand All @@ -121,3 +121,9 @@ variable "load_balancers" {
description = "The list of load balancers names to pass to the ASG module"
type = list(string)
}

variable "assign_eip" {
default = false
description = "Whether or not associating an EIP with the node."
type = bool
}