main.tf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. locals {
  2. instance_name = "resolver-${var.aws_partition_alias}"
  3. }
  4. resource "aws_network_interface" "instance" {
  5. subnet_id = var.subnet_id
  6. security_groups = [ module.required_security_group.id, aws_security_group.dns_security_group.id ]
  7. description = local.instance_name
  8. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  9. }
  10. resource "aws_eip" "instance" {
  11. vpc = true
  12. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  13. }
  14. resource "aws_eip_association" "instance" {
  15. network_interface_id = aws_network_interface.instance.id
  16. allocation_id = aws_eip.instance.id
  17. }
  18. resource "aws_instance" "instance" {
  19. #availability_zone = var.azs[count.index % 2]
  20. tenancy = "default"
  21. ebs_optimized = true
  22. disable_api_termination = var.instance_termination_protection
  23. instance_initiated_shutdown_behavior = "stop"
  24. instance_type = var.resolver_instance_type
  25. key_name = var.resolver_instance_key_name
  26. monitoring = false
  27. ami = local.ami_map["minion"]
  28. lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  29. root_block_device {
  30. volume_type = "gp2"
  31. #volume_size = "60"
  32. delete_on_termination = true
  33. }
  34. network_interface {
  35. device_index = 0
  36. network_interface_id = aws_network_interface.instance.id
  37. }
  38. user_data = data.template_cloudinit_config.cloud-init.rendered
  39. tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  40. }
  41. module "private_dns_record" {
  42. source = "../../../submodules/dns/private_A_record"
  43. name = local.instance_name
  44. ip_addresses = [ aws_instance.instance.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. source = "../../../submodules/dns/public_A_record"
  53. name = local.instance_name
  54. ip_addresses = [ aws_eip.instance.public_ip ]
  55. dns_info = var.dns_info
  56. providers = {
  57. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  58. }
  59. }
  60. data "template_file" "cloud-init" {
  61. # Should these be in a common directory? I suspect they'd be reusable
  62. template = "${file("${path.module}/cloud-init/cloud-init.tpl")}"
  63. vars = {
  64. hostname = local.instance_name
  65. fqdn = "dns-${var.aws_partition_alias}.${var.dns_info["private"]["zone"]}"
  66. environment = var.environment
  67. # can't use the DNS name like we would most places, because this is the DNS server
  68. saltmaster = var.salt_master_ip
  69. proxy = var.proxy_ip
  70. aws_partition = var.aws_partition
  71. aws_partition_alias = var.aws_partition_alias
  72. }
  73. }
  74. # Render a multi-part cloud-init config making use of the part
  75. # above, and other source files
  76. data "template_cloudinit_config" "cloud-init" {
  77. gzip = true
  78. base64_encode = true
  79. # Main cloud-config configuration file.
  80. part {
  81. filename = "init.cfg"
  82. content_type = "text/cloud-config"
  83. content = "${data.template_file.cloud-init.rendered}"
  84. }
  85. # Additional parts as needed
  86. #part {
  87. # content_type = "text/x-shellscript"
  88. # content = "ffbaz"
  89. #}
  90. }
  91. module "required_security_group" {
  92. source = "../../../submodules/security_group/required_group"
  93. vpc_id = var.vpc_id
  94. cidr_map = var.cidr_map
  95. tags = merge(var.standard_tags, var.tags)
  96. aws_region = var.aws_region
  97. aws_partition = var.aws_partition
  98. }
  99. resource "aws_security_group" "dns_security_group" {
  100. name = "dns_security_group"
  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. }