diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 1960402cb..1f85daf24 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -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! diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index 5f75fb423..66b22944a 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,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 } diff --git a/website/source/docs/builders/amazon-ebs.html.markdown b/website/source/docs/builders/amazon-ebs.html.markdown index 4c47a4de7..2ea9d4d8d 100644 --- a/website/source/docs/builders/amazon-ebs.html.markdown +++ b/website/source/docs/builders/amazon-ebs.html.markdown @@ -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" + } }