|
| 1 | +# Copyright (c) HashiCorp, Inc. |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +terraform { |
| 5 | + required_providers { |
| 6 | + enos = { |
| 7 | + source = "app.terraform.io/hashicorp-qti/enos" |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +data "enos_environment" "current" {} |
| 13 | + |
| 14 | +locals { |
| 15 | + selected_az = data.aws_availability_zones.available.names[random_integer.az.result] |
| 16 | + common_tags = merge( |
| 17 | + var.common_tags, |
| 18 | + { |
| 19 | + Type = var.cluster_tag |
| 20 | + Module = "boundary-worker" |
| 21 | + Pet = random_pet.worker.id |
| 22 | + }, |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +resource "random_pet" "worker" { |
| 27 | + separator = "_" |
| 28 | +} |
| 29 | + |
| 30 | +data "aws_availability_zones" "available" { |
| 31 | + state = "available" |
| 32 | + filter { |
| 33 | + name = "zone-name" |
| 34 | + values = var.availability_zones |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +data "aws_availability_zone" "worker_az" { |
| 39 | + name = local.selected_az |
| 40 | +} |
| 41 | + |
| 42 | +data "aws_kms_key" "kms_key" { |
| 43 | + key_id = var.kms_key_arn |
| 44 | +} |
| 45 | + |
| 46 | +resource "random_integer" "az" { |
| 47 | + min = 0 |
| 48 | + max = length(data.aws_availability_zones.available.names) - 1 |
| 49 | + keepers = { |
| 50 | + # Generate a new integer each time the list of aws_availability_zones changes |
| 51 | + # keepers have to be strings, sort the list in case order changes but zones don't |
| 52 | + listener_arn = join("", sort(data.aws_availability_zones.available.names)) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +# Create a subnet so that the worker doesn't share one with a controller |
| 57 | +resource "aws_subnet" "default" { |
| 58 | + vpc_id = var.vpc_name |
| 59 | + cidr_block = "10.13.9.0/24" |
| 60 | + map_public_ip_on_launch = true |
| 61 | + availability_zone = local.selected_az |
| 62 | + tags = merge( |
| 63 | + local.common_tags, |
| 64 | + { |
| 65 | + "Name" = "${var.vpc_name}_worker_${random_pet.worker.id}_subnet" |
| 66 | + }, |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +# The worker instance is a part of this security group, not to be confused with the next rule below |
| 71 | +resource "aws_security_group" "default" { |
| 72 | + name = "boundary-sg-worker-${random_pet.worker.id}" |
| 73 | + description = "SSH to worker to KMS and controllers" |
| 74 | + vpc_id = var.vpc_name |
| 75 | + |
| 76 | + ingress { |
| 77 | + description = "SSH to the worker instance" |
| 78 | + from_port = 22 |
| 79 | + to_port = 22 |
| 80 | + protocol = "tcp" |
| 81 | + cidr_blocks = ["${data.enos_environment.current.public_ip_address}/32"] |
| 82 | + } |
| 83 | + |
| 84 | + egress { |
| 85 | + description = "Communication from Boundary worker to controller" |
| 86 | + from_port = 9201 |
| 87 | + to_port = 9201 |
| 88 | + protocol = "tcp" |
| 89 | + cidr_blocks = ["0.0.0.0/0"] |
| 90 | + } |
| 91 | + |
| 92 | + egress { |
| 93 | + description = "Communication from Boundary worker to controller" |
| 94 | + from_port = 443 |
| 95 | + to_port = 443 |
| 96 | + protocol = "tcp" |
| 97 | + cidr_blocks = ["0.0.0.0/0"] |
| 98 | + } |
| 99 | + |
| 100 | + tags = merge( |
| 101 | + local.common_tags, |
| 102 | + { |
| 103 | + "Name" = "${var.vpc_name}_worker_sg" |
| 104 | + }, |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +# This module only manages a rule for the controller SG that is vital to the worker's operation |
| 109 | +# This module does _not_ manage the security group itself |
| 110 | +resource "aws_vpc_security_group_ingress_rule" "worker_to_controller" { |
| 111 | + description = "This rule allows traffic from a worker to controllers over WAN" |
| 112 | + security_group_id = var.controller_sg_id |
| 113 | + cidr_ipv4 = "${aws_instance.worker.public_ip}/32" |
| 114 | + from_port = 9201 |
| 115 | + to_port = 9201 |
| 116 | + ip_protocol = "tcp" |
| 117 | +} |
| 118 | + |
| 119 | +data "aws_route_table" "default" { |
| 120 | + vpc_id = var.vpc_name |
| 121 | + |
| 122 | + filter { |
| 123 | + name = "tag:Name" |
| 124 | + values = ["enos-vpc_route"] |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +resource "aws_route_table_association" "worker_rta" { |
| 129 | + subnet_id = aws_subnet.default.id |
| 130 | + route_table_id = data.aws_route_table.default.id |
| 131 | +} |
| 132 | + |
| 133 | +resource "aws_instance" "worker" { |
| 134 | + ami = var.ubuntu_ami_id |
| 135 | + instance_type = var.worker_instance_type |
| 136 | + vpc_security_group_ids = [aws_security_group.default.id] |
| 137 | + subnet_id = aws_subnet.default.id |
| 138 | + key_name = var.ssh_aws_keypair |
| 139 | + iam_instance_profile = var.iam_instance_profile_name |
| 140 | + monitoring = var.worker_monitoring |
| 141 | + |
| 142 | + root_block_device { |
| 143 | + iops = var.ebs_iops |
| 144 | + volume_size = var.ebs_size |
| 145 | + volume_type = var.ebs_type |
| 146 | + throughput = var.ebs_throughput |
| 147 | + tags = local.common_tags |
| 148 | + } |
| 149 | + |
| 150 | + tags = merge( |
| 151 | + local.common_tags, |
| 152 | + { |
| 153 | + Name = "${var.name_prefix}-boundary-worker", |
| 154 | + }, |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +resource "enos_bundle_install" "worker" { |
| 159 | + depends_on = [aws_instance.worker, aws_route_table_association.worker_rta] |
| 160 | + |
| 161 | + destination = var.boundary_install_dir |
| 162 | + artifactory = var.boundary_artifactory_release |
| 163 | + path = var.local_artifact_path |
| 164 | + release = var.boundary_release == null ? var.boundary_release : merge(var.boundary_release, { product = "boundary", edition = "oss" }) |
| 165 | + |
| 166 | + transport = { |
| 167 | + ssh = { |
| 168 | + host = aws_instance.worker.public_ip |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +resource "enos_file" "worker_config" { |
| 174 | + depends_on = [enos_bundle_install.worker] |
| 175 | + |
| 176 | + destination = "/etc/boundary/boundary.hcl" |
| 177 | + content = templatefile("${path.module}/templates/worker.hcl", { |
| 178 | + id = random_pet.worker.id |
| 179 | + kms_key_id = data.aws_kms_key.kms_key.id |
| 180 | + public_addr = aws_instance.worker.public_ip |
| 181 | + type = jsonencode(var.worker_type_tags) |
| 182 | + region = data.aws_availability_zone.worker_az.region |
| 183 | + controller_addresses = jsonencode(var.controller_addresses) |
| 184 | + }) |
| 185 | + |
| 186 | + transport = { |
| 187 | + ssh = { |
| 188 | + host = aws_instance.worker.public_ip |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +resource "enos_boundary_start" "worker_start" { |
| 194 | + depends_on = [ |
| 195 | + enos_file.worker_config, |
| 196 | + aws_vpc_security_group_ingress_rule.worker_to_controller, |
| 197 | + ] |
| 198 | + |
| 199 | + bin_path = "/opt/boundary/bin" |
| 200 | + config_path = "/etc/boundary" |
| 201 | + transport = { |
| 202 | + ssh = { |
| 203 | + host = aws_instance.worker.public_ip |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments