-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathami.pkr.hcl
88 lines (73 loc) · 2.15 KB
/
ami.pkr.hcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
packer {
required_version = ">= 1.8.0"
required_plugins {
amazon = {
source = "github.com/hashicorp/amazon"
version = "~> 1"
}
}
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
ami_prefix = "nomad-${var.nomad_version}"
}
variable "aws_region" {
type = string
default = "ap-south-1"
}
variable "dry_run" {
type = bool
default = true
}
variable "nomad_version" {
type = string
default = "1.6.1"
}
variable "install_docker" {
type = bool
default = false
}
// Get the most recent Ubuntu AMI ID.
data "amazon-ami" "autogenerated_1" {
filters = {
architecture = "x86_64"
"block-device-mapping.volume-type" = "gp2"
name = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
region = var.aws_region
}
source "amazon-ebs" "nomad" {
skip_create_ami = var.dry_run
# https://github.com/hashicorp/packer/issues/11656#issuecomment-1106564406
temporary_key_pair_type = "ed25519"
ami_description = "AMI for Nomad agents based on Ubuntu"
ami_name = var.install_docker ? "${local.ami_prefix}-docker-${local.timestamp}" : "${local.ami_prefix}-${local.timestamp}"
instance_type = "t2.micro"
shutdown_behavior = "terminate"
force_deregister = true
force_delete_snapshot = true
tags = {
nomad_version = var.nomad_version
source_ami_id = "{{ .SourceAMI }}"
source_ami_name = "{{ .SourceAMIName }}"
Name = var.install_docker ? "${local.ami_prefix}-docker" : "${local.ami_prefix}"
}
region = var.aws_region
source_ami = data.amazon-ami.autogenerated_1.id
ssh_username = "ubuntu"
}
build {
sources = ["source.amazon-ebs.nomad"]
// wait for sometime for instance to properly initialise.
provisioner "shell" {
inline = ["sleep 15"]
}
provisioner "shell" {
environment_vars = ["NOMAD_VERSION=${var.nomad_version}", "INSTALL_DOCKER=${var.install_docker}"]
script = "./setup.sh"
}
}