From 04f173a7633075f5ba4d3c6138cc9d854ad9e5ce Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Mon, 21 Aug 2023 10:14:25 +0100 Subject: [PATCH 1/2] Add support for multiple subnets --- main.tf | 32 ++++++++++++-------------------- variables.tf | 12 ++++++------ 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/main.tf b/main.tf index 3c6cfac..7f3a7e7 100644 --- a/main.tf +++ b/main.tf @@ -14,7 +14,7 @@ locals { resource "aws_vpc" "vpc" { count = var.create ? 1 : 0 - cidr_block = "${var.vpc-cidr-prefix}.0.0/16" + cidr_block = var.cidr-block tags = merge(var.tags, { Name = "depot-connection-${var.connection-id}" }) } @@ -38,17 +38,17 @@ resource "aws_route" "public-internet-gateway" { } resource "aws_subnet" "public" { - count = var.create ? 1 : 0 + count = var.create ? length(var.subnets) : 0 vpc_id = aws_vpc.vpc[0].id - availability_zone = var.availability-zone - cidr_block = "${var.vpc-cidr-prefix}.0.0/16" + availability_zone = var.subnets[count.index].availability-zone + cidr_block = var.subnets[count.index].cidr-block map_public_ip_on_launch = true - tags = merge(var.tags, { "Name" = "depot-connection-${var.connection-id}" }) + tags = merge(var.tags, { "Name" = "depot-${var.connection-id}-${var.subnets[count.index].availability-zone}" }) } resource "aws_route_table_association" "public" { - count = var.create ? 1 : 0 - subnet_id = aws_subnet.public[0].id + count = var.create ? length(var.subnets) : 0 + subnet_id = aws_subnet.public[count.index].id route_table_id = aws_route_table.public[0].id } @@ -170,10 +170,6 @@ resource "aws_launch_template" "x86" { subnet_id = aws_subnet.public[0].id } - placement { - availability_zone = var.availability-zone - } - tag_specifications { resource_type = "instance" tags = var.tags @@ -211,10 +207,6 @@ resource "aws_launch_template" "arm" { subnet_id = aws_subnet.public[0].id } - placement { - availability_zone = var.availability-zone - } - tag_specifications { resource_type = "instance" tags = var.tags @@ -311,16 +303,15 @@ resource "aws_iam_role" "cloud-agent" { { Action = ["ec2:RunInstances"] Effect = "Allow" - Resource = [ + Resource = concat([ aws_launch_template.arm[0].arn, aws_launch_template.x86[0].arn, aws_security_group.instance-buildkit[0].arn, aws_security_group.instance-default[0].arn, - aws_subnet.public[0].arn, "arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:network-interface/*", "arn:aws:ec2:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:volume/*", "arn:aws:ec2:${data.aws_region.current.name}::image/*", - ] + ], [for s in aws_subnet.public : s.arn]) }, { @@ -413,12 +404,13 @@ resource "aws_ecs_task_definition" "cloud-agent" { essential = true environment = concat( [ - { name = "CLOUD_AGENT_AWS_AVAILABILITY_ZONE", value = var.availability-zone }, + { name = "CLOUD_AGENT_AWS_AVAILABILITY_ZONE", value = var.subnets[0].availability-zone }, { name = "CLOUD_AGENT_AWS_LAUNCH_TEMPLATE_ARM", value = aws_launch_template.arm[0].id }, { name = "CLOUD_AGENT_AWS_LAUNCH_TEMPLATE_X86", value = aws_launch_template.x86[0].id }, { name = "CLOUD_AGENT_AWS_SG_BUILDKIT", value = aws_security_group.instance-buildkit[0].id }, { name = "CLOUD_AGENT_AWS_SG_DEFAULT", value = aws_security_group.instance-default[0].id }, { name = "CLOUD_AGENT_AWS_SUBNET_ID", value = aws_subnet.public[0].id }, + { name = "CLOUD_AGENT_AWS_SUBNETS", value = jsonencode(aws_subnet.public) }, { name = "CLOUD_AGENT_CLUSTER_ARN", value = aws_ecs_cluster.cloud-agent[0].arn }, { name = "CLOUD_AGENT_CONNECTION_ID", value = var.connection-id }, { name = "CLOUD_AGENT_SERVICE_NAME", value = local.service-name }, @@ -456,7 +448,7 @@ resource "aws_ecs_service" "cloud-agent" { network_configuration { security_groups = [aws_security_group.cloud-agent[0].id] - subnets = [aws_subnet.public[0].id] + subnets = [for s in aws_subnet.public : s.id] assign_public_ip = true } diff --git a/variables.tf b/variables.tf index 95b2685..6cbd96d 100644 --- a/variables.tf +++ b/variables.tf @@ -11,9 +11,9 @@ variable "connection-token" { sensitive = true } -variable "availability-zone" { - type = string - description = "Availability zone to use for the builder instances" +variable "subnets" { + type = list(object({ availability-zone = string, cidr-block = string })) + description = "Subnets to use for the VPC" } // Optional @@ -42,10 +42,10 @@ variable "instance-types" { default = { x86 = "c6i.xlarge", arm = "c6g.xlarge" } } -variable "vpc-cidr-prefix" { +variable "cidr-block" { type = string - description = "VPC CIDR prefix" - default = "10.0" + description = "VPC CIDR block" + default = "10.0.0.0/16" } variable "allow-ssm-access" { From dcf3519bd69a4aee7f0f5ef49d34b2ad6a568035 Mon Sep 17 00:00:00 2001 From: Jacob Gillespie Date: Mon, 21 Aug 2023 10:26:39 +0100 Subject: [PATCH 2/2] Fix instance tags --- main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index 7f3a7e7..161a07c 100644 --- a/main.tf +++ b/main.tf @@ -172,7 +172,7 @@ resource "aws_launch_template" "x86" { tag_specifications { resource_type = "instance" - tags = var.tags + tags = merge(var.tags, { "depot-connection" = var.connection-id }) } tag_specifications { @@ -209,7 +209,7 @@ resource "aws_launch_template" "arm" { tag_specifications { resource_type = "instance" - tags = var.tags + tags = merge(var.tags, { "depot-connection" = var.connection-id }) } tag_specifications {