DescribeKeyPairs.go - AWS Code Sample

DescribeKeyPairs.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) // GetKeyPairs retrieves a list of key pairs stored in Amazon EC2. // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // Output: // If success, the list of key pairs and nil // Otherwise, nil and an error from the call to DescribeKeyPairs func GetKeyPairs(sess *session.Session) (*ec2.DescribeKeyPairsOutput, error) { svc := ec2.New(sess) result, err := svc.DescribeKeyPairs(nil) if err != nil { return nil, err } return result, nil } func main() { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) result, err := GetKeyPairs(sess) if err != nil { fmt.Println("Got an error retrieving key pairs:") fmt.Println(err) return } fmt.Println("Key Pairs:") for _, pair := range result.KeyPairs { fmt.Println(" Name: " + *pair.KeyName) fmt.Println(" Fingerprint: " + *pair.KeyFingerprint) fmt.Println("") } }