123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- # 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" "phantom-server-interface" {
- count = local.instance_count
- subnet_id = var.public_subnets[0] # Phantom is on a public subnet for direct comms
- security_groups = [data.aws_security_group.typical-host.id, aws_security_group.phantom_server.id]
- description = "phantom-server"
- tags = merge(local.standard_tags, var.tags, { Name = "phantom-${count.index}" })
- }
- resource "aws_eip" "instance" {
- # checkov:skip=CKV2_AWS_19: EIPs are attached to VPC
- count = local.instance_count
- vpc = true
- tags = merge(local.standard_tags, var.tags, { Name = "phantom-${count.index}" })
- }
- resource "aws_eip_association" "instance" {
- count = local.instance_count
- network_interface_id = aws_network_interface.phantom-server-interface[count.index].id
- allocation_id = aws_eip.instance[count.index].id
- }
- resource "aws_instance" "phantom-server-instance" {
- count = local.instance_count
- tenancy = "default"
- ebs_optimized = true
- disable_api_termination = var.instance_termination_protection
- instance_initiated_shutdown_behavior = "stop"
- instance_type = var.environment == "prod" ? "m5a.4xlarge" : "t3a.xlarge"
- 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] }
- #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
- # These device definitions are optional, but added for clarity.
- root_block_device {
- volume_type = "gp3"
- # volume_size = "100"
- delete_on_termination = true
- encrypted = true
- kms_key_id = data.aws_kms_key.ebs-key.arn
- }
- ebs_block_device {
- # /opt - NOTE: Not in ami
- device_name = "/dev/xvdf"
- volume_type = "gp3"
- volume_size = var.environment == "test" ? 60 : 1000 # Phantom needs extra space for upgrades
- delete_on_termination = var.environment == "test" ? true : false # extra protection against deleting phantom drive
- encrypted = true
- kms_key_id = data.aws_kms_key.ebs-key.arn
- # Phantom is io intensive, so provisionign extra iops and throughput. Cost is about $50/mo
- iops = var.environment == "test" ? 3000 : 9000
- throughput = var.environment == "test" ? 125 : 375
- }
- ebs_block_device {
- # swap
- device_name = "/dev/xvdm"
- volume_type = "gp3"
- volume_size = 8
- 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_type = "gp3"
- volume_size = 8
- 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_type = "gp3"
- volume_size = 30
- 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_type = "gp3"
- # 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_type = "gp3"
- volume_size = 30
- 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_type = "gp3"
- # 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_type = "gp3"
- # 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.phantom-server-interface[count.index].id
- }
- user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
- tags = merge(local.standard_tags, var.tags, { Name = "phantom-${count.index}" })
- volume_tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = "phantom-${count.index}" })
- }
- # Render a multi-part cloud-init config making use of the part
- # above, and other source files
- data "template_cloudinit_config" "cloud-init" {
- count = local.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 = "phantom-${count.index}"
- fqdn = "phantom-${count.index}.${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
- }
- )
- }
- # mount /dev/xvdf at /opt/
- part {
- content_type = "text/cloud-boothook"
- content = file("${path.module}/cloud-init/opt.boothook")
- }
- }
- module "private_dns_record_phantom-server" {
- count = local.instance_count
- source = "../../submodules/dns/private_A_record"
- name = "phantom-${count.index}"
- ip_addresses = [aws_instance.phantom-server-instance[count.index].private_ip]
- dns_info = var.dns_info
- reverse_enabled = var.reverse_enabled
- providers = {
- aws.c2 = aws.c2
- }
- }
|