With Packer installed, let's just dive right into it and build our first image.
Our first image will be an [Amazon EC2 AMI](https://aws.amazon.com/ec2/) with
Redis pre-installed. This is just an example. Packer can create images for [many
platforms][platforms] with anything pre-installed.
Our first image will be an [Amazon EC2 AMI](https://aws.amazon.com/ec2/) This is just an example. Packer can create images for [many platforms][platforms].
If you don't have an AWS account, [create one now](https://aws.amazon.com/free/).
For the example, we'll use a "t2.micro" instance to build our image, which
@ -200,4 +198,273 @@ image was pretty useless in this case (nothing was changed about it), this page
should've given you a general idea of how Packer works, what templates are and
how to validate and build templates into machine images.
## Some more examples:
### Another Linux Example, with provisioners:
Create a file named `welcome.txt` and add the following:
```
WELCOME TO PACKER!
```
Create a file named `example.sh` and add the following:
```
#!/bin/bash
echo "hello
```
Set your access key and id as environment variables, so we don't need to pass them in through the command line:
```
export AWS_ACCESS_KEY_ID=MYACCESSKEYID
export AWS_SECRET_ACCESS_KEY=MYSECRETACCESSKEY
```
Now save the following text in a file named `firstrun.json`:
Note that this example provides a `source_ami` instead of a `source_ami_filter` -- this means the example may be out of date by the time you try to use it, but it is provided here so you can see what it looks like to use an ami by name.
Your output will look like this:
```
amazon-ebs output will be in this color.
==> amazon-ebs: Prevalidating AMI Name: packer-linux-aws-demo-1507231105
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="0"}'
winrm set winrm/config '@{MaxTimeoutms="7200000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service '@{MaxConcurrentOperationsPerUser="12000"}'
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/client/auth '@{Basic="true"}'
net stop winrm
set-service winrm -startupType automatic
# Finally, allow WinRM connections and start the service
netsh advfirewall firewall set rule name="WinRM" new action=allow
net start winrm
</powershell>
```
Save the above code in a file named `bootstrap_win.txt`.
The example config below shows the two different ways of using the powershell provisioner: `inline` and `script`.
The first example, `inline`, allows you to provide short snippets of code, and will create the script file for you. The second example allows you to run more complex code by providing the path to a script to run on the guest vm.
Here's an example of a `sample_script.ps1` that will work with the environment variables we will set in our packer config; copy the contents into your own `sample_script.ps1` and provide the path to it in your packer config:
```
Write-Output("PACKER_BUILD_NAME is automatically set for you, or you can set it in your builder variables; the default for this builder is: " + $Env:PACKER_BUILD_NAME )
Write-Output("Remember that escaping variables in powershell requires backticks; for example VAR1 from our config is " + $Env:VAR1 )
Write-Output("Likewise, VAR2 is " + $Env:VAR2 )
Write-Output("and VAR3 is " + $Env:VAR3 )
```
Next you need to create a packer config that will use this bootstrap file. See the example below, which contains examples of using source_ami_filter for windows in addition to the powershell and windows-restart provisioners:
==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-east-1: ami-2970b753
```
And if you navigate to your EC2 dashboard you should see your shiny new AMI.
##FAQs:
####Where did you get the windows source AMI from?
If you click the "AMIs" option under "Images" on the lefthand side of your EC2 dashboard, you'll get a view of all of you AMIs. There is a toggle in the filter bar that allows you to switch from "Owned by me" to "Public Images". From there, you can apply filters like `Owner : Amazon images` and `Platform : Windows` and do a keyword search for the particular flavor of windows you're interested in.