-
Notifications
You must be signed in to change notification settings - Fork 1
/
ami.pkr.hcl
executable file
·161 lines (140 loc) · 3.97 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
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
154
155
156
157
158
159
160
161
packer {
required_plugins {
git = {
version = ">=v0.3.2"
source = "github.com/ethanmdavidson/git"
}
}
}
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-08c40ec9ead489470" #Ubuntu 22.04 LTS
}
variable "ami_prefix" {
type = string
description = "AWS AMI name prefix"
default = "ami_prefix"
}
variable "ssh_username" {
type = string
description = "username to ssh into the AMI Instance"
default = "username"
}
variable "subnet_id" {
type = string
description = "Subnet of the default VPC"
default = "subnet-03d1bcaedaf95a150"
}
variable "OS" {
type = string
description = "Base operating system version"
default = "OS"
}
variable "ubuntu_version" {
type = string
description = "Version of the custom AMI"
default = "ubuntu-version"
}
variable "dev_id" {
type = string
description = "AWS dev account ID"
default = "54321"
}
variable "prod_id" {
type = string
description = "AWS prod account ID"
default = "12345"
}
variable "instance_type" {
type = string
description = "AWS AMI instance type"
default = "t3.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" "ec2" {
region = "${var.aws_region}"
ami_name = "${var.ami_prefix}-${local.truncated_sha} [${var.ubuntu_version}-${local.timestamp}]"
ami_description = "Linux AMI for CSYE 6225 built by ${data.git-commit.cwd-head.author}"
ami_users = [
"${var.dev_id}", # dev account ID
"${var.prod_id}", # prod account ID
]
tags = {
Name = "${var.ami_prefix}-${local.truncated_sha}"
Base_AMI_ID = "${var.source_ami}"
TimeStamp_ID = "${local.timestamp}"
OS_Version = "${var.OS}"
Release = "22.04 LTS"
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}"
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.ec2"]
# https://www.packer.io/docs/provisioners/file#uploading-files-that-don-t-exist-before-packer-starts
provisioner "file" {
source = "webapp.zip" # path in local system to a tar.gz file
destination = "/home/ubuntu/webapp.zip" # path in the AMI to store the webapp
}
provisioner "file" {
source = "nodeserver.service" # path in local system to a tar.gz file
destination = "/home/ubuntu/nodeserver.service" # path in the AMI to store the webapp
}
provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
"CHECKPOINT_DISABLE=1"
]
scripts = [
"./scripts/install.sh",
"./scripts/post-install.sh",
]
}
post-processor "manifest" {
output = "manifest.json"
strip_path = true
}
}