main.tf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. data "template_file" "cloud-init" {
  68. # Should these be in a common directory? I suspect they'd be reusable
  69. template = file("${path.module}/cloud-init/cloud-init.tpl")
  70. vars = {
  71. hostname = local.instance_name
  72. fqdn = "resolver-${var.aws_partition_alias}-${var.instance_number}.${var.dns_info["private"]["zone"]}"
  73. environment = var.environment
  74. # can't use the DNS name like we would most places, because this is the DNS server
  75. saltmaster = var.salt_master_ip
  76. proxy = var.proxy_ip
  77. aws_partition = var.aws_partition
  78. aws_partition_alias = var.aws_partition_alias
  79. aws_region = var.aws_region
  80. }
  81. }
  82. # Render a multi-part cloud-init config making use of the part
  83. # above, and other source files
  84. data "template_cloudinit_config" "cloud-init" {
  85. gzip = true
  86. base64_encode = true
  87. # Main cloud-config configuration file.
  88. part {
  89. filename = "init.cfg"
  90. content_type = "text/cloud-config"
  91. content = data.template_file.cloud-init.rendered
  92. }
  93. # Additional parts as needed
  94. #part {
  95. # content_type = "text/x-shellscript"
  96. # content = "ffbaz"
  97. #}
  98. }
  99. resource "aws_security_group" "dns_security_group" {
  100. name = "dns_security_group_${var.instance_number}"
  101. description = "DNS Security Group"
  102. vpc_id = var.vpc_id
  103. tags = merge(var.standard_tags, var.tags)
  104. }
  105. resource "aws_security_group_rule" "dns-tcp" {
  106. type = "ingress"
  107. from_port = 53
  108. to_port = 53
  109. protocol = "tcp"
  110. cidr_blocks = [ "10.0.0.0/8" ]
  111. security_group_id = aws_security_group.dns_security_group.id
  112. }
  113. resource "aws_security_group_rule" "dns-udp" {
  114. type = "ingress"
  115. from_port = 53
  116. to_port = 53
  117. protocol = "udp"
  118. cidr_blocks = [ "10.0.0.0/8" ]
  119. security_group_id = aws_security_group.dns_security_group.id
  120. }
  121. resource "aws_security_group_rule" "dns_outbound_tcp" {
  122. type = "egress"
  123. from_port = 53
  124. to_port = 53
  125. protocol = "tcp"
  126. cidr_blocks = [ "0.0.0.0/0" ]
  127. security_group_id = aws_security_group.dns_security_group.id
  128. }
  129. resource "aws_security_group_rule" "dns_outbound_udp" {
  130. type = "egress"
  131. from_port = 53
  132. to_port = 53
  133. protocol = "udp"
  134. cidr_blocks = [ "0.0.0.0/0" ]
  135. security_group_id = aws_security_group.dns_security_group.id
  136. }