Making packer compatible with multiple AWS regions

3,252 views
Skip to first unread message

kenj...@gmail.com

unread,
Nov 19, 2015, 12:20:11 AM11/19/15
to Packer
Hi,

I would like to use a single packer template to create an Amazon AMI based on AWS region of choice. However since AMI ID used in each region is different for the same image, this makes it difficult.

For example if I am to use Amazon Linux AMI 2015.09.1 (HVM) in Sydney, the AMI ID is ami-48d38c2b whereas the same image in North California uses the AMI ID of ami-d5ea86b5.

Is there any way to map region and corresponding AMI so that when I build AMI, I could run something like;

$ packer build -var 'region=ap-southeast-2' example.json ( create AMI in Sydney )
$ packer build -var 'region=us-west-1' example.json ( create AMI in North California )

Hubert Liu

unread,
Nov 20, 2015, 5:18:23 PM11/20/15
to Packer
Are you building the same image in each region?

If so - you can specify a "ami_regions" key to copy the resulting AMI to each region.

Example:
"builders": [
    {
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "us-east-1",
      "source_ami": "ami-cf35f3a4",
      "instance_type": "m4.large",
      "ssh_username": "ubuntu",
      "ami_name": "example-built",
      "subnet_id": "<your_subnet_id>",
      "security_group_id": "<your_sg_id>",
      "force_deregister": true,
      "ami_regions": [
        "us-east-1",
        "us-west-1",
        "us-west-2",
        "eu-west-1",
        "eu-central-1",
        "sa-east-1",
        "ap-northeast-1",
        "ap-southeast-1",
        "ap-southeast-2"
      ]
    }
  ]

Hopefully that helps!

Hubert Liu

unread,
Nov 20, 2015, 5:20:09 PM11/20/15
to Packer
This will be the result of the packer build:

amazon-ebs: Avoiding copying AMI to duplicate region us-east-1

amazon-ebs: Copying to: us-west-1

amazon-ebs: Copying to: us-west-2

amazon-ebs: Copying to: eu-west-1

amazon-ebs: Copying to: eu-central-1

amazon-ebs: Copying to: sa-east-1

amazon-ebs: Copying to: ap-northeast-1

amazon-ebs: Copying to: ap-southeast-1

amazon-ebs: Copying to: ap-southeast-2


amazon-ebs: Waiting for all copies to complete...

kenj...@gmail.com

unread,
Nov 23, 2015, 5:03:26 PM11/23/15
to Packer
Hi Hubert,

Thank you very much for the response. 

Yes and no, I would like to use Amazon Linux September 2015 version but in the meantime would like to avoid copying the resulting AMI across the different regions in order to avoid data transfer charge if possible.

David Zanetti

unread,
Nov 24, 2015, 4:49:55 PM11/24/15
to Packer
I suspect you want to find by name? There is no indication on an AMI of it being related to other ami other than Source, which I'm not sure is reliable anyway. If it's just find by name, then using `aws ec2 describe-images` against the target region and filtering by name will give you AMI IDs you can plug into packer as a variable on run.

For example, if I want the AMI IDs for "ubuntu-trusty-daily-amd64-server-20151119", then I can do:

$ for i in `aws ec2 describe-regions | jq -r '.Regions[].RegionName'`; do echo $i:`aws ec2 describe-images --region $i --filter "Name=name,Values=ubuntu/images-testing/ebs/ubuntu-trusty-daily-amd64-server-20151119" | jq -r '.Images[0].ImageId'` ; done
eu-west-1:ami-d2e045a1
ap-southeast-1:ami-53bf7e30
ap-southeast-2:ami-9f762ffc
eu-central-1:ami-5ec2d032
ap-northeast-1:ami-c298bbac
us-east-1:ami-e27f3b88
sa-east-1:ami-031ba16f
us-west-1:ami-9ddcb2fd
us-west-2:ami-20263641

(Note: this depends on AWS CLI tools and jq being installed)
Reply all
Reply to author
Forward
0 new messages