main.tf 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. }
  123. module "private_dns_record" {
  124. source = "../../submodules/dns/private_A_record"
  125. name = var.instance_name
  126. ip_addresses = [ aws_instance.instance.private_ip ]
  127. dns_info = var.dns_info
  128. reverse_enabled = var.reverse_enabled
  129. providers = {
  130. aws.c2 = aws.c2
  131. }
  132. }
  133. module "public_dns_record" {
  134. source = "../../submodules/dns/public_A_record"
  135. name = var.instance_name
  136. ip_addresses = [ aws_eip.instance.public_ip ]
  137. dns_info = var.dns_info
  138. providers = {
  139. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  140. }
  141. }
  142. data "template_file" "cloud-init" {
  143. # Should these be in a common directory? I suspect they'd be reusable
  144. template = "${file("${path.module}/cloud-init/cloud-init.tpl")}"
  145. vars = {
  146. hostname = var.instance_name
  147. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  148. environment = var.environment
  149. salt_master = var.salt_master
  150. proxy = var.proxy
  151. aws_partition = var.aws_partition
  152. aws_partition_alias = var.aws_partition_alias
  153. }
  154. }
  155. # Render a multi-part cloud-init config making use of the part
  156. # above, and other source files
  157. data "template_cloudinit_config" "cloud-init" {
  158. gzip = true
  159. base64_encode = true
  160. # Main cloud-config configuration file.
  161. part {
  162. filename = "init.cfg"
  163. content_type = "text/cloud-config"
  164. content = "${data.template_file.cloud-init.rendered}"
  165. }
  166. # Additional parts as needed
  167. #part {
  168. # content_type = "text/x-shellscript"
  169. # content = "ffbaz"
  170. #}
  171. }
  172. resource "aws_security_group" "bastion_security_group" {
  173. name = "bastion_security_group"
  174. description = "Security Group for Bastion Server(s)"
  175. vpc_id = var.vpc_id
  176. tags = merge(var.standard_tags, var.tags)
  177. }
  178. resource "aws_security_group_rule" "ssh-in" {
  179. type = "ingress"
  180. from_port = 22
  181. to_port = 22
  182. protocol = "tcp"
  183. cidr_blocks = var.trusted_ips
  184. security_group_id = aws_security_group.bastion_security_group.id
  185. }
  186. resource "aws_security_group_rule" "ssh-out" {
  187. type = "egress"
  188. from_port = 22
  189. to_port = 22
  190. protocol = "tcp"
  191. cidr_blocks = [ "10.0.0.0/8" ]
  192. security_group_id = aws_security_group.bastion_security_group.id
  193. }
  194. # Bastion can access any port internally
  195. resource "aws_security_group_rule" "bastion-out-all-ports" {
  196. type = "egress"
  197. protocol = "all"
  198. from_port = -1
  199. to_port = -1
  200. cidr_blocks = [ "10.0.0.0/8" ]
  201. security_group_id = aws_security_group.bastion_security_group.id
  202. }
  203. # Bastion gets http/https out to the internet. Most hosts need to use the proxy
  204. resource "aws_security_group_rule" "http-out" {
  205. type = "egress"
  206. from_port = 80
  207. to_port = 80
  208. protocol = "tcp"
  209. cidr_blocks = [ "0.0.0.0/0" ]
  210. security_group_id = aws_security_group.bastion_security_group.id
  211. }
  212. resource "aws_security_group_rule" "https-out" {
  213. type = "egress"
  214. from_port = 443
  215. to_port = 443
  216. protocol = "tcp"
  217. cidr_blocks = [ "0.0.0.0/0" ]
  218. security_group_id = aws_security_group.bastion_security_group.id
  219. }