Last active
August 21, 2024 05:20
-
-
Save eferro/651fbb72851fa7987fc642c8f39638eb to your computer and use it in GitHub Desktop.
golang aws: examples
This file contains 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
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) | |
} | |
} | |
} |
This file contains 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
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) | |
} | |
} |
This file contains 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
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 | |
}) | |
} |
This file contains 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
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) | |
} | |
} |
This file contains 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
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
is there a way to sort by creation date or something like that for "resp" "resp, err := ec2svc.DescribeInstances(params)"