0
0

AWSキーペアにED25519形式の鍵ファイルを作成してTerraformで公開鍵を登録する

Last updated at Posted at 2024-07-19

AWSキーペアに登録するキーペアの作成方法をよく失念するので、一連の流れを備忘録として整理。

  1. AWSキーペアに登録するためのキーペア(ED25519)を作成する
    個人的には強度の強いED25519を使っています

    ssh-keygen -t ed25519 -C "公開鍵に記載されるコメント"
    

    保存先のパスとパスフレーズ(確認も含め)を入力すると、秘密鍵と公開鍵が作成されます。

  2. tfファイルでaws_key_pairリソースを作成する

    resource "aws_key_pair" "sample" {
      key_name   = "sample"
      public_key = "ssh-ed25519XXXXXXXXXXXXX"
    }
    

    publik_keyには1で作成された公開鍵(.pub)の値をコピーする

  3. Terraformでplanしてみて意図したものになっているか確認する

    % terraform plan
    〜省略〜
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # aws_key_pair.sample will be created
      + resource "aws_key_pair" "sample" {
          + arn             = (known after apply)
          + fingerprint     = (known after apply)
          + id              = (known after apply)
          + key_name        = "sample"
          + key_name_prefix = (known after apply)
          + key_pair_id     = (known after apply)
          + key_type        = (known after apply)
          + public_key      = "ssh-ed25519XXXXXXXXXXXXX"
          + tags_all        = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    ─────────────────────────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
    
  4. TerraformでApplyする
    Terraform Cloudを使っています
    Apply画面

  5. AWS EC2 - キーペア上にもリソースが作成されていることが確認できます

    スクリーンショット 2024-07-19 14.40.42.png

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0