|
| 1 | +variable "project_name" { |
| 2 | + type = string |
| 3 | + default = "red-instance-existing-net" |
| 4 | +} |
| 5 | + |
| 6 | +variable "region" { |
| 7 | + type = string |
| 8 | + default = "us-east-1" |
| 9 | +} |
| 10 | + |
| 11 | +variable "instance_name" { |
| 12 | + type = string |
| 13 | + default = "red-existing" |
| 14 | +} |
| 15 | + |
| 16 | +provider "aws" { |
| 17 | + region = var.region |
| 18 | +} |
| 19 | + |
| 20 | +# A minimal network stood up in the example itself, standing in for any |
| 21 | +# externally-provided VPC (red-network, an existing corporate VPC, etc.). |
| 22 | +# This exercises red-instance's create_vpc = false path. |
| 23 | +resource "aws_vpc" "this" { |
| 24 | + cidr_block = "10.20.0.0/16" |
| 25 | + enable_dns_support = true |
| 26 | + enable_dns_hostnames = true |
| 27 | + tags = { Name = "${var.project_name}-vpc" } |
| 28 | +} |
| 29 | + |
| 30 | +resource "aws_internet_gateway" "this" { |
| 31 | + vpc_id = aws_vpc.this.id |
| 32 | + tags = { Name = "${var.project_name}-igw" } |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_subnet" "public" { |
| 36 | + vpc_id = aws_vpc.this.id |
| 37 | + cidr_block = "10.20.1.0/24" |
| 38 | + map_public_ip_on_launch = true |
| 39 | + availability_zone = "${var.region}a" |
| 40 | + tags = { Name = "${var.project_name}-public" } |
| 41 | +} |
| 42 | + |
| 43 | +resource "aws_route_table" "public" { |
| 44 | + vpc_id = aws_vpc.this.id |
| 45 | + route { |
| 46 | + cidr_block = "0.0.0.0/0" |
| 47 | + gateway_id = aws_internet_gateway.this.id |
| 48 | + } |
| 49 | + tags = { Name = "${var.project_name}-public-rt" } |
| 50 | +} |
| 51 | + |
| 52 | +resource "aws_route_table_association" "public" { |
| 53 | + subnet_id = aws_subnet.public.id |
| 54 | + route_table_id = aws_route_table.public.id |
| 55 | +} |
| 56 | + |
| 57 | +module "red_instance" { |
| 58 | + source = "../../" |
| 59 | + |
| 60 | + project_name = var.project_name |
| 61 | + instance_name = var.instance_name |
| 62 | + |
| 63 | + create_vpc = false |
| 64 | + vpc_id = aws_vpc.this.id |
| 65 | + subnet_id = aws_subnet.public.id |
| 66 | + |
| 67 | + ingress_rules = [ |
| 68 | + { |
| 69 | + description = "HTTPS from anywhere" |
| 70 | + from_port = 443 |
| 71 | + to_port = 443 |
| 72 | + protocol = "tcp" |
| 73 | + cidr_blocks = ["0.0.0.0/0"] |
| 74 | + } |
| 75 | + ] |
| 76 | + |
| 77 | + additional_tags = { |
| 78 | + Environment = "example" |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +output "instance_id" { |
| 83 | + value = module.red_instance.instance_id |
| 84 | +} |
| 85 | + |
| 86 | +output "public_ip" { |
| 87 | + value = module.red_instance.public_ip |
| 88 | +} |
| 89 | + |
| 90 | +output "vpc_id" { |
| 91 | + description = "VPC ID — should equal the externally-created VPC, not a module-created one." |
| 92 | + value = module.red_instance.vpc_id |
| 93 | +} |
| 94 | + |
| 95 | +output "supplied_vpc_id" { |
| 96 | + description = "The externally-created VPC ID, for cross-checking against the module output." |
| 97 | + value = aws_vpc.this.id |
| 98 | +} |
0 commit comments