main.tf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. locals {
  2. instance_name = "resolver-${var.aws_partition_alias}-${var.instance_number}"
  3. }
  4. # Rather than pass in the aws security group, we just look it up. This will
  5. # probably be useful other places, as well.
  6. data "aws_security_group" "typical-host" {
  7. name = "typical-host"
  8. vpc_id = var.vpc_id
  9. }
  10. resource "aws_network_interface" "instance" {
  11. subnet_id = var.subnet_id
  12. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.dns_security_group.id ]
  13. description = local.instance_name
  14. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  15. }
  16. resource "aws_eip" "instance" {
  17. vpc = true
  18. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  19. }
  20. resource "aws_eip_association" "instance" {
  21. network_interface_id = aws_network_interface.instance.id
  22. allocation_id = aws_eip.instance.id
  23. }
  24. resource "aws_instance" "instance" {
  25. #availability_zone = var.azs[count.index % 2]
  26. tenancy = "default"
  27. ebs_optimized = true
  28. disable_api_termination = var.instance_termination_protection
  29. instance_initiated_shutdown_behavior = "stop"
  30. instance_type = var.resolver_instance_type
  31. key_name = var.resolver_instance_key_name
  32. monitoring = false
  33. iam_instance_profile = "msoc-default-instance-profile"
  34. ami = local.ami_map["minion"]
  35. lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  36. root_block_device {
  37. volume_type = "gp2"
  38. #volume_size = "60"
  39. delete_on_termination = true
  40. }
  41. network_interface {
  42. device_index = 0
  43. network_interface_id = aws_network_interface.instance.id
  44. }
  45. user_data = data.template_cloudinit_config.cloud-init.rendered
  46. tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  47. }
  48. module "private_dns_record" {
  49. source = "../../../submodules/dns/private_A_record"
  50. name = local.instance_name
  51. ip_addresses = [ aws_instance.instance.private_ip ]
  52. dns_info = var.dns_info
  53. reverse_enabled = var.reverse_enabled
  54. providers = {
  55. aws.c2 = aws.c2
  56. }
  57. }
  58. module "public_dns_record" {
  59. source = "../../../submodules/dns/public_A_record"
  60. name = local.instance_name
  61. ip_addresses = [ aws_eip.instance.public_ip ]
  62. dns_info = var.dns_info
  63. providers = {
  64. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  65. }
  66. }
  67. # Render a multi-part cloud-init config making use of the part
  68. # above, and other source files
  69. data "template_cloudinit_config" "cloud-init" {
  70. gzip = true
  71. base64_encode = true
  72. # Main cloud-config configuration file.
  73. part {
  74. filename = "init.cfg"
  75. content_type = "text/cloud-config"
  76. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  77. {
  78. hostname = local.instance_name
  79. fqdn = "resolver-${var.aws_partition_alias}-${var.instance_number}.${var.dns_info["private"]["zone"]}"
  80. environment = var.environment
  81. # can't use the DNS name like we would most places, because this is the DNS server
  82. saltmaster = var.salt_master_ip
  83. proxy = var.proxy_ip
  84. aws_partition = var.aws_partition
  85. aws_partition_alias = var.aws_partition_alias
  86. aws_region = var.aws_region
  87. }
  88. )
  89. }
  90. # Additional parts as needed
  91. #part {
  92. # content_type = "text/x-shellscript"
  93. # content = "ffbaz"
  94. #}
  95. }
  96. resource "aws_security_group" "dns_security_group" {
  97. name = "dns_security_group_${var.instance_number}"
  98. description = "DNS Security Group"
  99. vpc_id = var.vpc_id
  100. tags = merge(var.standard_tags, var.tags)
  101. }
  102. resource "aws_security_group_rule" "dns-tcp" {
  103. type = "ingress"
  104. from_port = 53
  105. to_port = 53
  106. protocol = "tcp"
  107. cidr_blocks = [ "10.0.0.0/8" ]
  108. security_group_id = aws_security_group.dns_security_group.id
  109. }
  110. resource "aws_security_group_rule" "dns-udp" {
  111. type = "ingress"
  112. from_port = 53
  113. to_port = 53
  114. protocol = "udp"
  115. cidr_blocks = [ "10.0.0.0/8" ]
  116. security_group_id = aws_security_group.dns_security_group.id
  117. }
  118. resource "aws_security_group_rule" "dns_outbound_tcp" {
  119. type = "egress"
  120. from_port = 53
  121. to_port = 53
  122. protocol = "tcp"
  123. cidr_blocks = [ "0.0.0.0/0" ]
  124. security_group_id = aws_security_group.dns_security_group.id
  125. }
  126. resource "aws_security_group_rule" "dns_outbound_udp" {
  127. type = "egress"
  128. from_port = 53
  129. to_port = 53
  130. protocol = "udp"
  131. cidr_blocks = [ "0.0.0.0/0" ]
  132. security_group_id = aws_security_group.dns_security_group.id
  133. }