-
Notifications
You must be signed in to change notification settings - Fork 3
/
ubuntu.ami.pkr.hcl
153 lines (133 loc) · 3.66 KB
/
ubuntu.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
packer {
required_plugins {
git = {
version = ">= v0.4.3"
source = "github.com/ethanmdavidson/git"
}
amazon = {
source = "github.com/hashicorp/amazon"
version = "~> 1"
}
}
}
variable "aws_region" {
type = string
description = "AWS Region"
default = "us-east-1"
}
variable "source_ami" {
type = string
description = "Default Ubuntu AMI to build our custom AMI"
default = "ami-053b0d53c279acc90" #Ubuntu 22.04 LTS
}
variable "ami_prefix" {
type = string
description = "AWS AMI name prefix"
default = "CSYE-7125"
}
variable "ssh_username" {
type = string
description = "username to ssh into the AMI Instance"
default = "ubuntu"
}
variable "subnet_id" {
type = string
description = "Subnet of the default VPC"
}
variable "OS" {
type = string
description = "Base operating system version"
default = "Ubuntu"
}
variable "ubuntu_version" {
type = string
description = "Version of the custom AMI"
default = "22.04 LTS"
}
variable "ami_users" {
type = list(string)
description = "List of users who will access the custom AMI"
}
variable "instance_type" {
type = string
description = "AWS AMI instance type"
default = "t2.micro"
}
variable "volume_type" {
type = string
description = "EBS volume type"
default = "gp2"
}
variable "volume_size" {
type = string
description = "EBS volume size"
default = "50"
}
variable "device_name" {
type = string
description = "EBS device name"
default = "/dev/sda1"
}
locals {
truncated_sha = substr(data.git-commit.cwd-head.hash, 0, 8)
version = data.git-repository.cwd.head == "master" && data.git-repository.cwd.is_clean ? var.ubuntu_version : "${var.ubuntu_version}-${local.truncated_sha}"
timestamp = substr(regex_replace(timestamp(), "[- TZ:]", ""), 8, 13)
}
data "git-repository" "cwd" {}
data "git-commit" "cwd-head" {}
source "amazon-ebs" "ubuntu" {
region = "${var.aws_region}"
ami_name = "${var.ami_prefix}-${local.truncated_sha} [${var.ubuntu_version}-${local.timestamp}]"
ami_description = "Ubuntu AMI for CSYE 7125 built by ${data.git-commit.cwd-head.author}"
tags = {
Name = "${var.ami_prefix}-${local.truncated_sha}"
Base_AMI_ID = "${var.source_ami}"
TimeStamp_ID = "${local.timestamp}"
OS_Version = "${var.OS}"
Release = "${var.ubuntu_version}"
Author = "${data.git-commit.cwd-head.author}"
}
ami_regions = [
"${var.aws_region}",
]
aws_polling {
delay_seconds = 120
max_attempts = 50
}
instance_type = "${var.instance_type}"
source_ami = "${var.source_ami}"
ssh_username = "${var.ssh_username}"
subnet_id = "${var.subnet_id}"
ami_users = "${var.ami_users}"
launch_block_device_mappings {
delete_on_termination = true
device_name = "${var.device_name}"
volume_size = "${var.volume_size}"
volume_type = "${var.volume_type}"
}
}
build {
sources = ["source.amazon-ebs.ubuntu"]
# https://www.packer.io/docs/provisioners/file#uploading-files-that-don-t-exist-before-packer-starts
provisioner "file" {
source = "./jenkins/plugins.txt"
destination = "/home/ubuntu/plugins.txt"
}
provisioner "file" {
source = "./configs.tgz"
destination = "/home/ubuntu/configs.tgz"
}
provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
"CHECKPOINT_DISABLE=1"
]
scripts = [
"./scripts/install.sh",
]
}
post-processor "manifest" {
output = "manifest.json"
strip_path = true
}
}