instance-mailrelay2.tf 5.0 KB

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