TerraformでAWS環境の鍵ペアをインポートする

Terraformのドキュメントを見ていたら、AWS環境の鍵ペアをインポートできることが
分かり試してみました。


コンフィグは以下のとおりです。

provider "aws" {
  access_key = <your access key>
  secret_key = <your secret key>
  region     = "ap-northeast-1"
}

resource "aws_key_pair" "terraform-key" {
  key_name   = "terraform-key"
  public_key = <public_key>
}


で、やってみます。

$ terraform plan                                                                                                                     
Refreshing Terraform state prior to plan...


The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

+ aws_key_pair.terraform-key
    fingerprint: "" => "<computed>"
    key_name:    "" => "terraform-key"
    public_key:  "" => <public_key>


$ terraform apply                                                                                                                    
aws_key_pair.terraform-key: Creating...
  fingerprint: "" => "<computed>"
  key_name:    "" => "terraform-key"
  public_key:  "" => <public_key>
aws_key_pair.terraform-key: Creation complete


無事、鍵を登録しました。

 aws ec2 describe-key-pairs
{
    "KeyPairs": [
        {
            "KeyName": "terraform-key",
            "KeyFingerprint": "db:dc:28:2b:51:b5:47:45:b5:47:bd:7f:d4:d8:84:9a"
        }
    ]
}


今日はこんなところで。