Advertisement
Guest User

Untitled

a guest
Dec 20th, 2024
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. packer {
  2. required_plugins {
  3. amazon = {
  4. version = ">= 1.2.2"
  5. source = "github.com/hashicorp/amazon"
  6. }
  7. ansible = {
  8. version = "~> 1"
  9. source = "github.com/hashicorp/ansible"
  10. }
  11. }
  12. }
  13.  
  14. locals {
  15. timestamp_date = "${formatdate("YYYY-MM-DD", local.timestamp)}"
  16. timestamp_full = "${formatdate("YYYY-MM-DD_HH:mm:ss", local.timestamp)}"
  17.  
  18. # Template the description string
  19. ami_description = format(
  20. "STIG-partitioned [HARDENED], LVM-enabled, \"minimal\", with updates through %s. Default username `%s`",
  21. formatdate("YYYY-MM-DD", local.timestamp), "ec2-user
  22. )
  23.  
  24. # Calculate AWS AMI deprecate_at timestamp
  25. aws_ami_deprecate_at = var.deprecation_lifetime != null ? timeadd(local.timestamp, var.deprecation_lifetime) : null
  26.  
  27. timestamp = timestamp()
  28. }
  29.  
  30. source "amazon-ebssurrogate" "base" {
  31. ami_root_device {
  32. source_device_name = "/dev/sdf"
  33. device_name = "/dev/sda1"
  34. delete_on_termination = true
  35. volume_size = var.root_volume_size
  36. volume_type = "gp3"
  37. }
  38. ami_name = var.ami_name
  39. ami_description = local.ami_description
  40. ami_virtualization_type = "hvm"
  41. associate_public_ip_address = false
  42. communicator = "ssh"
  43. ena_support = true
  44. deprecate_at = local.aws_ami_deprecate_at
  45. force_deregister = var.aws_force_deregister
  46. force_delete_snapshot = var.aws_force_delete_snapshot
  47. instance_type = var.aws_instance_type
  48. iam_instance_profile = var.aws_instance_role
  49. launch_block_device_mappings {
  50. delete_on_termination = true
  51. device_name = "/dev/sda1"
  52. volume_size = var.root_volume_size
  53. volume_type = "gp3"
  54. }
  55. launch_block_device_mappings {
  56. delete_on_termination = true
  57. device_name = "/dev/sdf"
  58. volume_size = var.root_volume_size
  59. volume_type = "gp3"
  60. }
  61. ami_block_device_mappings {
  62. device_name = "/dev/sda1"
  63. delete_on_termination = true
  64. volume_size = var.root_volume_size
  65. volume_type = "gp3"
  66. }
  67. max_retries = 30
  68. region = var.aws_region
  69. sriov_support = true
  70. ssh_interface = var.aws_ssh_interface
  71. ssh_port = 22
  72. ssh_pty = true
  73. ssh_timeout = "15m"
  74. ssh_username = var.ssh_username
  75. ssh_key_exchange_algorithms = [
  76. "ecdh-sha2-nistp521",
  77. "ecdh-sha2-nistp256",
  78. "ecdh-sha2-nistp384",
  79. "ecdh-sha2-nistp521",
  80. "diffie-hellman-group14-sha1",
  81. "diffie-hellman-group1-sha1"
  82. ]
  83. temporary_security_group_source_cidrs = var.aws_temporary_security_group_source_cidrs
  84. user_data_file = "${path.root}/userdata/userdata.cloud"
  85. vpc_id = var.aws_vpc_id
  86. subnet_id = var.aws_subnet_id
  87. # enforces imdsv2 support on the running instance being provisioned by Packer
  88. metadata_options {
  89. http_endpoint = "enabled"
  90. http_tokens = "required"
  91. http_put_response_hop_limit = 1
  92. }
  93.  
  94. ##
  95. ## Tags applied to the Runtime Instance
  96. ##
  97. run_tags = {
  98. Name = "Packer_Builder_${var.ami_name}-${local.timestamp_full}"
  99. Base_AMI_ID = "{{ .SourceAMI }}"
  100. Base_AMI_Name = "{{ .SourceAMIName }}"
  101. OS = "RHEL 8"
  102. }
  103.  
  104. ##
  105. ## Tags applied to the AMI
  106. ##
  107. tags = {
  108. Name = "${var.ami_name}"
  109. Base_AMI_ID = "{{ .SourceAMI }}"
  110. Base_AMI_Name = "{{ .SourceAMIName }}"
  111. OS = "RHEL 8"
  112. }
  113.  
  114. ##
  115. ## Search for AMI based on AMI Name
  116. ##
  117. source_ami_filter {
  118. filters = {
  119. name = var.aws_ami_search_name
  120. virtualization-type = "hvm"
  121. root-device-type = "ebs"
  122. }
  123. owners = [var.aws_ami_search_owner]
  124. most_recent = true
  125. }
  126. }
  127.  
  128. build {
  129. sources = ["source.amazon-ebssurrogate.base"]
  130.  
  131. provisioner "shell" {
  132. execute_command = "{{ .Vars }} sudo -E /bin/bash '{{ .Path }}'"
  133. inline = [
  134. "sudo yum install -y lvm2 nvme-cli"
  135. ]
  136. }
  137.  
  138. provisioner "shell" {
  139. execute_command = "{{ .Vars }} sudo -E /bin/bash '{{ .Path }}'"
  140. script = "${path.root}/scripts/volume.sh"
  141. timeout = "5m"
  142. expect_disconnect = true
  143. }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement