main.tf 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Some instance variables
  2. locals {
  3. ami_selection = "master" # 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.bastion_security_group.id]
  18. description = var.instance_name
  19. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_eip" "instance" {
  22. # checkov:skip=CKV2_AWS_19: EIPs are attached to VPC
  23. vpc = true
  24. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  25. }
  26. resource "aws_eip_association" "instance" {
  27. network_interface_id = aws_network_interface.instance.id
  28. allocation_id = aws_eip.instance.id
  29. }
  30. resource "aws_instance" "instance" {
  31. # availability_zone = var.azs[count.index % 2]
  32. tenancy = "default"
  33. ebs_optimized = true
  34. disable_api_termination = var.instance_termination_protection
  35. instance_initiated_shutdown_behavior = "stop"
  36. instance_type = "t3a.medium"
  37. key_name = "msoc-build"
  38. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  39. iam_instance_profile = "msoc-default-instance-profile"
  40. metadata_options {
  41. http_endpoint = "enabled"
  42. # checkov:skip=CKV_AWS_79:see tfsec explanation
  43. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  44. http_tokens = "optional"
  45. }
  46. ami = local.ami_map[local.ami_selection]
  47. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  48. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  49. # that could be removed.
  50. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  51. # These device definitions are optional, but added for clarity.
  52. root_block_device {
  53. volume_type = "gp2"
  54. #volume_size = "60"
  55. delete_on_termination = true
  56. encrypted = true
  57. kms_key_id = data.aws_kms_key.ebs-key.arn
  58. }
  59. ebs_block_device {
  60. # swap
  61. device_name = "/dev/xvdm"
  62. volume_size = 48
  63. delete_on_termination = true
  64. encrypted = true
  65. kms_key_id = data.aws_kms_key.ebs-key.arn
  66. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  67. # This may prompt replacement when the AMI is updated.
  68. # See:
  69. # https://github.com/hashicorp/terraform/issues/19958
  70. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  71. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  72. }
  73. ebs_block_device {
  74. # /home
  75. device_name = "/dev/xvdn"
  76. # volume_size = xx
  77. delete_on_termination = true
  78. encrypted = true
  79. kms_key_id = data.aws_kms_key.ebs-key.arn
  80. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var
  84. device_name = "/dev/xvdo"
  85. # volume_size = xx
  86. delete_on_termination = true
  87. encrypted = true
  88. kms_key_id = data.aws_kms_key.ebs-key.arn
  89. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var/tmp
  93. device_name = "/dev/xvdp"
  94. # volume_size = xx
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var/log
  102. device_name = "/dev/xvdq"
  103. # volume_size = xx
  104. delete_on_termination = true
  105. encrypted = true
  106. kms_key_id = data.aws_kms_key.ebs-key.arn
  107. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  108. }
  109. ebs_block_device {
  110. # /var/log/audit
  111. device_name = "/dev/xvdr"
  112. # volume_size = xx
  113. delete_on_termination = true
  114. encrypted = true
  115. kms_key_id = data.aws_kms_key.ebs-key.arn
  116. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  117. }
  118. ebs_block_device {
  119. # /tmp
  120. device_name = "/dev/xvds"
  121. # volume_size = xx
  122. delete_on_termination = true
  123. encrypted = true
  124. kms_key_id = data.aws_kms_key.ebs-key.arn
  125. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  126. }
  127. network_interface {
  128. device_index = 0
  129. network_interface_id = aws_network_interface.instance.id
  130. }
  131. user_data = data.template_cloudinit_config.cloud-init.rendered
  132. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = var.instance_name })
  133. volume_tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  134. }
  135. module "private_dns_record" {
  136. source = "../../submodules/dns/private_A_record"
  137. name = var.instance_name
  138. ip_addresses = [aws_instance.instance.private_ip]
  139. dns_info = var.dns_info
  140. reverse_enabled = var.reverse_enabled
  141. providers = {
  142. aws.c2 = aws.c2
  143. }
  144. }
  145. module "public_dns_record" {
  146. source = "../../submodules/dns/public_A_record"
  147. name = var.instance_name
  148. ip_addresses = [aws_eip.instance.public_ip]
  149. dns_info = var.dns_info
  150. providers = {
  151. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  152. }
  153. }
  154. # Render a multi-part cloud-init config making use of the part
  155. # above, and other source files
  156. data "template_cloudinit_config" "cloud-init" {
  157. gzip = true
  158. base64_encode = true
  159. # Main cloud-config configuration file.
  160. part {
  161. filename = "init.cfg"
  162. content_type = "text/cloud-config"
  163. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  164. {
  165. hostname = var.instance_name
  166. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  167. environment = var.environment
  168. salt_master = local.salt_master
  169. proxy = local.proxy
  170. aws_partition = var.aws_partition
  171. aws_partition_alias = var.aws_partition_alias
  172. aws_region = var.aws_region
  173. }
  174. )
  175. }
  176. # Additional parts as needed
  177. #part {
  178. # content_type = "text/x-shellscript"
  179. # content = "ffbaz"
  180. #}
  181. }
  182. #----------------------------------------------------------------------------
  183. # Bastion Security Group
  184. #----------------------------------------------------------------------------
  185. resource "aws_security_group" "bastion_security_group" {
  186. name = "bastion_security_group"
  187. description = "Security Group for Bastion Server(s)"
  188. vpc_id = var.vpc_id
  189. tags = merge(local.standard_tags, var.tags)
  190. }
  191. #----------------------------------------------------------------------------
  192. # INGRESS
  193. #----------------------------------------------------------------------------
  194. resource "aws_security_group_rule" "ssh-in" {
  195. type = "ingress"
  196. description = "SSH - Inbound"
  197. from_port = 22
  198. to_port = 22
  199. protocol = "tcp"
  200. cidr_blocks = local.trusted_ips
  201. security_group_id = aws_security_group.bastion_security_group.id
  202. }
  203. # Uncomment for performance testing of the VPN
  204. #resource "aws_security_group_rule" "iperf-in" {
  205. # description = "iperf testing"
  206. # type = "ingress"
  207. # from_port = 4000
  208. # to_port = 4000
  209. # protocol = "tcp"
  210. # cidr_blocks = [ "0.0.0.0/0" ]
  211. # security_group_id = aws_security_group.bastion_security_group.id
  212. #}
  213. #
  214. #resource "aws_security_group_rule" "iperf-udp-in" {
  215. # description = "iperf testing"
  216. # type = "ingress"
  217. # from_port = 4000
  218. # to_port = 4000
  219. # protocol = "udp"
  220. # cidr_blocks = [ "0.0.0.0/0" ]
  221. # security_group_id = aws_security_group.bastion_security_group.id
  222. #}
  223. resource "aws_security_group_rule" "ssh-out" {
  224. type = "egress"
  225. description = "SSH - Outbound"
  226. from_port = 22
  227. to_port = 22
  228. protocol = "tcp"
  229. cidr_blocks = ["10.0.0.0/8"]
  230. security_group_id = aws_security_group.bastion_security_group.id
  231. }
  232. #----------------------------------------------------------------------------
  233. # EGRESS
  234. #----------------------------------------------------------------------------
  235. # Bastion can access any port internally
  236. resource "aws_security_group_rule" "bastion-out-all-ports" {
  237. type = "egress"
  238. description = "Bastion can access any port internally - Outbound"
  239. protocol = "all"
  240. from_port = -1
  241. to_port = -1
  242. cidr_blocks = ["10.0.0.0/8"]
  243. security_group_id = aws_security_group.bastion_security_group.id
  244. }
  245. # Bastion gets http/https out to the internet. Most hosts need to use the proxy
  246. resource "aws_security_group_rule" "http-out" {
  247. type = "egress"
  248. description = "Bastion HTTP - Outbound"
  249. from_port = 80
  250. to_port = 80
  251. protocol = "tcp"
  252. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-egress-sgr
  253. security_group_id = aws_security_group.bastion_security_group.id
  254. }
  255. resource "aws_security_group_rule" "https-out" {
  256. type = "egress"
  257. description = "Bastion HTTPS - Outbound"
  258. from_port = 443
  259. to_port = 443
  260. protocol = "tcp"
  261. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-egress-sgr
  262. security_group_id = aws_security_group.bastion_security_group.id
  263. }