main.tf 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  27. iam_instance_profile = aws_iam_instance_profile.jira_server_instance_profile.name
  28. metadata_options {
  29. http_endpoint = "enabled"
  30. # checkov:skip=CKV_AWS_79:see tfsec explanation
  31. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  32. http_tokens = "optional"
  33. }
  34. ami = local.ami_map[local.ami_selection]
  35. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  36. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  37. # that could be removed.
  38. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  39. #lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  40. # These device definitions are optional, but added for clarity.
  41. root_block_device {
  42. volume_type = "gp3"
  43. volume_size = "100"
  44. delete_on_termination = true
  45. encrypted = true
  46. kms_key_id = data.aws_kms_key.ebs-key.arn
  47. }
  48. ebs_block_device {
  49. # swap
  50. device_name = "/dev/xvdm"
  51. volume_type = "gp3"
  52. volume_size = 8
  53. delete_on_termination = true
  54. encrypted = true
  55. kms_key_id = data.aws_kms_key.ebs-key.arn
  56. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  57. # This may prompt replacement when the AMI is updated.
  58. # See:
  59. # https://github.com/hashicorp/terraform/issues/19958
  60. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  61. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  62. }
  63. ebs_block_device {
  64. # /home
  65. device_name = "/dev/xvdn"
  66. volume_type = "gp3"
  67. # volume_size = xx
  68. delete_on_termination = true
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  72. }
  73. ebs_block_device {
  74. # /var
  75. device_name = "/dev/xvdo"
  76. volume_type = "gp3"
  77. # volume_size = xx
  78. delete_on_termination = true
  79. encrypted = true
  80. kms_key_id = data.aws_kms_key.ebs-key.arn
  81. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  82. }
  83. ebs_block_device {
  84. # /var/tmp
  85. device_name = "/dev/xvdp"
  86. volume_type = "gp3"
  87. # volume_size = xx
  88. delete_on_termination = true
  89. encrypted = true
  90. kms_key_id = data.aws_kms_key.ebs-key.arn
  91. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  92. }
  93. ebs_block_device {
  94. # /var/log
  95. device_name = "/dev/xvdq"
  96. volume_type = "gp3"
  97. # volume_size = xx
  98. delete_on_termination = true
  99. encrypted = true
  100. kms_key_id = data.aws_kms_key.ebs-key.arn
  101. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  102. }
  103. ebs_block_device {
  104. # /var/log/audit
  105. device_name = "/dev/xvdr"
  106. volume_type = "gp3"
  107. # volume_size = xx
  108. delete_on_termination = true
  109. encrypted = true
  110. kms_key_id = data.aws_kms_key.ebs-key.arn
  111. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  112. }
  113. ebs_block_device {
  114. # /tmp
  115. device_name = "/dev/xvds"
  116. volume_type = "gp3"
  117. # volume_size = xx
  118. delete_on_termination = true
  119. encrypted = true
  120. kms_key_id = data.aws_kms_key.ebs-key.arn
  121. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  122. }
  123. network_interface {
  124. device_index = 0
  125. network_interface_id = aws_network_interface.jira-server-interface.id
  126. }
  127. user_data = data.template_cloudinit_config.cloud-init.rendered
  128. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = "jira-server" })
  129. volume_tags = merge(local.standard_tags, var.tags, { Name = "jira-server" })
  130. }
  131. # Render a multi-part cloud-init config making use of the part
  132. # above, and other source files
  133. data "template_cloudinit_config" "cloud-init" {
  134. gzip = true
  135. base64_encode = true
  136. # Main cloud-config configuration file.
  137. part {
  138. filename = "init.cfg"
  139. content_type = "text/cloud-config"
  140. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  141. {
  142. hostname = "jira-server"
  143. fqdn = "jira-server.${var.dns_info["private"]["zone"]}"
  144. environment = var.environment
  145. salt_master = local.salt_master
  146. proxy = local.proxy
  147. aws_partition = var.aws_partition
  148. aws_partition_alias = var.aws_partition_alias
  149. aws_region = var.aws_region
  150. }
  151. )
  152. }
  153. # Additional parts as needed
  154. #part {
  155. # content_type = "text/x-shellscript"
  156. # content = "ffbaz"
  157. #}
  158. }
  159. module "private_dns_record_jira-server" {
  160. source = "../../../submodules/dns/private_A_record"
  161. name = "jira-server"
  162. ip_addresses = [aws_instance.jira-server-instance.private_ip]
  163. dns_info = var.dns_info
  164. reverse_enabled = var.reverse_enabled
  165. providers = {
  166. aws.c2 = aws.c2
  167. }
  168. }