Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

Working with Amazon EC2 Key Pairs

フォーカスモード
Working with Amazon EC2 Key Pairs - AWS SDK for Go (version 1)
このページはお客様の言語に翻訳されていません。 翻訳のリクエスト

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

These Go examples show you how to:

  • Describe an Amazon EC2 key pair

  • Create an Amazon EC2 key pair

  • Delete an Amazon EC2 key pair

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

Amazon EC2 uses public–key cryptography to encrypt and decrypt login information. Public–key cryptography uses a public key to encrypt data, then the recipient uses the private key to decrypt the data. The public and private keys are known as a key pair.

The routines use the AWS SDK for Go to perform these tasks by using these methods of the EC2 type:

Prerequisites

Describe Your Key Pairs

Create a new Go file named ec2_describe_keypairs.go.

Import the required AWS SDK for Go packages.

package main import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" "fmt" "os" )

Use the following code to create a session and Amazon EC2 client.

// go run ec2_describe_keypairs.go // credentials from the shared credentials file ~/.aws/credentials. sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create an EC2 service client.

Call DescribeKeyPairs to get a list of key pairs and print them out.

// Returns a list of key pairs result, err := svc.DescribeKeyPairs(nil) if err != nil { exitErrorf("Unable to get key pairs, %v", err) } fmt.Println("Key Pairs:") for _, pair := range result.KeyPairs { fmt.Printf("%s: %s\n", *pair.KeyName, *pair.KeyFingerprint) }

The routine uses the following utility function.

func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", args...) os.Exit(1)

See the complete example on GitHub.

Create a Key Pair

Create a new Go file named ec2_create_keypair.go.

Import the required AWS SDK for Go packages.

package main import ( "fmt" "os" "path/filepath" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" )

Get the key pair name passed in to the code and, to access Amazon EC2, create an EC2 client.

func main() { if len(os.Args) != 2 { exitErrorf("pair name required\nUsage: %s key_pair_name", filepath.Base(os.Args[0])) } pairName := os.Args[1] sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create an EC2 service client. svc := ec2.New(sess)

Create a new key pair with the provided name.

result, err := svc.CreateKeyPair(&ec2.CreateKeyPairInput{ KeyName: aws.String(pairName), }) if err != nil { if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidKeyPair.Duplicate" { exitErrorf("Keypair %q already exists.", pairName) } exitErrorf("Unable to create key pair: %s, %v.", pairName, err) } fmt.Printf("Created key pair %q %s\n%s\n", *result.KeyName, *result.KeyFingerprint, *result.KeyMaterial) }

The routine uses the following utility function.

func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", args...) os.Exit(1) }

See the complete example on GitHub.

Delete a Key Pair

Create a new Go file named ec2_delete_keypair.go.

Import the required AWS SDK for Go packages.

package main import ( "fmt" "os" "path/filepath" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" )

Get the key pair name passed in to the code and, to access Amazon EC2, create an EC2 client.

func main() { if len(os.Args) != 2 { exitErrorf("pair name required\nUsage: %s key_pair_name", filepath.Base(os.Args[0])) } pairName := os.Args[1] sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create an EC2 service client. svc := ec2.New(sess)

Delete the key pair with the provided name.

_, err = svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{ KeyName: aws.String(pairName), }) if err != nil { if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "InvalidKeyPair.Duplicate" { exitErrorf("Key pair %q does not exist.", pairName) } exitErrorf("Unable to delete key pair: %s, %v.", pairName, err) } fmt.Printf("Successfully deleted %q key pair\n", pairName) }

The routine uses the following utility function.

func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", args...) os.Exit(1) }

See the complete example on GitHub.

このページの内容

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.