Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save josephsmartinez/3973f9b74236b8b1695a0ae8ecca29db to your computer and use it in GitHub Desktop.
Save josephsmartinez/3973f9b74236b8b1695a0ae8ecca29db to your computer and use it in GitHub Desktop.
golang aws: examples

AWS Golang SDK examples

package main
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func main() {
ec2svc := ec2.New(session.New())
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Environment"),
Values: []*string{aws.String("prod")},
},
{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running"), aws.String("pending")},
},
},
}
resp, err := ec2svc.DescribeInstances(params)
if err != nil {
fmt.Println("there was an error listing instances in", err.Error())
log.Fatal(err.Error())
}
for idx, res := range resp.Reservations {
fmt.Println(" > Reservation Id", *res.ReservationId, " Num Instances: ", len(res.Instances))
for _, inst := range resp.Reservations[idx].Instances {
fmt.Println(" - Instance ID: ", *inst.InstanceId)
}
}
}
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
s3svc := s3.New(session.New())
result, err := s3svc.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
fmt.Println("Failed to list buckets", err)
return
}
fmt.Println("Buckets:")
for _, bucket := range result.Buckets {
fmt.Printf("%s : %s\n", aws.StringValue(bucket.Name), bucket.CreationDate)
}
}
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
s3svc := s3.New(session.New())
inputparams := &s3.ListObjectsInput{
Bucket: aws.String("eferro-bucket"),
MaxKeys: aws.Int64(10),
}
pageNum := 0
s3svc.ListObjectsPages(inputparams, func(page *s3.ListObjectsOutput, lastPage bool) bool {
fmt.Println("Page", pageNum)
pageNum++
for _, value := range page.Contents {
fmt.Println(*value.Key)
}
fmt.Println("pageNum", pageNum, "lastPage", lastPage)
// return if we should continue with the next page
return true
})
}
package main
import (
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)
func main() {
sqssvc := sqs.New(session.New())
params := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String("https://sqs.<region>.amazonaws.com/<acc_number>/<queue_name>"),
AttributeNames: []*string{
aws.String("ApproximateNumberOfMessages"),
aws.String("ApproximateNumberOfMessagesDelayed"),
aws.String("ApproximateNumberOfMessagesNotVisible"),
},
}
resp, _ := sqssvc.GetQueueAttributes(params)
for attrib, _ := range resp.Attributes {
prop := resp.Attributes[attrib]
i, _ := strconv.Atoi(*prop)
fmt.Println(attrib, i)
}
}
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)
func main() {
sqssvc := sqs.New(session.New())
params := &sqs.ListQueuesInput{
QueueNamePrefix: aws.String("prod-"),
}
sqs_resp, err := sqssvc.ListQueues(params)
if err != nil {
fmt.Println(err.Error())
return
}
for _, url := range sqs_resp.QueueUrls {
fmt.Println(*url)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment