main.tf 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. }
  5. data "aws_security_group" "typical-host" {
  6. name = "typical-host"
  7. vpc_id = var.vpc_id
  8. }
  9. # Use the default EBS key
  10. data "aws_kms_key" "ebs-key" {
  11. key_id = "alias/ebs_root_encrypt_decrypt"
  12. }
  13. resource "aws_network_interface" "jira-server-interface" {
  14. subnet_id = var.private_subnets[0]
  15. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.jira_server.id]
  16. description = "jira-server"
  17. tags = merge(local.standard_tags, var.tags, { Name = "jira-server" })
  18. }
  19. resource "aws_instance" "jira-server-instance" {
  20. tenancy = "default"
  21. ebs_optimized = true
  22. disable_api_termination = var.instance_termination_protection
  23. instance_initiated_shutdown_behavior = "stop"
  24. instance_type = "m5a.xlarge"
  25. key_name = "msoc-build"
  26. monitoring = false
  27. iam_instance_profile = aws_iam_instance_profile.jira_server_instance_profile.name
  28. ami = local.ami_map[local.ami_selection]
  29. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  30. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  31. # that could be removed.
  32. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  33. #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  34. # These device definitions are optional, but added for clarity.
  35. root_block_device {
  36. volume_type = "gp3"
  37. volume_size = "100"
  38. delete_on_termination = true
  39. encrypted = true
  40. kms_key_id = data.aws_kms_key.ebs-key.arn
  41. }
  42. ebs_block_device {
  43. # swap
  44. device_name = "/dev/xvdm"
  45. volume_type = "gp3"
  46. volume_size = 8
  47. delete_on_termination = true
  48. encrypted = true
  49. kms_key_id = data.aws_kms_key.ebs-key.arn
  50. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  51. # This may prompt replacement when the AMI is updated.
  52. # See:
  53. # https://github.com/hashicorp/terraform/issues/19958
  54. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  55. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  56. }
  57. ebs_block_device {
  58. # /home
  59. device_name = "/dev/xvdn"
  60. volume_type = "gp3"
  61. # volume_size = xx
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  66. }
  67. ebs_block_device {
  68. # /var
  69. device_name = "/dev/xvdo"
  70. volume_type = "gp3"
  71. # volume_size = xx
  72. delete_on_termination = true
  73. encrypted = true
  74. kms_key_id = data.aws_kms_key.ebs-key.arn
  75. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  76. }
  77. ebs_block_device {
  78. # /var/tmp
  79. device_name = "/dev/xvdp"
  80. volume_type = "gp3"
  81. # volume_size = xx
  82. delete_on_termination = true
  83. encrypted = true
  84. kms_key_id = data.aws_kms_key.ebs-key.arn
  85. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  86. }
  87. ebs_block_device {
  88. # /var/log
  89. device_name = "/dev/xvdq"
  90. volume_type = "gp3"
  91. # volume_size = xx
  92. delete_on_termination = true
  93. encrypted = true
  94. kms_key_id = data.aws_kms_key.ebs-key.arn
  95. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  96. }
  97. ebs_block_device {
  98. # /var/log/audit
  99. device_name = "/dev/xvdr"
  100. volume_type = "gp3"
  101. # volume_size = xx
  102. delete_on_termination = true
  103. encrypted = true
  104. kms_key_id = data.aws_kms_key.ebs-key.arn
  105. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  106. }
  107. ebs_block_device {
  108. # /tmp
  109. device_name = "/dev/xvds"
  110. volume_type = "gp3"
  111. # volume_size = xx
  112. delete_on_termination = true
  113. encrypted = true
  114. kms_key_id = data.aws_kms_key.ebs-key.arn
  115. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  116. }
  117. network_interface {
  118. device_index = 0
  119. network_interface_id = aws_network_interface.jira-server-interface.id
  120. }
  121. user_data = data.template_cloudinit_config.cloud-init.rendered
  122. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = "jira-server" })
  123. volume_tags = merge(local.standard_tags, var.tags, { Name = "jira-server" })
  124. }
  125. # Render a multi-part cloud-init config making use of the part
  126. # above, and other source files
  127. data "template_cloudinit_config" "cloud-init" {
  128. gzip = true
  129. base64_encode = true
  130. # Main cloud-config configuration file.
  131. part {
  132. filename = "init.cfg"
  133. content_type = "text/cloud-config"
  134. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  135. {
  136. hostname = "jira-server"
  137. fqdn = "jira-server.${var.dns_info["private"]["zone"]}"
  138. environment = var.environment
  139. salt_master = local.salt_master
  140. proxy = local.proxy
  141. aws_partition = var.aws_partition
  142. aws_partition_alias = var.aws_partition_alias
  143. aws_region = var.aws_region
  144. }
  145. )
  146. }
  147. # Additional parts as needed
  148. #part {
  149. # content_type = "text/x-shellscript"
  150. # content = "ffbaz"
  151. #}
  152. }
  153. module "private_dns_record_jira-server" {
  154. source = "../../../submodules/dns/private_A_record"
  155. name = "jira-server"
  156. ip_addresses = [aws_instance.jira-server-instance.private_ip]
  157. dns_info = var.dns_info
  158. reverse_enabled = var.reverse_enabled
  159. providers = {
  160. aws.c2 = aws.c2
  161. }
  162. }