Skip to content

Commit ef49812

Browse files
authored
EIP module refactoring (#81)
* gateway and instance now use same EIP module * formatting code * readme rebuild
1 parent af10fd2 commit ef49812

File tree

7 files changed

+29
-32
lines changed

7 files changed

+29
-32
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Terraform module to provision public and private [`subnets`](http://docs.aws.ama
5050
__Note:__ this module is intended for use with an existing VPC and existing Internet Gateway.
5151
To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module.
5252

53+
__Note 2:__ EIP module first checks if `gateway` mode enabled, so takes precedence other `instance` in case both enabled (which is bad idea and won't work anyway)
54+
5355

5456
---
5557

README.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ description: |-
6666
__Note:__ this module is intended for use with an existing VPC and existing Internet Gateway.
6767
To create a new VPC, use [terraform-aws-vpc](https://github.com/cloudposse/terraform-aws-vpc) module.
6868
69+
__Note 2:__ EIP module first checks if `gateway` mode enabled, so takes precedence other `instance` in case both enabled (which is bad idea and won't work anyway)
70+
6971
# How to use this project
7072
usage: |-
7173
```hcl

nat-eip.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
locals {
2+
nat_eip_count = "${local.nat_gateways_count > 0 ? local.nat_gateways_count : (local.nat_instance_count > 0 ? local.nat_instance_count : 0)}"
3+
}
4+
5+
resource "aws_eip" "default" {
6+
count = "${local.nat_eip_count}"
7+
vpc = true
8+
tags = "${merge(module.private_label.tags, map("Name", format("%s%s%s", module.private_label.id, var.delimiter, replace(element(var.availability_zones, count.index), "-", var.delimiter))))}"
9+
10+
lifecycle {
11+
create_before_destroy = true
12+
}
13+
}

nat-gateway.tf

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
module "nat_label" {
22
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.11.1"
33
context = "${module.label.context}"
4-
attributes = "${distinct(compact(concat(module.label.attributes,list("nat"))))}"
4+
attributes = "${distinct(compact(concat(module.label.attributes, list("nat"))))}"
55
}
66

77
locals {
88
nat_gateways_count = "${var.nat_gateway_enabled == "true" ? length(var.availability_zones) : 0}"
99
}
1010

11-
resource "aws_eip" "default" {
12-
count = "${local.nat_gateways_count}"
13-
vpc = true
14-
tags = "${merge(module.private_label.tags, map("Name",format("%s%s%s", module.private_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
15-
16-
lifecycle {
17-
create_before_destroy = true
18-
}
19-
}
20-
2111
resource "aws_nat_gateway" "default" {
2212
count = "${local.nat_gateways_count}"
2313
allocation_id = "${element(aws_eip.default.*.id, count.index)}"
2414
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
25-
tags = "${merge(module.nat_label.tags, map("Name",format("%s%s%s", module.nat_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
15+
tags = "${merge(module.nat_label.tags, map("Name", format("%s%s%s", module.nat_label.id, var.delimiter, replace(element(var.availability_zones, count.index), "-", var.delimiter))))}"
2616

2717
lifecycle {
2818
create_before_destroy = true

nat-instance.tf

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module "nat_instance_label" {
22
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.11.1"
33
context = "${module.label.context}"
4-
attributes = "${distinct(compact(concat(module.label.attributes,list("nat", "instance"))))}"
4+
attributes = "${distinct(compact(concat(module.label.attributes, list("nat", "instance"))))}"
55
}
66

77
locals {
@@ -67,7 +67,7 @@ resource "aws_instance" "nat_instance" {
6767
instance_type = "${var.nat_instance_type}"
6868
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
6969
vpc_security_group_ids = ["${aws_security_group.nat_instance.id}"]
70-
tags = "${merge(module.nat_instance_label.tags, map("Name",format("%s%s%s", module.nat_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
70+
tags = "${merge(module.nat_instance_label.tags, map("Name", format("%s%s%s", module.nat_label.id, var.delimiter, replace(element(var.availability_zones, count.index), "-", var.delimiter))))}"
7171

7272
# Required by NAT
7373
# https://docs.aws.amazon.com/vpc/latest/userguide/VPC_NAT_Instance.html#EIP_Disable_SrcDestCheck
@@ -80,20 +80,10 @@ resource "aws_instance" "nat_instance" {
8080
}
8181
}
8282

83-
resource "aws_eip" "nat_instance" {
84-
count = "${local.nat_instance_count}"
85-
vpc = true
86-
tags = "${merge(module.nat_instance_label.tags, map("Name",format("%s%s%s", module.nat_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
87-
88-
lifecycle {
89-
create_before_destroy = true
90-
}
91-
}
92-
9383
resource "aws_eip_association" "nat_instance" {
9484
count = "${local.nat_instance_count}"
9585
instance_id = "${element(aws_instance.nat_instance.*.id, count.index)}"
96-
allocation_id = "${element(aws_eip.nat_instance.*.id, count.index)}"
86+
allocation_id = "${element(aws_eip.default.*.id, count.index)}"
9787
}
9888

9989
resource "aws_route" "nat_instance" {

private.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module "private_label" {
22
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.11.1"
33
context = "${module.label.context}"
4-
attributes = "${compact(concat(module.label.attributes,list("private")))}"
5-
tags = "${merge(module.label.tags, map(var.subnet_type_tag_key, format(var.subnet_type_tag_value_format,"private")))}"
4+
attributes = "${compact(concat(module.label.attributes, list("private")))}"
5+
tags = "${merge(module.label.tags, map(var.subnet_type_tag_key, format(var.subnet_type_tag_value_format, "private")))}"
66
}
77

88
locals {
@@ -15,7 +15,7 @@ resource "aws_subnet" "private" {
1515
availability_zone = "${element(var.availability_zones, count.index)}"
1616
cidr_block = "${cidrsubnet(signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block, ceil(log(local.private_subnet_count * 2, 2)), count.index)}"
1717

18-
tags = "${merge(module.private_label.tags, map("Name",format("%s%s%s", module.private_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
18+
tags = "${merge(module.private_label.tags, map("Name", format("%s%s%s", module.private_label.id, var.delimiter, replace(element(var.availability_zones, count.index), "-", var.delimiter))))}"
1919

2020
lifecycle {
2121
# Ignore tags added by kops or kubernetes
@@ -27,7 +27,7 @@ resource "aws_route_table" "private" {
2727
count = "${length(var.availability_zones)}"
2828
vpc_id = "${data.aws_vpc.default.id}"
2929

30-
tags = "${merge(module.private_label.tags, map("Name",format("%s%s%s", module.private_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
30+
tags = "${merge(module.private_label.tags, map("Name", format("%s%s%s", module.private_label.id, var.delimiter, replace(element(var.availability_zones, count.index), "-", var.delimiter))))}"
3131
}
3232

3333
resource "aws_route_table_association" "private" {

public.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module "public_label" {
22
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.11.1"
33
context = "${module.label.context}"
4-
attributes = "${compact(concat(module.label.attributes,list("public")))}"
5-
tags = "${merge(module.label.tags, map(var.subnet_type_tag_key, format(var.subnet_type_tag_value_format,"public")))}"
4+
attributes = "${compact(concat(module.label.attributes, list("public")))}"
5+
tags = "${merge(module.label.tags, map(var.subnet_type_tag_key, format(var.subnet_type_tag_value_format, "public")))}"
66
}
77

88
locals {
@@ -16,7 +16,7 @@ resource "aws_subnet" "public" {
1616
availability_zone = "${element(var.availability_zones, count.index)}"
1717
cidr_block = "${cidrsubnet(signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block, ceil(log(local.public_subnet_count * 2, 2)), local.public_subnet_count + count.index)}"
1818
map_public_ip_on_launch = "${local.map_public_ip_on_launch}"
19-
tags = "${merge(module.public_label.tags, map("Name",format("%s%s%s", module.public_label.id, var.delimiter, replace(element(var.availability_zones, count.index),"-",var.delimiter))))}"
19+
tags = "${merge(module.public_label.tags, map("Name", format("%s%s%s", module.public_label.id, var.delimiter, replace(element(var.availability_zones, count.index), "-", var.delimiter))))}"
2020

2121
lifecycle {
2222
# Ignore tags added by kops or kubernetes

0 commit comments

Comments
 (0)