diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 805a34c4b..89c3cf601 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -3,6 +3,7 @@ package common import ( "errors" "fmt" + "github.com/mitchellh/goamz/ec2" "time" ) @@ -18,6 +19,7 @@ type RunConfig struct { SecurityGroupId string `mapstructure:"security_group_id"` SubnetId string `mapstructure:"subnet_id"` VpcId string `mapstructure:"vpc_id"` + Tags []ec2.Tag // Unexported fields that are calculated from others sshTimeout time.Duration diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index da51c264e..7158e22df 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -107,7 +107,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, &common.StepProvision{}, &stepStopInstance{}, - &stepCreateAMI{}, + &stepCreateAMI{ + Tags: b.config.Tags, + }, } // Run! diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index 5f75fb423..facb38374 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -12,7 +12,9 @@ import ( "time" ) -type stepCreateAMI struct{} +type stepCreateAMI struct { + Tags []ec2.Tag +} type amiNameData struct { CreateTime string @@ -64,6 +66,19 @@ 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("Add tags to AMI (%s)...", createResp.ImageId)) + amiId := []string{createResp.ImageId} + _, err := ec2conn.CreateTags(amiId, 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 }