Skip to content

Instantly share code, notes, and snippets.

@shaftoe
Last active January 23, 2021 13:17
Show Gist options
  • Save shaftoe/8177edf8cc72d132c9e510f813ea83a2 to your computer and use it in GitHub Desktop.
Save shaftoe/8177edf8cc72d132c9e510f813ea83a2 to your computer and use it in GitHub Desktop.
aws-sdk-go-v2 service/ec2: VolumeAvailableWaiter.Wait() doesn't seem to work as expected
package main
import (
"context"
"log"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
// stringPtr returns a pointer to the passed string.
func stringPtr(s string) *string {
return &s
}
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("Unable to load SDK config: %v", err)
}
client := ec2.NewFromConfig(cfg)
az := "us-east-2a"
log.Println("Create a new volume in", az)
volumeOpts := &ec2.CreateVolumeInput{
AvailabilityZone: &az,
VolumeType: types.VolumeTypeStandard,
Size: 1,
TagSpecifications: []types.TagSpecification{
{
Tags: []types.Tag{
{Key: stringPtr("Name"), Value: stringPtr("TO REMOVE")},
{Key: stringPtr("Owner"), Value: stringPtr("shaftoe")},
},
ResourceType: types.ResourceTypeVolume,
},
},
}
volume, err := client.CreateVolume(context.TODO(), volumeOpts)
if err != nil {
log.Fatalf("Failure creating new volume: %v", err)
}
log.Println("Volume", *volume.VolumeId, "creation request sent")
timeout := time.Minute * 5
log.Println("Waiting for creation process to complete in less than", timeout)
err = ec2.NewVolumeAvailableWaiter(client).Wait(
context.TODO(),
&ec2.DescribeVolumesInput{
DryRun: true,
VolumeIds: []string{*volume.VolumeId},
},
timeout,
)
if err != nil {
log.Fatalf("Waiter error: %v", err)
}
log.Println("Volume creation completed")
}
// this creates the volume as expected but fails at line 58:
// Waiter error: exceeded max wait time for VolumeAvailable waiter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment