package aws import ( "fmt" "github.com/hashicorp/terraform/helper/schema" awsSDK "github.com/awslabs/aws-sdk-go/aws" awsEC2 "github.com/awslabs/aws-sdk-go/service/ec2" ) 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).ec2SDKconn keyName := d.Get("key_name").(string) publicKey := d.Get("public_key").(string) req := &awsEC2.ImportKeyPairInput{ KeyName: awsSDK.String(keyName), PublicKeyMaterial: []byte(publicKey), } resp, err := ec2conn.ImportKeyPair(req) 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).ec2SDKconn req := &awsEC2.DescribeKeyPairsInput{ KeyNames: []*string{awsSDK.String(d.Id())}, } resp, err := ec2conn.DescribeKeyPairs(req) if err != nil { return fmt.Errorf("Error retrieving KeyPair: %s", err) } for _, keyPair := range resp.KeyPairs { if *keyPair.KeyName == d.Id() { d.Set("key_name", keyPair.KeyName) d.Set("fingerprint", keyPair.KeyFingerprint) return nil } } return fmt.Errorf("Unable to find key pair within: %#v", resp.KeyPairs) } func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error { ec2conn := meta.(*AWSClient).ec2SDKconn _, err := ec2conn.DeleteKeyPair(&awsEC2.DeleteKeyPairInput{ KeyName: awsSDK.String(d.Id()), }) return err }