main.tf 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. # Rather than pass in the aws security group, we just look it up. This will
  6. # probably be useful other places, as well.
  7. data "aws_security_group" "typical-host" {
  8. name = "typical-host"
  9. vpc_id = var.vpc_id
  10. }
  11. # Use the default EBS key
  12. data "aws_kms_key" "ebs-key" {
  13. key_id = "alias/ebs_root_encrypt_decrypt"
  14. }
  15. resource "aws_network_interface" "instance" {
  16. subnet_id = var.subnets[0]
  17. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.mailrelay_security_group.id ]
  18. description = var.instance_name
  19. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_instance" "instance" {
  22. tenancy = "default"
  23. ebs_optimized = true
  24. disable_api_termination = var.instance_termination_protection
  25. instance_initiated_shutdown_behavior = "stop"
  26. instance_type = var.instance_type
  27. key_name = "msoc-build"
  28. monitoring = false
  29. iam_instance_profile = "msoc-default-instance-profile"
  30. ami = local.ami_map[local.ami_selection]
  31. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  32. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  33. # that could be removed.
  34. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  35. # These device definitions are optional, but added for clarity.
  36. root_block_device {
  37. volume_type = "gp2"
  38. #volume_size = "60"
  39. delete_on_termination = true
  40. encrypted = true
  41. kms_key_id = data.aws_kms_key.ebs-key.arn
  42. }
  43. ebs_block_device {
  44. # swap
  45. device_name = "/dev/xvdm"
  46. #volume_size = 48
  47. delete_on_termination = true
  48. encrypted = true
  49. kms_key_id = data.aws_kms_key.ebs-key.arn
  50. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  51. # This may prompt replacement when the AMI is updated.
  52. # See:
  53. # https://github.com/hashicorp/terraform/issues/19958
  54. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  55. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  56. }
  57. ebs_block_device {
  58. # /home
  59. device_name = "/dev/xvdn"
  60. # volume_size = xx
  61. delete_on_termination = true
  62. encrypted = true
  63. kms_key_id = data.aws_kms_key.ebs-key.arn
  64. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  65. }
  66. ebs_block_device {
  67. # /var
  68. device_name = "/dev/xvdo"
  69. # volume_size = xx
  70. delete_on_termination = true
  71. encrypted = true
  72. kms_key_id = data.aws_kms_key.ebs-key.arn
  73. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  74. }
  75. ebs_block_device {
  76. # /var/tmp
  77. device_name = "/dev/xvdp"
  78. # volume_size = xx
  79. delete_on_termination = true
  80. encrypted = true
  81. kms_key_id = data.aws_kms_key.ebs-key.arn
  82. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  83. }
  84. ebs_block_device {
  85. # /var/log
  86. device_name = "/dev/xvdq"
  87. # volume_size = xx
  88. delete_on_termination = true
  89. encrypted = true
  90. kms_key_id = data.aws_kms_key.ebs-key.arn
  91. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  92. }
  93. ebs_block_device {
  94. # /var/log/audit
  95. device_name = "/dev/xvdr"
  96. # volume_size = xx
  97. delete_on_termination = true
  98. encrypted = true
  99. kms_key_id = data.aws_kms_key.ebs-key.arn
  100. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  101. }
  102. ebs_block_device {
  103. # /tmp
  104. device_name = "/dev/xvds"
  105. # volume_size = xx
  106. delete_on_termination = true
  107. encrypted = true
  108. kms_key_id = data.aws_kms_key.ebs-key.arn
  109. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  110. }
  111. network_interface {
  112. device_index = 0
  113. network_interface_id = aws_network_interface.instance.id
  114. }
  115. user_data = data.template_cloudinit_config.cloud_init_config.rendered
  116. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  117. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  118. }
  119. module "private_dns_record" {
  120. source = "../../submodules/dns/private_A_record"
  121. name = var.instance_name
  122. ip_addresses = [ aws_instance.instance.private_ip ]
  123. dns_info = var.dns_info
  124. reverse_enabled = var.reverse_enabled
  125. providers = {
  126. aws.c2 = aws.c2
  127. }
  128. }
  129. #The Cloud init data is to prepare the instance for use.
  130. data "template_file" "cloud_init" {
  131. # Should these be in a common directory? I suspect they'd be reusable
  132. template = file("${path.module}/cloud-init/cloud_init.tpl")
  133. vars = {
  134. hostname = var.instance_name
  135. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  136. environment = var.environment
  137. salt_master = var.salt_master
  138. proxy = var.proxy
  139. aws_partition = var.aws_partition
  140. aws_partition_alias = var.aws_partition_alias
  141. aws_region = var.aws_region
  142. }
  143. }
  144. # Render a multi-part cloud-init config making use of the part
  145. # above, and other source files
  146. data "template_cloudinit_config" "cloud_init_config" {
  147. gzip = true
  148. base64_encode = true
  149. # Main cloud-config configuration file.
  150. part {
  151. filename = "init.cfg"
  152. content_type = "text/cloud-config"
  153. content = data.template_file.cloud_init.rendered
  154. }
  155. # part {
  156. # content_type = "text/cloud-boothook"
  157. # content = file("${path.module}/cloud-init/repo_server_volumes.boothook")
  158. #}
  159. }
  160. resource "aws_security_group" "mailrelay_security_group" {
  161. name = "mailrelay_security_group"
  162. description = "Security Group for the Mail Relay Server(s)"
  163. vpc_id = var.vpc_id
  164. tags = merge(var.standard_tags, var.tags)
  165. }
  166. resource "aws_security_group_rule" "smtp-in" {
  167. description = "inbound smtp requests"
  168. type = "ingress"
  169. from_port = 25
  170. to_port = 25
  171. protocol = "tcp"
  172. cidr_blocks = [ "10.0.0.0/8" ]
  173. security_group_id = aws_security_group.mailrelay_security_group.id
  174. }
  175. #resource "aws_security_group_rule" "smtp-out" {
  176. # description = "outbound smtp requests"
  177. # type = "egress"
  178. # from_port = 25
  179. # to_port = 25
  180. # protocol = "tcp"
  181. # cidr_blocks = [ "10.0.0.0/8" ]
  182. # security_group_id = aws_security_group.mailrelay_security_group.id
  183. #}
  184. resource "aws_security_group_rule" "submission-out" {
  185. description = "outbound submission (smtp-s) requests"
  186. type = "egress"
  187. from_port = 587
  188. to_port = 587
  189. protocol = "tcp"
  190. cidr_blocks = [ "0.0.0.0/0" ]
  191. security_group_id = aws_security_group.mailrelay_security_group.id
  192. }