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

Remove EIP allocation #29

Merged
merged 1 commit into from
Sep 19, 2020
Merged
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
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ module "nat" {
private_subnets_cidr_blocks = module.vpc.private_subnets_cidr_blocks
private_route_table_ids = module.vpc.private_route_table_ids
}

resource "aws_eip" "nat" {
network_interface = module.nat.eni_id
tags = {
"Name" = "nat-instance-main"
}
}
```

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

- Auto Scaling Group with mixed instances policy
- Launch Template
- Elastic IP
- Elastic Network Interface
- Security Group
- IAM Role for SSM and ENI attachment
- VPC Route (optional)

You need to attach your elastic IP to the ENI.

Take a look at the diagram:

![diagram](diagram.svg)
Expand Down Expand Up @@ -119,6 +127,24 @@ resource "aws_security_group_rule" "nat_ssh" {
```


## Migration guide

### Upgrade to v2 from v1

This module no longer creates an EIP since v2.

To keep your EIP when you migrate to module v2, rename the EIP in the state as follows:

```console
% terraform state mv -dry-run module.nat.aws_eip.this aws_eip.nat
Would move "module.nat.aws_eip.this" to "aws_eip.nat"

% terraform state mv module.nat.aws_eip.this aws_eip.nat
Move "module.nat.aws_eip.this" to "aws_eip.nat"
Successfully moved 1 object(s).
```


## Contributions

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

## Outputs

| Name | Description |
|------|-------------|
| eip\_id | ID of the Elastic IP |
| eip\_public\_ip | Public IP of the Elastic IP for the NAT instance |
| eni\_id | ID of the ENI for the NAT instance |
| eni\_private\_ip | Private IP of the ENI for the NAT instance |
| iam\_role\_name | Name of the IAM role for the NAT instance |
Expand Down
9 changes: 8 additions & 1 deletion example/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ module "nat" {
]
}

resource "aws_eip" "nat" {
network_interface = module.nat.eni_id
tags = {
"Name" = "nat-instance-example"
}
}

# IAM policy for port forwarding (optional)
resource "aws_iam_role_policy" "dnat_service" {
role = module.nat.iam_role_name
Expand Down Expand Up @@ -72,5 +79,5 @@ resource "aws_security_group_rule" "dnat_http" {
}

output "nat_public_ip" {
value = module.nat.eip_public_ip
value = aws_eip.nat.public_ip
}
6 changes: 0 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ resource "aws_network_interface" "this" {
tags = local.common_tags
}

resource "aws_eip" "this" {
count = var.enabled ? var.eip_creation ? 1 : 0 : 0
network_interface = aws_network_interface.this.id
tags = local.common_tags
}

resource "aws_route" "this" {
count = length(var.private_route_table_ids)
route_table_id = var.private_route_table_ids[count.index]
Expand Down
10 changes: 0 additions & 10 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
output "eip_id" {
description = "ID of the Elastic IP"
value = var.enabled ? var.eip_creation ? aws_eip.this[0].id : "" : ""
}

output "eip_public_ip" {
description = "Public IP of the Elastic IP for the NAT instance"
value = var.enabled ? var.eip_creation ? aws_eip.this[0].public_ip : "" : ""
}

output "eni_id" {
description = "ID of the ENI for the NAT instance"
value = aws_network_interface.this.id
Expand Down
5 changes: 0 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ variable "user_data_runcmd" {
type = list
default = []
}
variable "eip_creation" {
description = "Whether to create an elastic ip"
type = bool
default = true
}

locals {
// Generate common tags by merging variables and default Name
Expand Down