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

Commit 683992c

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 c5804f1 commit 683992c

File tree

6 files changed

+108
-8
lines changed

6 files changed

+108
-8
lines changed
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
variable "region" {
2+
description = "The region to put resources in"
3+
default = "us-east-1"
4+
}
5+
6+
variable "az" {
7+
description = "The availability zone to put resources in"
8+
default = "us-east-1c"
9+
}
10+
11+
variable "key_name" {
12+
description = "The keypair used to ssh into the asg intances"
13+
default = "shida-east-1"
14+
}
15+
16+
provider "aws" {
17+
region = var.region
18+
}
19+
20+
module "vpc" {
21+
source = "../../modules/vpc-scenario-1"
22+
azs = [var.az]
23+
name_prefix = "eiptest"
24+
cidr = "192.168.0.0/16"
25+
public_subnet_cidrs = ["192.168.0.0/16"]
26+
region = var.region
27+
map_on_launch = false
28+
}
29+
30+
module "snasg" {
31+
source = "../../modules/single-node-asg"
32+
name_prefix = "unit"
33+
name_suffix = "eiptest"
34+
ami = module.ubuntu-ami.id
35+
instance_type = "t2.micro"
36+
region = var.region
37+
key_name = var.key_name
38+
subnet_id = module.vpc.public_subnet_ids[0]
39+
security_group_ids = [aws_security_group.eiptest.id]
40+
assign_eip = true
41+
}
42+
43+
module "ubuntu-ami" {
44+
source = "../../modules/ami-ubuntu"
45+
release = "16.04"
46+
}
47+
48+
resource "aws_security_group" "eiptest" {
49+
name = "eiptest"
50+
vpc_id = module.vpc.vpc_id
51+
52+
ingress {
53+
from_port = 22
54+
to_port = 22
55+
protocol = "tcp"
56+
cidr_blocks = ["0.0.0.0/0"]
57+
}
58+
59+
egress {
60+
from_port = 0
61+
to_port = 0
62+
protocol = "-1"
63+
cidr_blocks = ["0.0.0.0/0"]
64+
}
65+
}

modules/persistent-ebs/data.tf

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ data "aws_iam_policy_document" "attach_ebs_policy_doc" {
2222
}
2323

2424
resource "aws_iam_policy" "attach_ebs_policy" {
25-
name = "attach_ebs"
26-
25+
name = "attach_ebs"
2726
policy = data.aws_iam_policy_document.attach_ebs_policy_doc.json
2827
}

modules/single-node-asg/main.tf

+28-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*
1212
*/
1313

14+
resource "aws_eip" "eip" {
15+
count = var.assign_eip ? 1 : 0
16+
}
17+
1418
module "service-data" {
1519
source = "../persistent-ebs"
1620
name_prefix = "${var.name_prefix}-${var.name_suffix}-data"
@@ -42,20 +46,21 @@ module "server" {
4246
ami = var.ami
4347
subnet_ids = [var.subnet_id]
4448
azs = [data.aws_subnet.server-subnet.availability_zone]
45-
public_ip = var.public_ip
4649
key_name = var.key_name
4750
elb_names = var.load_balancers
4851
max_nodes = 1
4952
min_nodes = 1
5053
root_volume_type = var.root_volume_type
5154
root_volume_size = var.root_volume_size
52-
53-
iam_profile = module.instance_profile.iam_profile_id
55+
iam_profile = module.instance_profile.iam_profile_id
5456

5557
user_data = <<END_INIT
5658
#!/bin/bash
5759
${var.init_prefix}
60+
apt update
61+
apt install -y awscli
5862
${module.init-attach-ebs.init_snippet}
63+
${var.assign_eip ? "aws ec2 associate-address --instance-id \"$(ec2metadata --instance-id)\" --region \"${var.region}\" --allocation-id \"${element(aws_eip.eip.*.id,0)}\"" : ""}
5964
${var.init_suffix}
6065
END_INIT
6166

@@ -73,3 +78,23 @@ data "aws_subnet" "server-subnet" {
7378
id = var.subnet_id
7479
}
7580

81+
resource "aws_iam_role_policy_attachment" "associate_eip" {
82+
role = module.instance_profile.iam_role_name
83+
policy_arn = aws_iam_policy.associate_eip_policy.arn
84+
}
85+
86+
resource "aws_iam_policy" "associate_eip_policy" {
87+
name = "associate_address"
88+
policy = data.aws_iam_policy_document.associate_eip_policy_doc.json
89+
}
90+
91+
data "aws_iam_policy_document" "associate_eip_policy_doc" {
92+
statement {
93+
sid = ""
94+
effect = "Allow"
95+
actions = [
96+
"ec2:AssociateAddress"
97+
]
98+
resources = ["*"]
99+
}
100+
}

modules/single-node-asg/variables.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -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+
}

modules/vpc-scenario-1/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module "public-subnets" {
3939
name_prefix = "${var.name_prefix}-public"
4040
cidr_blocks = var.public_subnet_cidrs
4141
extra_tags = var.extra_tags
42+
public = var.map_on_launch
4243
}
4344

4445
module "public-gateway" {

modules/vpc-scenario-1/variables.tf

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ variable "extra_tags" {
3232
variable "enable_dns_hostnames" {
3333
default = true
3434
description = "boolean, enable/disable VPC attribute, enable_dns_hostnames"
35-
type = string
35+
type = bool
3636
}
3737

3838
variable "enable_dns_support" {
3939
default = true
4040
description = "boolean, enable/disable VPC attribute, enable_dns_support"
41-
type = string
41+
type = bool
4242
}
4343

4444
variable "dns_servers" {
@@ -47,3 +47,8 @@ variable "dns_servers" {
4747
type = list(string)
4848
}
4949

50+
variable "map_on_launch" {
51+
default = true
52+
description = "Map public subnet addresses to instances."
53+
type = bool
54+
}

0 commit comments

Comments
 (0)