From 6ddceceed54231936db96c926440d58b239c00a2 Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Sun, 11 Sep 2016 00:34:40 +0100 Subject: [PATCH 1/2] Tag EBS volumes in addition to the AMI --- create_command.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/create_command.go b/create_command.go index 7940cb5..17842fc 100644 --- a/create_command.go +++ b/create_command.go @@ -151,11 +151,31 @@ func create(c CreateCommand) (string, error) { return snapshotId, errors.New("ERROR: Could not find the AMI just created.") } + ami := *respDscrImages.Images[0] + // If the AMI's status is failed throw an error - if *respDscrImages.Images[0].State == "failed" { + if *ami.State == ec2.ImageStateFailed { return snapshotId, errors.New("ERROR: AMI was created but entered a state of 'failed'. This is an AWS issue. Please re-run this command. Note that you will need to manually de-register the AMI in the AWS console or via the API.") } + // Tag each volume for the AMI as well so we can find them later + for _, blockDeviceMapping := range ami.BlockDeviceMappings { + if blockDeviceMapping != nil && blockDeviceMapping.Ebs != nil { + c.Ui.Output("==> Adding tags to EBS Volume Snapshot " + *blockDeviceMapping.Ebs.SnapshotId + " (" + *blockDeviceMapping.DeviceName + ") of AMI " + *ami.Name + "...") + _, err := svc.CreateTags(&ec2.CreateTagsInput{ + Resources: []*string{blockDeviceMapping.Ebs.SnapshotId}, + Tags: []*ec2.Tag{ + &ec2.Tag{ Key: aws.String(EC2_SNAPPER_INSTANCE_ID_TAG), Value: &c.InstanceId }, + &ec2.Tag{ Key: aws.String("Name"), Value: aws.String(c.AmiName + "-" + *blockDeviceMapping.DeviceName) }, + }, + }) + + if err != nil { + return snapshotId, err + } + } + } + // Announce success c.Ui.Info("==> Success! Created " + snapshotId + " named \"" + name + "\"") return snapshotId, nil From 6c9d6895d5d2ab10ae6e7940ae66f5128f8875aa Mon Sep 17 00:00:00 2001 From: Yevgeniy Brikman Date: Sun, 11 Sep 2016 08:29:56 +0100 Subject: [PATCH 2/2] Wait for EC2 instance to exist before tagging it --- integration_create_delete_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/integration_create_delete_test.go b/integration_create_delete_test.go index 806ccd2..66a399e 100644 --- a/integration_create_delete_test.go +++ b/integration_create_delete_test.go @@ -186,6 +186,11 @@ func launchInstance(svc *ec2.EC2, logger *log.Logger, t *testing.T) (*ec2.Instan instance := runResult.Instances[0] logger.Printf("Launched instance %s", *instance.InstanceId) + err = svc.WaitUntilInstanceExists(&ec2.DescribeInstancesInput{InstanceIds: []*string{instance.InstanceId}}) + if err != nil { + t.Fatal(err) + } + tagInstance(instance, instanceName, svc, logger, t) return instance, instanceName