- Create a packer project
- In your
<config>.json file similar to the following:
{
"builders": [
{
"ami_name" : "packer-bastion-{{timestamp}}",
"ami_regions" : [ "{{user `aws_region_secondary`}}" ],
"ami_users" : "{{user `aws_accounts`}}",
"instance_type" : "t2.micro",
"launch_block_device_mappings": [
{
"device_name": "/dev/xvdl",
"volume_size": "{{user `log_volume_size`}}",
"volume_type": "standard",
"delete_on_termination": true
}
],
"ami_block_device_mappings": [
{
"delete_on_termination": true,
"device_name": "/dev/xvdl",
"volume_size": "{{user `log_volume_size`}}",
"volume_type": "standard"
}
],
"name" : "{{user `company`}}_bastion",
"profile" : "{{user `profile`}}",
"region" : "{{user `aws_region_primary`}}",
"source_ami_filter": {
"filters": {
"name" : "{{user `aws_ami_name`}}",
"owner-alias" : "amazon",
"root-device-type" : "ebs"
},
"most_recent": true
},
"ssh_username" : "ec2-user",
"type" : "amazon-ebs",
"tags": {
"Name" : "{{isotime \"20060102-150405\"}}",
"release" : "{{user `environment`}}",
"version" : "{{user `version`}}"
}
}
],
"provisioners": [
{
"type" : "shell",
"inline" : [ "mkdir -p /tmp/packer/base/devops /tmp/packer/consul /tmp/packer/bastion" ]
},
{
"type" : "file",
"source" : "assets/base/",
"destination" : "/tmp/packer/base"
},
{
"type" : "file",
"source" : "assets/consul/",
"destination" : "/tmp/packer/consul"
},
{
"type" : "file",
"source" : "assets/bastion/",
"destination" : "/tmp/packer/bastion"
},
{
"type" : "shell",
"environment_vars": [
"CONSUL_VERSION={{user `consul_version`}}"
],
"execute_command" : "echo '' | sudo -S su - -c '{{ .Vars }} {{ .Path }}'",
"scripts" : [
"provisioners/base.sh"
]
}
]
}
- Within the
assets/base/devops folder, include a file named utils.sh by using touch assets/base/devops/utils.sh.
- In your
provisioners/base.sh file copy the folder /tmp/packer/base/devops to another location such as /usr/local/bin such that you have the end result of /usr/local/bin/devops/utils.sh.
- Set the file mode bits using
chmod 0750 /usr/local/bin/devops/*.sh
Example:
echo "> Move & chmod devops scripts"
mv /tmp/packer/base/devops /usr/local/bin/
chmod 750 /usr/local/bin/devops/*.sh
- Run packer using a command like
packer build -var-file=vars.json -var environment=test bastion.json
- Packer will run, until it completes and successfully exists
provisioners/base.sh then die with the error:
Build 'bastion' errored: Error removing temporary script at /tmp/script_8544.sh!