forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateKeyPair.go
71 lines (61 loc) · 2.21 KB
/
CreateKeyPair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[ec2.go.create_key_pair]
package main
// snippet-start:[ec2.go.create_key_pair.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
// snippet-end:[ec2.go.create_key_pair.imports]
// MakeKeyPair creates a Amazon Elastic Compute Cloud (Amazon EC2) key pair.
// Inputs:
// svc is an Amazon EC2 service client
// keyName is the name of the key pair
// Output:
// If success, information about the key pair and nil
// Otherwise, nil and an error from the call to CreateKeyPair
func MakeKeyPair(svc ec2iface.EC2API, keyName *string) (*ec2.CreateKeyPairOutput, error) {
// snippet-start:[ec2.go.create_key_pair.call]
result, err := svc.CreateKeyPair(&ec2.CreateKeyPairInput{
KeyName: keyName,
})
// snippet-end:[ec2.go.create_key_pair.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// snippet-start:[ec2.go.create_key_pair.args]
keyName := flag.String("k", "", "The name of the key pair")
flag.Parse()
if *keyName == "" {
fmt.Println("You must supply the name of the key pair (-k KEY-NAME)")
return
}
// snippet-end:[ec2.go.create_key_pair.args]
// snippet-start:[ec2.go.create_key_pair.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := ec2.New(sess)
// snippet-end:[ec2.go.create_key_pair.session]
// Creates a key pair with the given name
result, err := MakeKeyPair(svc, keyName)
if err != nil {
fmt.Println("Got an error creating the key pair:")
fmt.Println(err)
return
}
// snippet-start:[ec2.go.create_key_pair.display]
fmt.Println("Created key pair:")
fmt.Println(" Name: " + *result.KeyName)
fmt.Println(" Fingerprint: " + *result.KeyFingerprint)
fmt.Println(" Material: " + *result.KeyMaterial)
// snippet-end:[ec2.go.create_key_pair.display]
}
// snippet-end:[ec2.go.create_key_pair]