Dynamic source ami#3817
Conversation
| } | ||
|
|
||
| ui.Say("Launching a source AWS instance...") | ||
| imageResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ |
There was a problem hiding this comment.
Previously we made the DescribeImages call twice, rather than using our state bag. This now uses the result in the state bag.
|
This should also cover |
builder/amazon/common/run_config.go
Outdated
| if c.SourceAmi == "" { | ||
| errs = append(errs, errors.New("A source_ami must be specified")) | ||
| if c.SourceAmi == "" && c.SourceAmiFilter.Empty() { | ||
| errs = append(errs, errors.New("A source_ami or dynamic_source_ami must be specified")) |
There was a problem hiding this comment.
Fixed, good catch!
|
We currently use source AMI passed from ENV to achieve this, having an inbuilt aws-ami-filter would be amazing for our use-case. |
|
This there anything else I can help with / do here? :) |
| ``` | ||
| This selects the most recent Ubuntu 16.04 HVM EBS AMI from Canonical. | ||
|
|
||
| - `source_ami_filter.filters` (map of strings) - filters used to select a `source_ami`. |
There was a problem hiding this comment.
This should be indented and only say filters as key, see ami_block_device_mappings. The same goes for owners, and most_recent.
| This is helpful to limit the AMIs to a trusted third party, or to your own account. | ||
|
|
||
| - `source_ami_filter.most_recent` (bool) - Selects the newest created image when true. | ||
| This is most useful for selecting a daily distro build. |
There was a problem hiding this comment.
This should state that it fail unless exactly one AMI is found, like in Terraform.
|
This looks great! But I would also like to have this on |
|
@rickard-von-essen 0e26376 should achieve the doc enhancements you pointed out. |
|
👍 Maybe you are working on this but, the |
|
Good catch. I spaced on that part since it has been a while ( I thought it was already part of the common flow). I'll fix that up too. |
|
I've updated the other two amazon builders. I'm not as familiar with them so I didn't run a complete usage test for them. One wrinkle was that the chroot builder doesn't use I've also checked the "Allow editors from maintainers." check box. Feel free to push any clean up / fixes to this branch. Thank you for taking a look at this! |
|
Super! I'll try to do some testing tomorrow. |
mwhooker
left a comment
There was a problem hiding this comment.
This is great. Besides a few comments, this lgtm. I want to wait for either @rickard-von-essen or myself to test these before merging, but looks super useful
|
|
||
| if len(imageResp.Images) != 1 { | ||
| state.Put("error", fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI)) | ||
| image, ok := state.Get("source_image").(*ec2.Image) |
There was a problem hiding this comment.
I think it's ok to let go panic here. Existing practice is
image := state.Get("source_image").(*ec2.Image)
There was a problem hiding this comment.
Yeah, I hit a panic when writing this and realized I should handle the error.
As a new comer to golang seeing panics were a little overwhelming.
My intention here was to provide a fairly easy debug point in the future.
This shouldn't ever happen since "we" are the ones putting the object in the shared state. I think I hit this because I had an ec2.Image instead of *ec2.Image.
It seems like since we have the above, there isn't too much to gain by removing it.
Perhaps we could improve the error message though?
I guess, to phrase it differently,
Yeah, it probably is fine, since this should get hit. If we have it though, is it worth removing?
There was a problem hiding this comment.
nah probably not worth removing
| } | ||
| ``` | ||
| This selects the most recent Ubuntu 16.04 HVM EBS AMI from Canonical. | ||
| NOTE: This will fail unless *exactly* one AMI is returned. |
There was a problem hiding this comment.
won't this succeed even if more than one image is returned since most_recent is true?
There was a problem hiding this comment.
Yeah, let me clarify the docs to say that most_recent will work.
|
@mwhooker If this is in an acceptable state, would it be possible to get this into the v0.11 release this month then? |
| func (a imageSort) Len() int { return len(a) } | ||
| func (a imageSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] } | ||
| func (a imageSort) Less(i, j int) bool { | ||
| itime, _ := time.Parse(time.RFC3339, *a[i].CreationDate) |
There was a problem hiding this comment.
also not a huge issue, but RFC3339 times are lexicographically sortable
5.1. Ordering
If date and time components are ordered from least precise to most
precise, then a useful property is achieved. Assuming that the time
zones of the dates and times are the same (e.g., all in UTC),
expressed using the same string (e.g., all "Z" or all "+00:00"), and
all times have the same number of fractional second digits, then the
date and time strings may be sorted as strings (e.g., using the
strcmp() function in C) and a time-ordered sequence will result. The
presence of optional punctuation would violate this characteristic.
There was a problem hiding this comment.
Yeah, This was direct from terraform, so we'll be consistent :)
|
@jrnt30 it's possible, but probably not. there's a lot I need to get done for 0.11 and I'm not sure i'll have time to test this. If you want to help by testing the PR, I might feel more confident merging it in for 0.11, however. I'm mostly looking to test that it's backwards compatible, ie existing builders work, and to test a few of the edge cases, like how it behaves if the filters return a huge number of images with mostRecent = true. |
|
@mwhooker I have done some basic testing. It returns the ami I expect and bails when I have I haven't found any performance issues when doing stupid things like: "source_ami_filter": {
"filters": {
"name": "*x86_64*",
"virtualization-type": "hvm",
"root-device-type": "ebs"
},
"most_recent": [true|false]
}, |
|
@mwhooker If you don't want to do any more testing LGTM. |
|
Lgtm. Feel free to merge in, or I can when I get back next week
|
|
Thank you all! |
|
0.11 is already out. Should make it in 0.11.1 which will be next release
|
|
Actually I misspoke before. 0.12 is the next release this will be in
|
|
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Example packer file:
This adds a new option,
source_ami_filterwhich will use the AWS API to find an image at build time. This allows users to hit a moving target, while keeping the configuration in packer, rather than in a bash script.This is desirable as it allows one to get the latest kernels, packages, and security fixes.
This effectively makes the
source_amia simple option. One could specify it insidesource_ami_filtersand achieve the same result.Closes #2756