Created
November 20, 2015 00:53
-
-
Save jwreagor/cc6b9597abe7aa45c439 to your computer and use it in GitHub Desktop.
Example of the AWS SDK v1.0 release...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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