Packer by Hashicorp (https://www.packer.io/) is used to create AWS AMI (Amazon Machine Image) which are used as images that instances are spun up from. Packer allows you to take a base image and provision it as required. Packer uses json so you can't add comments to your packer files which is a bit annoying. I have commented the packer elements in the example below.
Packer will spin up the 'source_ami' specified and connect with ssh to execute the commands in the 'provisioners' section of the file. The new AMI is created from this instance once all the commands have been run. You can see this instance in the AWS Console which is then immediately terminated once Packer has finished working.
You can see the created AMIs in the AWS Console. Go to
Services - EC2 - AMIs (Left Panel)
Define a set of variables at the top of the file that are easily changed. This way you don't have to hunt through the file to find an instances of these variables that need to be altered later.
{
"variables": {
"region": "<region>",
// This uses the profile from the .aws/credentials file
"profile": "<profile>",
// The base ami that you are starting from
"source_ami": "<base ami>",
// The optional VPC (virtual private cloud) and subnet that you want this ami to be part of
"vpc_id": "<vpc>",
"subnet_id": "<subnet>"
},
"builders": [
{
"ami_name": "<name of the ami created>",
"ami_description": "<description>",
// How is the communication with the packer instance going to be established
"communicator": "ssh",
// Force any AMI with the same name to be removed ('deregistered')
"force_deregister": true,
"instance_type": "t2.micro",
// Use the parameters which are defined in the 'variables' section above
"profile": "{{user `profile`}}",
"region": "{{user `region`}}",
"source_ami": "{{user `source_ami`}}",
"ssh_pty": true,
"ssh_username": "<ssh username that you are going to connect as>",
"subnet_id": "{{user `subnet_id`}}",
"type": "amazon-ebs",
"vpc_id": "{{user `vpc_id`}}"
}
],
// The provisioners section that adds additional files, installs etc to the AMI that is going to be created
"provisioners": [
// This first provisioner installs wget
{
"type": "shell",
"inline": [
"sudo yum update -y",
"sudo yum -y install wget"
]
},
// Perhaps also install java afterwards?
{
"type": "shell",
"inline": [
"sudo yum -y install java-1.8.0-openjdk-devel"
]
},
]
}
No comments:
Post a Comment