main.tf 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Rather than pass in the aws security group, we just look it up. This will
  2. # probably be useful other places, as well.
  3. data "aws_security_group" "typical-host" {
  4. name = "typical-host"
  5. vpc_id = var.vpc_id
  6. }
  7. # Use the default EBS key
  8. data "aws_kms_key" "ebs-key" {
  9. key_id = "alias/ebs_root_encrypt_decrypt"
  10. }
  11. resource "aws_network_interface" "instance" {
  12. subnet_id = var.subnets[0]
  13. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.bastion_security_group.id ]
  14. description = var.instance_name
  15. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  16. }
  17. resource "aws_eip" "instance" {
  18. vpc = true
  19. tags = merge(var.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_eip_association" "instance" {
  22. network_interface_id = aws_network_interface.instance.id
  23. allocation_id = aws_eip.instance.id
  24. }
  25. resource "aws_instance" "instance" {
  26. #availability_zone = var.azs[count.index % 2]
  27. tenancy = "default"
  28. ebs_optimized = true
  29. disable_api_termination = var.instance_termination_protection
  30. instance_initiated_shutdown_behavior = "stop"
  31. instance_type = var.instance_type
  32. key_name = "msoc-build"
  33. monitoring = false
  34. iam_instance_profile = "msoc-default-instance-profile"
  35. ami = local.ami_map["minion"]
  36. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  37. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  38. # that could be removed.
  39. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  40. # These device definitions are optional, but added for clarity.
  41. root_block_device {
  42. volume_type = "gp2"
  43. #volume_size = "60"
  44. delete_on_termination = true
  45. encrypted = true
  46. kms_key_id = data.aws_kms_key.ebs-key.arn
  47. }
  48. ebs_block_device {
  49. # swap
  50. device_name = "/dev/xvdm"
  51. volume_size = 48
  52. delete_on_termination = true
  53. encrypted = true
  54. kms_key_id = data.aws_kms_key.ebs-key.arn
  55. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  56. # This may prompt replacement when the AMI is updated.
  57. # See:
  58. # https://github.com/hashicorp/terraform/issues/19958
  59. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  60. snapshot_id = local.block_device_mappings["minion"]["/dev/xvdm"].ebs.snapshot_id
  61. }
  62. ebs_block_device {
  63. # /home
  64. device_name = "/dev/xvdn"
  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["minion"]["/dev/xvdn"].ebs.snapshot_id
  70. }
  71. ebs_block_device {
  72. # /var
  73. device_name = "/dev/xvdo"
  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["minion"]["/dev/xvdo"].ebs.snapshot_id
  79. }
  80. ebs_block_device {
  81. # /var/tmp
  82. device_name = "/dev/xvdp"
  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["minion"]["/dev/xvdp"].ebs.snapshot_id
  88. }
  89. ebs_block_device {
  90. # /var/log
  91. device_name = "/dev/xvdq"
  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["minion"]["/dev/xvdq"].ebs.snapshot_id
  97. }
  98. ebs_block_device {
  99. # /var/log/audit
  100. device_name = "/dev/xvdr"
  101. # volume_size = xx
  102. delete_on_termination = true
  103. encrypted = true
  104. kms_key_id = data.aws_kms_key.ebs-key.arn
  105. snapshot_id = local.block_device_mappings["minion"]["/dev/xvdr"].ebs.snapshot_id
  106. }
  107. ebs_block_device {
  108. # /tmp
  109. device_name = "/dev/xvds"
  110. # volume_size = xx
  111. delete_on_termination = true
  112. encrypted = true
  113. kms_key_id = data.aws_kms_key.ebs-key.arn
  114. snapshot_id = local.block_device_mappings["minion"]["/dev/xvds"].ebs.snapshot_id
  115. }
  116. network_interface {
  117. device_index = 0
  118. network_interface_id = aws_network_interface.instance.id
  119. }
  120. user_data = data.template_cloudinit_config.cloud-init.rendered
  121. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  122. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  123. }
  124. module "private_dns_record" {
  125. source = "../../submodules/dns/private_A_record"
  126. name = var.instance_name
  127. ip_addresses = [ aws_instance.instance.private_ip ]
  128. dns_info = var.dns_info
  129. reverse_enabled = var.reverse_enabled
  130. providers = {
  131. aws.c2 = aws.c2
  132. }
  133. }
  134. module "public_dns_record" {
  135. source = "../../submodules/dns/public_A_record"
  136. name = var.instance_name
  137. ip_addresses = [ aws_eip.instance.public_ip ]
  138. dns_info = var.dns_info
  139. providers = {
  140. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  141. }
  142. }
  143. data "template_file" "cloud-init" {
  144. # Should these be in a common directory? I suspect they'd be reusable
  145. template = "${file("${path.module}/cloud-init/cloud-init.tpl")}"
  146. vars = {
  147. hostname = var.instance_name
  148. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  149. environment = var.environment
  150. salt_master = var.salt_master
  151. proxy = var.proxy
  152. aws_partition = var.aws_partition
  153. aws_partition_alias = var.aws_partition_alias
  154. }
  155. }
  156. # Render a multi-part cloud-init config making use of the part
  157. # above, and other source files
  158. data "template_cloudinit_config" "cloud-init" {
  159. gzip = true
  160. base64_encode = true
  161. # Main cloud-config configuration file.
  162. part {
  163. filename = "init.cfg"
  164. content_type = "text/cloud-config"
  165. content = "${data.template_file.cloud-init.rendered}"
  166. }
  167. # Additional parts as needed
  168. #part {
  169. # content_type = "text/x-shellscript"
  170. # content = "ffbaz"
  171. #}
  172. }
  173. resource "aws_security_group" "bastion_security_group" {
  174. name = "bastion_security_group"
  175. description = "Security Group for Bastion Server(s)"
  176. vpc_id = var.vpc_id
  177. tags = merge(var.standard_tags, var.tags)
  178. }
  179. resource "aws_security_group_rule" "ssh-in" {
  180. type = "ingress"
  181. from_port = 22
  182. to_port = 22
  183. protocol = "tcp"
  184. cidr_blocks = var.trusted_ips
  185. security_group_id = aws_security_group.bastion_security_group.id
  186. }
  187. resource "aws_security_group_rule" "ssh-out" {
  188. type = "egress"
  189. from_port = 22
  190. to_port = 22
  191. protocol = "tcp"
  192. cidr_blocks = [ "10.0.0.0/8" ]
  193. security_group_id = aws_security_group.bastion_security_group.id
  194. }
  195. # Bastion can access any port internally
  196. resource "aws_security_group_rule" "bastion-out-all-ports" {
  197. type = "egress"
  198. protocol = "all"
  199. from_port = -1
  200. to_port = -1
  201. cidr_blocks = [ "10.0.0.0/8" ]
  202. security_group_id = aws_security_group.bastion_security_group.id
  203. }
  204. # Bastion gets http/https out to the internet. Most hosts need to use the proxy
  205. resource "aws_security_group_rule" "http-out" {
  206. type = "egress"
  207. from_port = 80
  208. to_port = 80
  209. protocol = "tcp"
  210. cidr_blocks = [ "0.0.0.0/0" ]
  211. security_group_id = aws_security_group.bastion_security_group.id
  212. }
  213. resource "aws_security_group_rule" "https-out" {
  214. type = "egress"
  215. from_port = 443
  216. to_port = 443
  217. protocol = "tcp"
  218. cidr_blocks = [ "0.0.0.0/0" ]
  219. security_group_id = aws_security_group.bastion_security_group.id
  220. }