質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Terraform

Terraformは、インフラ構築のためのツールです。AWS/DigitalOcean/GoogleCloudといった様々なインフラに対応。インフラ構成のコード管理や変更の作業などの手間を自動化し、インフラ構築の効率化を図ることができます。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

2回答

1585閲覧

Terraformで作成したEC2へSSH接続したい

退会済みユーザー

退会済みユーザー

総合スコア0

Terraform

Terraformは、インフラ構築のためのツールです。AWS/DigitalOcean/GoogleCloudといった様々なインフラに対応。インフラ構成のコード管理や変更の作業などの手間を自動化し、インフラ構築の効率化を図ることができます。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

0クリップ

投稿2020/08/31 00:20

編集2020/08/31 14:43

前提・実現したいこと

Terraformで作成したEC2へSSH接続したいです。

ここに質問の内容を詳しく書いてください。
TerraformでVPC、EC2、RDSの作成後、EC2へSSH接続を試みましたが拒否されてしまいました。
SSH接続に使用したPemファイルは、AWSサイト上で作成したEC2インスタンスには接続できたのでカギ自体には問題がないはずです(権限は400、ディレクトリは合ってます)。
SSH接続を制御しているのはセキュリティグループだと思いますので、接続しやすいようにインバウンドを cidr_block = "0.0.0.0/0" にしています。

発生している問題・エラーメッセージ

% ssh -i "~/.ssh/キー名.pem" ec2-user@(Elastic IP) The authenticity of host '(Elastic IP)' can't be established. ECDSA key fingerprint is ***. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '(Elastic IP)' (ECDSA) to the list of known hosts. ec2-user@(Elastic IP): Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

該当のソースコード

variable "aws_access_key" {} variable "aws_secret_key" {} variable "region" { default = "us-east-1" } terraform { required_providers { aws = { source = "hashicorp/aws" } } } provider "aws" { access_key = "${var.aws_access_key}" secret_key = "${var.aws_secret_key}" region = "${var.region}" } resource "aws_vpc" "vpc" { cidr_block = "10.0.0.0/16" instance_tenancy = "default" enable_dns_support = "true" enable_dns_hostnames = "false" } resource "aws_internet_gateway" "igw" { vpc_id = "${aws_vpc.vpc.id}" } resource "aws_subnet" "subnet_1a" { vpc_id = "${aws_vpc.vpc.id}" cidr_block = "10.0.0.0/24" availability_zone = "us-east-1a" } resource "aws_subnet" "subnet_1c" { vpc_id = "${aws_vpc.vpc.id}" cidr_block = "10.0.1.0/24" availability_zone = "us-east-1c" } resource "aws_route_table" "table" { vpc_id = "${aws_vpc.vpc.id}" route { cidr_block = "0.0.0.0/0" gateway_id = "${aws_internet_gateway.igw.id}" } } resource "aws_route_table_association" "subnet_1a" { subnet_id = "${aws_subnet.subnet_1a.id}" route_table_id = "${aws_route_table.table.id}" } resource "aws_route_table_association" "subnet_1c" { subnet_id = "${aws_subnet.subnet_1c.id}" route_table_id = "${aws_route_table.table.id}" } resource "aws_security_group" "securitygroup" { name = "securitygroup" description = "Allow HTTP SSH inbound traffic" vpc_id = "${aws_vpc.vpc.id}" } resource "aws_security_group_rule" "inbound_http" { type = "ingress" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] security_group_id = "${aws_security_group.securitygroup.id}" } resource "aws_security_group_rule" "inbound_ssh" { type = "ingress" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] security_group_id = "${aws_security_group.securitygroup.id}" } resource "aws_security_group_rule" "outbound" { type = "egress" from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] security_group_id = "${aws_security_group.securitygroup.id}" } resource "aws_db_subnet_group" "subnet_group" { name = "subnet_group" subnet_ids = ["${aws_subnet.subnet_1a.id}", "${aws_subnet.subnet_1c.id}"] } resource "aws_db_instance" "mysql" { identifier = "instance" allocated_storage = 20 storage_type = "gp2" engine = "mysql" engine_version = "5.6.34" instance_class = "db.t2.micro" name = "mysql" username = "ユーザー名" password = "パスワード" vpc_security_group_ids = ["${aws_security_group.securitygroup.id}"] db_subnet_group_name = "${aws_db_subnet_group.subnet_group.name}" skip_final_snapshot = true } resource "aws_instance" "ec2-test" { ami = "ami-02354e95b39ca8dec" instance_type = "t2.micro" key_name = "${aws_key_pair.auth.id}" vpc_security_group_ids = [ "${aws_security_group.securitygroup.id}" ] subnet_id = "${aws_subnet.subnet_1c.id}" associate_public_ip_address = "true" } resource "aws_key_pair" "auth" { key_name = "${var.key_name}" public_key = "${file(var.public_key_path)}" } resource "aws_eip" "elastic_ip" { instance = "${aws_instance.ec2-test.id}" }

#試したこと
以下のようにログを出してみました。
ssh -vvv ec2-user@(Elastic IP) -i ~/.ssh/キー名.pem
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/username/.ssh/キー名.pem
debug3: sign_and_send_pubkey: RSA *****
debug3: sign_and_send_pubkey: signing using rsa-sha2-512
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
ec2-user@(Elastic IP): Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

接続できるインスタンスの場合は以下のようなログになります。
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/username/.ssh/キー名.pem
debug3: sign_and_send_pubkey: RSA ***
debug3: sign_and_send_pubkey: signing using rsa-sha2-512
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 52
debug1: Authentication succeeded (publickey).

rsa-sha2-512 (証明書?)でのサインインが上手く行ってないような記載がありますが、EC2インスタンス作成時に関連した設定はしていないはずです。

解決策が見つからず困っています。
すみませんが見ていただけないでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

EC2インスタンス作成時のキーペア登録で解決しました。
(上記コードは修正済みです)

投稿2020/08/31 14:46

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

接続鍵が違ってるときに出るエラーですね
接続鍵があってるのか、鍵の形式があってるのか、そこらへんをチェックしてみましょう

投稿2020/08/31 00:59

y_waiwai

総合スコア87784

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/08/31 02:03

回答ありがとうございます。 接続鍵は、AWSサイト上でEC2インスタンスを作成したときに作った鍵です(こちらは接続は問題ありません)。 それをTerraformで作成した別のEC2インスタンスへのSSH接続にも使用しました(こちらが本件です)。 その場合は、Terraformからのインスタンスに使用する場合の設定が必要でしょうか。 また、別途鍵を作るときは次のコマンドを使用しています。 $ aws ec2 create-key-pair --key-name キー名 --query 'KeyMaterial' --output text > .キー名.pem $ chmod 400 キー名 https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ec2-key-pairs.html アドバイスよろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問