diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index f0f0adf3b..71a04aea9 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -327,6 +327,7 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error { IAMInstanceProfile: instanceOpts.IAMInstanceProfile, ImageID: instanceOpts.ImageID, InstanceType: instanceOpts.InstanceType, + KeyName: instanceOpts.KeyName, MaxCount: aws.Long(int64(1)), MinCount: aws.Long(int64(1)), NetworkInterfaces: instanceOpts.NetworkInterfaces, diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index eb52289bd..ef7edcc20 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/awsutil" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -456,6 +457,40 @@ func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { }) } +// Guard against regression with KeyPairs +// https://github.com/hashicorp/terraform/issues/2302 +func TestAccAWSInstance_keyPairCheck(t *testing.T) { + var v ec2.Instance + + testCheckKeyPair := func(keyName string) resource.TestCheckFunc { + return func(*terraform.State) error { + if v.KeyName == nil { + return fmt.Errorf("No Key Pair found, expected(%s)", keyName) + } + if *v.KeyName != keyName { + return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, awsutil.StringValue(v.KeyName)) + } + + return nil + } + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccInstanceConfigKeyPair, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists("aws_instance.foo", &v), + testCheckKeyPair("tmp-key"), + ), + }, + }, + }) +} + func testAccCheckInstanceDestroy(s *terraform.State) error { return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) } @@ -889,3 +924,19 @@ resource "aws_eip" "foo_eip" { depends_on = ["aws_internet_gateway.gw"] } ` +const testAccInstanceConfigKeyPair = ` +provider "aws" { + region = "us-east-1" +} + +resource "aws_key_pair" "debugging" { + key_name = "tmp-key" + public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" +} + +resource "aws_instance" "foo" { + ami = "ami-408c7f28" + instance_type = "t1.micro" + key_name = "${aws_key_pair.debugging.key_name}" +} +`