From 9e2ecafc4640fe9cfc4eb2bc23b6baaff646dbba Mon Sep 17 00:00:00 2001 From: Phil Frost Date: Mon, 18 May 2015 16:19:51 -0400 Subject: [PATCH 1/2] Handle AWS keypairs which no longer exist When refreshing a keypair, update state appropriately rather than crash if the keypair no longer exists on AWS. Likely fixes #1851. --- builtin/providers/aws/resource_aws_key_pair.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builtin/providers/aws/resource_aws_key_pair.go b/builtin/providers/aws/resource_aws_key_pair.go index b9c406467..cf4d82c54 100644 --- a/builtin/providers/aws/resource_aws_key_pair.go +++ b/builtin/providers/aws/resource_aws_key_pair.go @@ -65,6 +65,11 @@ func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error { } resp, err := conn.DescribeKeyPairs(req) if err != nil { + awsErr, ok := err.(aws.APIError) + if ok && awsErr.Code == "InvalidKeyPair.NotFound" { + d.SetId("") + return nil + } return fmt.Errorf("Error retrieving KeyPair: %s", err) } From 66372d8d1dd0e6c12898159fa15556bf28a93750 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Thu, 21 May 2015 09:21:57 -0500 Subject: [PATCH 2/2] small update to keypair_refresh, to bring in-line with sdk changes --- builtin/providers/aws/resource_aws_key_pair.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_key_pair.go b/builtin/providers/aws/resource_aws_key_pair.go index cf4d82c54..036c35fe8 100644 --- a/builtin/providers/aws/resource_aws_key_pair.go +++ b/builtin/providers/aws/resource_aws_key_pair.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/aws/awserr" "github.com/awslabs/aws-sdk-go/service/ec2" ) @@ -65,8 +66,8 @@ func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error { } resp, err := conn.DescribeKeyPairs(req) if err != nil { - awsErr, ok := err.(aws.APIError) - if ok && awsErr.Code == "InvalidKeyPair.NotFound" { + awsErr, ok := err.(awserr.Error) + if ok && awsErr.Code() == "InvalidKeyPair.NotFound" { d.SetId("") return nil }