Merge pull request #233 from jmassara/tags

builder/amazon/ebs: Added tagging support for amazon/ebs AMIs
This commit is contained in:
Mark Peek 2013-08-06 15:01:32 -07:00
commit 413fc1b73a
3 changed files with 40 additions and 3 deletions

View File

@ -27,6 +27,12 @@ type config struct {
// Configuration of the resulting AMI
AMIName string `mapstructure:"ami_name"`
// Tags for the AMI
Tags map[string]string
// Unexported fields that are calculated from others
ec2Tags []ec2.Tag
}
type Builder struct {
@ -57,6 +63,15 @@ func (b *Builder) Prepare(raws ...interface{}) error {
}
}
// Convert Tags to ec2.Tag
if b.config.Tags != nil {
var ec2Tags []ec2.Tag
for key, value := range b.config.Tags {
ec2Tags = append(ec2Tags, ec2.Tag{key, value})
}
b.config.ec2Tags = ec2Tags
}
if errs != nil && len(errs.Errors) > 0 {
return errs
}
@ -107,7 +122,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
&common.StepProvision{},
&stepStopInstance{},
&stepCreateAMI{},
&stepCreateAMI{
Tags: b.config.ec2Tags,
},
}
// Run!

View File

@ -12,7 +12,9 @@ import (
"time"
)
type stepCreateAMI struct{}
type stepCreateAMI struct {
Tags []ec2.Tag
}
type amiNameData struct {
CreateTime string
@ -64,6 +66,18 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
return multistep.ActionHalt
}
// Add tags to AMI
if s.Tags != nil {
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", createResp.ImageId))
_, err := ec2conn.CreateTags([]string{createResp.ImageId}, s.Tags)
if err != nil {
err := fmt.Errorf("Error adding tags to AMI (%s): %s", createResp.ImageId, err)
state["error"] = err
ui.Error(err.Error())
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}

View File

@ -81,6 +81,8 @@ Optional:
* `vpc_id` (string) - If launching into a VPC subnet, Packer needs the
VPC ID in order to create a temporary security group within the VPC.
* `tags` (array of key/value pairs) - Tags applied to the AMI.
## Basic Example
Here is a basic example. It is completely valid except for the access keys:
@ -94,7 +96,11 @@ Here is a basic example. It is completely valid except for the access keys:
"source_ami": "ami-de0d9eb7",
"instance_type": "t1.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-quick-start {{.CreateTime}}"
"ami_name": "packer-quick-start {{.CreateTime}}",
"tags": {
"myTagName1": "myTagValue1",
"myTagName2": "myTagValue2"
}
}
</pre>