Skip to content

Commit

Permalink
fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
mipmip committed Oct 31, 2023
1 parent 0008213 commit 7a18070
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 19 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ Any subnet.subnet_group must refer to an existing key in the subnet_groups map.
Use networkaddress_bits to define the amount of addresses provisioned for the
subnet. This number must be higher than 3.

## Troubleshooting

### Error when creating cheap nat instances.

There is a dependency problem when createing cheap nat instances.

**Solution** First enable NAT in network.json, `terraform apply`, then set
`use_nat_instances = true` and rerun `terraform apply`.

## Future work / ideas

Possibility of automatically adding subnet groups when new availability zones
Expand Down
9 changes: 6 additions & 3 deletions nat.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_eip" "this" {
for_each = { for key in local.availability_zones_with_nat : key => key }
for_each = { for key in local.availability_zones_with_nat : key => key if !var.use_nat_instances }

#vpc = true # dont support EC2-ClassicLink but otherwise TF keeps reacreating this resource
domain = "vpc"
Expand Down Expand Up @@ -32,17 +32,20 @@ resource "aws_nat_gateway" "this" {
module "nat_instances" {
source = "./nat_instances"

for_each = { for k, v in aws_eip.this : k => v if var.use_nat_instances }
for_each = { for key in local.availability_zones_with_nat : key => key if var.use_nat_instances}

name = "${var.name}-${each.key}"
kms_key_arn = var.kms_key_arn
# fetch first public subnet to locate nat gateway in in the same az zone, if not available then crash
public_subnet_id = element([for key, value in aws_subnet.this : value if value.availability_zone == each.key && !var.configuration.subnet_groups[var.configuration.subnets[key].subnet_group].nat_gateway && var.configuration.subnet_groups[var.configuration.subnets[key].subnet_group].internet_gateway], 0).id
elastic_ip_id = each.value.id
sqs_dlq_arn = var.sqs_dlq_arn
route_nat_gateway_tag_name = local.route_nat_gateway_tag_name

nat_route_table_arns = [for k, v in aws_route_table.this : v.arn if v.tags[local.route_nat_gateway_tag_name] == "true"]

depends_on = [
aws_internet_gateway.this
]
}

resource "aws_route" "nat_gateway" {
Expand Down
2 changes: 0 additions & 2 deletions nat_instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module "ec2_asg" {
initial_amount_of_pods = 1

name = var.name
#initial_amount_of_pods = 1

ec2_ami_name_filter_list = ["amzn2-ami-ecs-hvm-2.0.*-arm64-ebs"]
ec2_ami_owner_list = ["591542846629"] # Amazon
Expand All @@ -21,7 +20,6 @@ module "ec2_asg" {
subnet_ids = [var.public_subnet_id]
use_public_ip = false # we are the NAT gateway
use_floating_ip = true
own_eip_for_floaing_ip = var.elastic_ip_id
user_data_completion_hook = true
sqs_dlq_arn = var.sqs_dlq_arn
kms_key_arn = var.kms_key_arn
Expand Down
5 changes: 0 additions & 5 deletions nat_instances/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ variable "public_subnet_id" {
type = string
}

variable "elastic_ip_id" {
description = "Elastic IP"
type = string
}

variable "sqs_dlq_arn" {
description = "SQS DLQ Arn to transfer unprocessed / failed infra messages into."
type = string
Expand Down

0 comments on commit 7a18070

Please sign in to comment.