NOTE: We use HCL2 (Hashicorp Configuration Language 2) syntax for creating AMI (Amazon Machine Image) in this example. It can be written in json template as well.
# Before you can build the AMI, you need to provide your AWS credentials to Packer. These credentials have permissions to create, modify and delete EC2 instances. Refer to the
export AWS_PROFILE=aws-stage
# Starting from version 1.7, Packer supports a new packer init command allowing automatic installation of Packer plugins.
packer init .
# Format your template. Packer will print out the names of the files it modified
packer fmt .
# If Packer detects any invalid configuration, Packer will print out the file name, the error type and line number of the invalid configuration
packer validate .
packer build -var-file="ubuntu-stage.pkrvars.hcl" -var ssh_private_key_file=~/.ssh/acharolia-ireland.pem .
Visit the AWS AMI page to verify that Packer successfully built your AMI.
After running the above command, your AWS account now has an AMI associated with it. AMIs are stored in S3 by Amazon so you may be charged.