前提・実現したいこと
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インスタンス作成時に関連した設定はしていないはずです。
解決策が見つからず困っています。
すみませんが見ていただけないでしょうか。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。