main.tf 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_instance" "instance" {
  12. count = var.enabled ? 1 : 0
  13. # availability_zone = var.azs[count.index % 2]
  14. tenancy = "default"
  15. ebs_optimized = true
  16. disable_api_termination = var.instance_termination_protection
  17. instance_initiated_shutdown_behavior = "stop"
  18. instance_type = "t3a.medium"
  19. key_name = "msoc-build"
  20. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  21. iam_instance_profile = "msoc-default-instance-profile"
  22. subnet_id = var.subnets[0]
  23. associate_public_ip_address = true
  24. vpc_security_group_ids = [data.aws_security_group.typical-host.id, aws_security_group.test_instance_security_group[count.index].id]
  25. metadata_options {
  26. http_endpoint = "enabled"
  27. # checkov:skip=CKV_AWS_79:see tfsec explanation
  28. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  29. http_tokens = "optional"
  30. }
  31. ami = local.ami
  32. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  33. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  34. # that could be removed.
  35. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  36. user_data = data.template_cloudinit_config.cloud-init.rendered
  37. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = var.instance_name })
  38. volume_tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  39. }
  40. module "private_dns_record" {
  41. count = var.enabled ? 1 : 0
  42. source = "../../submodules/dns/private_A_record"
  43. name = var.instance_name
  44. ip_addresses = [aws_instance.instance[count.index].private_ip]
  45. dns_info = var.dns_info
  46. reverse_enabled = var.reverse_enabled
  47. providers = {
  48. aws.c2 = aws.c2
  49. }
  50. }
  51. module "public_dns_record" {
  52. count = var.enabled ? 1 : 0
  53. source = "../../submodules/dns/public_A_record"
  54. name = var.instance_name
  55. ip_addresses = [aws_instance.instance[count.index].public_ip]
  56. dns_info = var.dns_info
  57. providers = {
  58. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  59. }
  60. }
  61. # Render a multi-part cloud-init config making use of the part
  62. # above, and other source files
  63. data "template_cloudinit_config" "cloud-init" {
  64. gzip = true
  65. base64_encode = true
  66. # Main cloud-config configuration file.
  67. part {
  68. filename = "init.cfg"
  69. content_type = "text/cloud-config"
  70. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  71. {
  72. hostname = var.instance_name
  73. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  74. environment = var.environment
  75. salt_master = local.salt_master
  76. proxy = local.proxy
  77. aws_partition = var.aws_partition
  78. aws_partition_alias = var.aws_partition_alias
  79. aws_region = var.aws_region
  80. }
  81. )
  82. }
  83. # Additional parts as needed
  84. #part {
  85. # content_type = "text/x-shellscript"
  86. # content = "ffbaz"
  87. #}
  88. }
  89. #----------------------------------------------------------------------------
  90. # Test Instance Security Group
  91. #----------------------------------------------------------------------------
  92. resource "aws_security_group" "test_instance_security_group" {
  93. count = var.enabled ? 1 : 0
  94. name = "test_instance_security_group"
  95. description = "Security Group for Test Instance Server(s)"
  96. vpc_id = var.vpc_id
  97. tags = merge(local.standard_tags, var.tags)
  98. }
  99. #----------------------------------------------------------------------------
  100. # INGRESS
  101. #----------------------------------------------------------------------------
  102. resource "aws_security_group_rule" "all-in" {
  103. count = var.enabled ? 1 : 0
  104. type = "ingress"
  105. description = "Allow all inbound to test instance"
  106. from_port = -1
  107. to_port = -1
  108. protocol = -1
  109. cidr_blocks = ["0.0.0.0/0"]
  110. security_group_id = aws_security_group.test_instance_security_group[count.index].id
  111. }
  112. #----------------------------------------------------------------------------
  113. # EGRESS
  114. #----------------------------------------------------------------------------
  115. # Test Instance can access any port
  116. resource "aws_security_group_rule" "test_instance-out-all-ports" {
  117. count = var.enabled ? 1 : 0
  118. type = "egress"
  119. description = "Test Instance can access any port internally - Outbound"
  120. protocol = "all"
  121. from_port = -1
  122. to_port = -1
  123. cidr_blocks = ["0.0.0.0/0"]
  124. security_group_id = aws_security_group.test_instance_security_group[count.index].id
  125. }