Skip to content

Instantly share code, notes, and snippets.

@jwreagor
Created November 20, 2015 00:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwreagor/cc6b9597abe7aa45c439 to your computer and use it in GitHub Desktop.
Save jwreagor/cc6b9597abe7aa45c439 to your computer and use it in GitHub Desktop.
Example of the AWS SDK v1.0 release...
// Create a session
s := session.New(aws.NewConfig().WithRegion("us-west-2"))
// Add a handler to print every API request for the session
s.Handlers.Send.PushFront(func(r *request.Request) {
fmt.Printf("Request: %s/%s\n", r.ClientInfo.ServiceName, r.Operation)
})
// We want to start all instances in a VPC, so let's get their IDs first.
ec2client := ec2.New(s)
var instanceIDsToStart []*string
describeInstancesInput := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("vpc-id"),
Values: aws.StringSlice([]string{"vpc-82977de9"}),
},
},
}
// Use a paginator to easily iterate over multiple pages of response
ec2client.DescribeInstancesPages(describeInstancesInput,
func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
// Use JMESPath expressions to query complex structures
ids, _ := awsutil.ValuesAtPath(page, "Reservations[].Instances[].InstanceId")
for _, id := range ids {
instanceIDsToStart = append(instanceIDsToStart, id.(*string))
}
return !lastPage
})
// The SDK provides several utility functions for literal <--> pointer transformation
fmt.Println("Starting:", aws.StringValueSlice(instanceIDsToStart))
// Skipped for brevity here, but *always* handle errors in the real world :)
ec2client.StartInstances(&ec2.StartInstancesInput{
InstanceIds: instanceIDsToStart,
})
// Finally, use a waiter function to wait until the instances are running
ec2client.WaitUntilInstanceRunning(describeInstancesInput)
fmt.Println("Instances are now running.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment