|
| 1 | +################################################################################ |
| 2 | +# Provider Configuration |
| 3 | +################################################################################ |
| 4 | + |
| 5 | +provider "aws" { |
| 6 | + region = local.region |
| 7 | +} |
| 8 | + |
| 9 | +data "aws_availability_zones" "available" {} |
| 10 | + |
| 11 | +################################################################################ |
| 12 | +# Locals |
| 13 | +################################################################################ |
| 14 | + |
| 15 | +locals { |
| 16 | + name = "ex-${basename(path.cwd)}" |
| 17 | + region = "us-east-1" |
| 18 | + |
| 19 | + # Get the first 2 availability zones for simplicity |
| 20 | + azs = slice(data.aws_availability_zones.available.names, 0, 2) |
| 21 | + |
| 22 | + # Base VPC CIDR |
| 23 | + vpc_cidr = "10.0.0.0/16" |
| 24 | + |
| 25 | + # Calculate subnet CIDRs automatically based on AZ count |
| 26 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)] |
| 27 | + public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k + 10)] |
| 28 | + |
| 29 | + tags = { |
| 30 | + Example = local.name |
| 31 | + GithubRepo = "terraform-aws-vpc" |
| 32 | + GithubOrg = "terraform-aws-modules" |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +################################################################################ |
| 37 | +# VPC Module |
| 38 | +################################################################################ |
| 39 | + |
| 40 | +module "vpc" { |
| 41 | + source = "../.." |
| 42 | + |
| 43 | + name = local.name |
| 44 | + cidr = local.vpc_cidr |
| 45 | + |
| 46 | + azs = local.azs |
| 47 | + private_subnets = local.private_subnets |
| 48 | + public_subnets = local.public_subnets |
| 49 | + |
| 50 | + enable_nat_gateway = true |
| 51 | + single_nat_gateway = true |
| 52 | + |
| 53 | + # EC2 Instance Connect Endpoint Configuration |
| 54 | + create_instance_connect_endpoint = true |
| 55 | + instance_connect_preserve_client_ip = true |
| 56 | + instance_connect_security_group_ids = [aws_security_group.allow_ssh.id] |
| 57 | + instance_connect_tags = { |
| 58 | + Environment = "example" |
| 59 | + } |
| 60 | + |
| 61 | + tags = merge(local.tags, { |
| 62 | + Name = local.name |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +################################################################################ |
| 67 | +# Security Group for EC2 Instance Connect |
| 68 | +################################################################################ |
| 69 | + |
| 70 | +resource "aws_security_group" "allow_ssh" { |
| 71 | + name = "${local.name}-allow-ssh" |
| 72 | + description = "Allow SSH for EC2 Instance Connect" |
| 73 | + vpc_id = module.vpc.vpc_id |
| 74 | + |
| 75 | + ingress { |
| 76 | + from_port = 22 |
| 77 | + to_port = 22 |
| 78 | + protocol = "tcp" |
| 79 | + cidr_blocks = ["0.0.0.0/0"] |
| 80 | + } |
| 81 | + |
| 82 | + egress { |
| 83 | + from_port = 0 |
| 84 | + to_port = 0 |
| 85 | + protocol = "-1" |
| 86 | + cidr_blocks = ["0.0.0.0/0"] |
| 87 | + } |
| 88 | + |
| 89 | + tags = merge(local.tags, { |
| 90 | + Name = "${local.name}-allow-ssh" |
| 91 | + }) |
| 92 | +} |
0 commit comments