instance-mailrelay2.tf 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # MSOCI-1852 - Mailrelay2 server that requires authentication
  2. resource "aws_network_interface" "instance2" {
  3. subnet_id = var.subnets[0]
  4. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.mailrelay_security_group.id]
  5. description = var.instance_name
  6. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  7. }
  8. resource "aws_instance" "instance2" {
  9. tenancy = "default"
  10. ebs_optimized = true
  11. disable_api_termination = var.instance_termination_protection
  12. instance_initiated_shutdown_behavior = "stop"
  13. instance_type = "t3a.xlarge"
  14. key_name = "msoc-build"
  15. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  16. iam_instance_profile = "msoc-default-instance-profile"
  17. metadata_options {
  18. http_endpoint = "enabled"
  19. # checkov:skip=CKV_AWS_79:see tfsec explanation
  20. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  21. http_tokens = "optional"
  22. }
  23. ami = local.ami_map[local.ami_selection]
  24. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  25. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  26. # that could be removed.
  27. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  28. # These device definitions are optional, but added for clarity.
  29. root_block_device {
  30. volume_type = "gp2"
  31. #volume_size = "60"
  32. delete_on_termination = true
  33. encrypted = true
  34. kms_key_id = data.aws_kms_key.ebs-key.arn
  35. }
  36. ebs_block_device {
  37. # swap
  38. device_name = "/dev/xvdm"
  39. #volume_size = 48
  40. delete_on_termination = true
  41. encrypted = true
  42. kms_key_id = data.aws_kms_key.ebs-key.arn
  43. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  44. # This may prompt replacement when the AMI is updated.
  45. # See:
  46. # https://github.com/hashicorp/terraform/issues/19958
  47. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  48. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  49. }
  50. ebs_block_device {
  51. # /home
  52. device_name = "/dev/xvdn"
  53. # volume_size = xx
  54. delete_on_termination = true
  55. encrypted = true
  56. kms_key_id = data.aws_kms_key.ebs-key.arn
  57. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  58. }
  59. ebs_block_device {
  60. # /var
  61. device_name = "/dev/xvdo"
  62. # volume_size = xx
  63. delete_on_termination = true
  64. encrypted = true
  65. kms_key_id = data.aws_kms_key.ebs-key.arn
  66. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  67. }
  68. ebs_block_device {
  69. # /var/tmp
  70. device_name = "/dev/xvdp"
  71. # volume_size = xx
  72. delete_on_termination = true
  73. encrypted = true
  74. kms_key_id = data.aws_kms_key.ebs-key.arn
  75. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  76. }
  77. ebs_block_device {
  78. # /var/log
  79. device_name = "/dev/xvdq"
  80. # volume_size = xx
  81. delete_on_termination = true
  82. encrypted = true
  83. kms_key_id = data.aws_kms_key.ebs-key.arn
  84. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  85. }
  86. ebs_block_device {
  87. # /var/log/audit
  88. device_name = "/dev/xvdr"
  89. # volume_size = xx
  90. delete_on_termination = true
  91. encrypted = true
  92. kms_key_id = data.aws_kms_key.ebs-key.arn
  93. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  94. }
  95. ebs_block_device {
  96. # /tmp
  97. device_name = "/dev/xvds"
  98. # volume_size = xx
  99. delete_on_termination = true
  100. encrypted = true
  101. kms_key_id = data.aws_kms_key.ebs-key.arn
  102. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  103. }
  104. network_interface {
  105. device_index = 0
  106. network_interface_id = aws_network_interface.instance2.id
  107. }
  108. user_data = data.template_cloudinit_config.cloud_init_config2.rendered
  109. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = var.instance_name })
  110. volume_tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  111. }
  112. module "private_dns_record2" {
  113. source = "../../submodules/dns/private_A_record"
  114. name = var.instance_name
  115. ip_addresses = [aws_instance.instance2.private_ip]
  116. dns_info = var.dns_info
  117. reverse_enabled = var.reverse_enabled
  118. providers = {
  119. aws.c2 = aws.c2
  120. }
  121. }
  122. # Render a multi-part cloud-init config making use of the part
  123. # above, and other source files
  124. data "template_cloudinit_config" "cloud_init_config2" {
  125. gzip = true
  126. base64_encode = true
  127. # Main cloud-config configuration file.
  128. part {
  129. filename = "init.cfg"
  130. content_type = "text/cloud-config"
  131. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  132. {
  133. hostname = var.instance_name
  134. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  135. environment = var.environment
  136. salt_master = local.salt_master
  137. proxy = local.proxy
  138. aws_partition = var.aws_partition
  139. aws_partition_alias = var.aws_partition_alias
  140. aws_region = var.aws_region
  141. }
  142. )
  143. }
  144. # part {
  145. # content_type = "text/cloud-boothook"
  146. # content = file("${path.module}/cloud-init/repo_server_volumes.boothook")
  147. #}
  148. }