# Some instance variables locals { ami_selection = "minion" # master, minion, ... } # Rather than pass in the aws security group, we just look it up. This will # probably be useful other places, as well. data "aws_security_group" "typical-host" { name = "typical-host" vpc_id = var.vpc_id } # Use the default EBS key data "aws_kms_key" "ebs-key" { key_id = "alias/ebs_root_encrypt_decrypt" } resource "aws_network_interface" "instance" { for_each = toset(var.instance_count) subnet_id = var.subnets[each.value - 1] security_groups = [data.aws_security_group.typical-host.id, aws_security_group.instance_security_group.id] description = "${var.instance_name}-${each.value}" tags = merge(local.standard_tags, var.tags, { Name = "${var.instance_name}-${each.value}" }) } # resource "aws_eip" "instance" { # for_each = toset(var.instance_count) # vpc = true # tags = merge(local.standard_tags, var.tags, { Name = "${var.instance_name}-${each.value}" }) # } # resource "aws_eip_association" "instance" { # for_each = toset(var.instance_count) # network_interface_id = aws_network_interface.instance[each.key].id # allocation_id = aws_eip.instance[each.key].id # } resource "aws_instance" "instance" { # TODO: Make instance count numeric. While Instance count is currently a list of strings so # that the names are nicely 1, 2, and 3, but this could also have been solved with a '+1'. A count # seems like it would be cleaner. for_each = toset(var.instance_count) #availability_zone = var.azs[count.index % 2] tenancy = "default" ebs_optimized = true disable_api_termination = var.instance_termination_protection instance_initiated_shutdown_behavior = "stop" instance_type = var.environment == "prod" ? "t3a.medium" : "t3a.small" key_name = "msoc-build" monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time iam_instance_profile = module.instance_profile.profile_id metadata_options { http_endpoint = "enabled" # checkov:skip=CKV_AWS_79:see tfsec explanation # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668 http_tokens = "optional" } ami = local.ami_map[local.ami_selection] # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then # that could be removed. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] } # These device definitions are optional, but added for clarity. root_block_device { volume_type = "gp2" #volume_size = "60" delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn } ebs_block_device { # swap device_name = "/dev/xvdm" volume_size = 48 delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly. # This may prompt replacement when the AMI is updated. # See: # https://github.com/hashicorp/terraform/issues/19958 # https://github.com/terraform-providers/terraform-provider-aws/issues/13118 snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id } ebs_block_device { # /home device_name = "/dev/xvdn" # volume_size = xx delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id } ebs_block_device { # /var device_name = "/dev/xvdo" # volume_size = xx delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id } ebs_block_device { # /var/tmp device_name = "/dev/xvdp" # volume_size = xx delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id } ebs_block_device { # /var/log device_name = "/dev/xvdq" # volume_size = xx delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id } ebs_block_device { # /var/log/audit device_name = "/dev/xvdr" # volume_size = xx delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id } ebs_block_device { # /tmp device_name = "/dev/xvds" # volume_size = xx delete_on_termination = true encrypted = true kms_key_id = data.aws_kms_key.ebs-key.arn snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id } network_interface { device_index = 0 network_interface_id = aws_network_interface.instance[each.key].id } #TODO switch to dynamic tag user_data = data.template_cloudinit_config.cloud_init_config[each.key].rendered tags = merge(local.standard_tags, var.tags, { "Name" : length(var.instance_count) > 1 ? "${var.instance_name}-${each.value}" : var.instance_name }) volume_tags = merge(local.standard_tags, var.tags, var.instance_tags[tonumber(each.key) - 1], { "Name" : length(var.instance_count) > 1 ? "${var.instance_name}-${each.value}" : var.instance_name }) } module "private_dns_record" { for_each = toset(var.instance_count) source = "../../submodules/dns/private_A_record" name = "${var.instance_name}-${each.value}" ip_addresses = [aws_instance.instance[each.key].private_ip] dns_info = var.dns_info reverse_enabled = var.reverse_enabled providers = { aws.c2 = aws.c2 } } # Render a multi-part cloud-init config making use of the part # above, and other source files data "template_cloudinit_config" "cloud_init_config" { for_each = toset(var.instance_count) gzip = true base64_encode = true # Main cloud-config configuration file. part { filename = "init.cfg" content_type = "text/cloud-config" content = templatefile("${path.module}/cloud-init/cloud-init.tpl", { hostname = "${var.instance_name}-${each.value}" fqdn = "${var.instance_name}-${each.value}.${var.dns_info["private"]["zone"]}" environment = var.environment salt_master = local.salt_master proxy = local.proxy aws_partition = var.aws_partition aws_partition_alias = var.aws_partition_alias aws_region = var.aws_region } ) } } #---------------------------------------------------------------------------- # Vault Server Security Group #---------------------------------------------------------------------------- resource "aws_security_group" "instance_security_group" { name = "${var.instance_name}_security_group" description = "Security Group for ${var.instance_name}(s)" vpc_id = var.vpc_id tags = merge(local.standard_tags, var.tags) } #---------------------------------------------------------------------------- # INGRESS #---------------------------------------------------------------------------- resource "aws_security_group_rule" "vault_server_from_alb" { type = "ingress" description = "Allows the servers to receive traffic for troubleshooting" from_port = 443 to_port = 443 protocol = "tcp" source_security_group_id = aws_security_group.vault_ALB_server.id security_group_id = aws_security_group.instance_security_group.id } #---------------------------------------------------------------------------- # EGRESS #---------------------------------------------------------------------------- resource "aws_security_group_rule" "https-out" { type = "egress" description = "For endpoints and troubleshooting" from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["10.0.0.0/8"] security_group_id = aws_security_group.instance_security_group.id } resource "aws_security_group_rule" "vault_server_to_alb" { type = "egress" description = "Allow Vault to communicate via HTTPS w/ other members in cluster" from_port = 443 to_port = 443 protocol = "tcp" source_security_group_id = aws_security_group.vault_ALB_server.id security_group_id = aws_security_group.instance_security_group.id } #grab the dynamodb endpoint data "aws_prefix_list" "private_dynamodb" { name = "com.amazonaws.*.dynamodb" } resource "aws_security_group_rule" "vault_server_egress_dynamodb" { type = "egress" description = "Vault HTTPS - Outbound to Dynamodb" from_port = 443 to_port = 443 protocol = "tcp" security_group_id = aws_security_group.instance_security_group.id prefix_list_ids = [data.aws_prefix_list.private_dynamodb.id] }