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

Commit 5d2abcd

Browse files
authored
Remove EIP allocation (#29)
1 parent 06798e0 commit 5d2abcd

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

‎README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ module "nat" {
4141
private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks
4242
private_route_table_ids = module.vpc.private_route_table_ids
4343
}
44+
45+
resource "aws_eip" "nat" {
46+
network_interface = module.nat.eni_id
47+
tags = {
48+
"Name" = "nat-instance-main"
49+
}
50+
}
4451
```
4552

4653
Now create an EC2 instance in the private subnet to verify the NAT configuration.
@@ -55,12 +62,13 @@ This module provisions the following resources:
5562

5663
- Auto Scaling Group with mixed instances policy
5764
- Launch Template
58-
- Elastic IP
5965
- Elastic Network Interface
6066
- Security Group
6167
- IAM Role for SSM and ENI attachment
6268
- VPC Route (optional)
6369

70+
You need to attach your elastic IP to the ENI.
71+
6472
Take a look at the diagram:
6573

6674
![diagram](diagram.svg)
@@ -119,6 +127,24 @@ resource "aws_security_group_rule" "nat_ssh" {
119127
```
120128

121129

130+
## Migration guide
131+
132+
### Upgrade to v2 from v1
133+
134+
This module no longer creates an EIP since v2.
135+
136+
To keep your EIP when you migrate to module v2, rename the EIP in the state as follows:
137+
138+
```console
139+
% terraform state mv -dry-run module.nat.aws_eip.this aws_eip.nat
140+
Would move "module.nat.aws_eip.this" to "aws_eip.nat"
141+
142+
% terraform state mv module.nat.aws_eip.this aws_eip.nat
143+
Move "module.nat.aws_eip.this" to "aws_eip.nat"
144+
Successfully moved 1 object(s).
145+
```
146+
147+
122148
## Contributions
123149

124150
This is an open source software. Feel free to open issues and pull requests.
@@ -152,14 +178,11 @@ No requirements.
152178
| user\_data\_runcmd | Additional runcmd section of cloud-init | `list` | `[]` | no |
153179
| user\_data\_write\_files | Additional write\_files section of cloud-init | `list` | `[]` | no |
154180
| vpc\_id | ID of the VPC | `string` | n/a | yes |
155-
| eip_creation | Whether to create an eip | `bool` | `true` | no |
156181

157182
## Outputs
158183

159184
| Name | Description |
160185
|------|-------------|
161-
| eip\_id | ID of the Elastic IP |
162-
| eip\_public\_ip | Public IP of the Elastic IP for the NAT instance |
163186
| eni\_id | ID of the ENI for the NAT instance |
164187
| eni\_private\_ip | Private IP of the ENI for the NAT instance |
165188
| iam\_role\_name | Name of the IAM role for the NAT instance |

‎example/example.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ module "nat" {
4141
]
4242
}
4343

44+
resource "aws_eip" "nat" {
45+
network_interface = module.nat.eni_id
46+
tags = {
47+
"Name" = "nat-instance-example"
48+
}
49+
}
50+
4451
# IAM policy for port forwarding (optional)
4552
resource "aws_iam_role_policy" "dnat_service" {
4653
role = module.nat.iam_role_name
@@ -72,5 +79,5 @@ resource "aws_security_group_rule" "dnat_http" {
7279
}
7380

7481
output "nat_public_ip" {
75-
value = module.nat.eip_public_ip
82+
value = aws_eip.nat.public_ip
7683
}

‎main.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ resource "aws_network_interface" "this" {
3131
tags = local.common_tags
3232
}
3333

34-
resource "aws_eip" "this" {
35-
count = var.enabled ? var.eip_creation ? 1 : 0 : 0
36-
network_interface = aws_network_interface.this.id
37-
tags = local.common_tags
38-
}
39-
4034
resource "aws_route" "this" {
4135
count = length(var.private_route_table_ids)
4236
route_table_id = var.private_route_table_ids[count.index]

‎outputs.tf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
output "eip_id" {
2-
description = "ID of the Elastic IP"
3-
value = var.enabled ? var.eip_creation ? aws_eip.this[0].id : "" : ""
4-
}
5-
6-
output "eip_public_ip" {
7-
description = "Public IP of the Elastic IP for the NAT instance"
8-
value = var.enabled ? var.eip_creation ? aws_eip.this[0].public_ip : "" : ""
9-
}
10-
111
output "eni_id" {
122
description = "ID of the ENI for the NAT instance"
133
value = aws_network_interface.this.id

‎variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ variable "user_data_runcmd" {
7171
type = list
7272
default = []
7373
}
74-
variable "eip_creation" {
75-
description = "Whether to create an elastic ip"
76-
type = bool
77-
default = true
78-
}
7974

8075
locals {
8176
// Generate common tags by merging variables and default Name

0 commit comments

Comments
 (0)