opentofu/builtin/providers/aws/resource_aws_key_pair.go
Pete Hodgson 5b66b9306e add aws_key_pair resource
For now this only supports importing a key pair (by specifying a
public_key) property. In the future it'd be fairly trivial to support
key pair creation, with the private key returned as a computed property.

In real world usage you'd probably want to provide that public_key
property via a variable rather than hard-coding it into a terraform
config that'd end up in source control.
2014-12-18 01:27:06 -08:00

75 lines
1.6 KiB
Go

package aws
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsKeyPair() *schema.Resource {
return &schema.Resource{
Create: resourceAwsKeyPairCreate,
Read: resourceAwsKeyPairRead,
Update: nil,
Delete: resourceAwsKeyPairDelete,
Schema: map[string]*schema.Schema{
"key_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn
keyName := d.Get("key_name").(string)
publicKey := d.Get("public_key").(string)
resp, err := ec2conn.ImportKeyPair(keyName, publicKey)
if err != nil {
return fmt.Errorf("Error import KeyPair: %s", err)
}
d.SetId(resp.KeyName)
return nil
}
func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn
resp, err := ec2conn.KeyPairs([]string{d.Id()}, nil)
if err != nil {
return fmt.Errorf("Error retrieving KeyPair: %s", err)
}
for _, keyPair := range resp.Keys {
if keyPair.Name == d.Id() {
d.Set("key_name", keyPair.Name)
d.Set("fingerprint", keyPair.Fingerprint)
return nil
}
}
return fmt.Errorf("Unable to find key pair within: %#v", resp.Keys)
}
func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn
_, err := ec2conn.DeleteKeyPair(d.Id())
return err
}