Skip to content

Commit

Permalink
Merge pull request #12 from josh-padnick/ebs-snapshot
Browse files Browse the repository at this point in the history
Tag EBS volumes in addition to the AMI
  • Loading branch information
brikis98 authored Sep 11, 2016
2 parents ed3458b + 6c9d689 commit 3b1831d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 21 additions & 1 deletion create_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions integration_create_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3b1831d

Please sign in to comment.