gpt4 book ai didi

amazon-web-services - 为什么我无法通过 ssh 连接到使用 AWS CloudFormation 创建的 AWS ec2 实例?

转载 作者:行者123 更新时间:2023-12-03 07:44:15 34 4
gpt4 key购买 nike

我正在使用 CloudFormation 创建一个 EC2 实例,其中提到了 VPC、子网和安全组。但是,当实例启动时,我无法 ssh 到实例并收到以下消息:

ssh -i aws_jenkins.pem [email protected]

无法确定主机“34.217.129.89 (34.217.129.89)”的真实性。ECDSA key 指纹为 SHA256:rs3bjVKolzdmktzfiSd0Oo5RU6dLdT/PGHpycStgFK8。ECDSA key 指纹为 MD5:7f:cc:61:c4:f3:1a:b7:45:9a:f0:da:e8:0c:a4:d9:bc。

您确定要继续连接吗(是/否)?是的

警告:已将“34.217.129.89”(ECDSA) 永久添加到已知主机列表中。

权限被拒绝(publickey、gssapi-keyex、gssapi-with-mic)。

但是,如果我从 AWS 控制台创建另一个 EC2

使用相同的 ImageID、VPC、子网、SecurityGroup 和 key 。我可以通过 SSH 连接到它。

但是为什么通过 CloudFormation 创建的实例不支持 SSH

我无法解决这个问题,非常感谢任何见解/解决方案。

AWSTemplateFormatVersion: 2010-09-09

Parameters:

EnvironmentName:
Description: An environment name that will be prefixed to resource names
Type: String

VpcCIDR:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.192.0.0/16

PublicSubnet1CIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone
Type: String
Default: 10.192.10.0/24

PublicSubnet2CIDR:
Description: Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone
Type: String
Default: 10.192.11.0/24

InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t2.micro
AllowedValues: [ t1.micro, t2.nano, t2.micro, t2.small, t2.medium]
ConstraintDescription : must be a valid EC2 instance type.

KeyName:
Description: The EC2 Key Pair to allow SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
Default: jenkins-test
ConstraintDescription: must be the name of an existing EC2 KeyPair.

SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: 9
MaxLength: 18
Default: 0.0.0.0/0
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.

Resources:

VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCIDR
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Ref EnvironmentName

InternetGateway:
Type: AWS::EC2::InternetGateway
DependsOn: VPC
Properties:
Tags:
- Key: Name
Value: !Ref EnvironmentName

InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC

PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs ]
CidrBlock: !Ref PublicSubnet1CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-Public-Subnet-(AZ1)

PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 1, !GetAZs ]
CidrBlock: !Ref PublicSubnet2CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub ${EnvironmentName}-Public-Subnet-(AZ2)

PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${EnvironmentName} Public Routes

DefaultPublicRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway

PublicSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet1

PublicSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PublicSubnet2


InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access and HTTP to instance
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref SSHLocation

- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref SSHLocation
VpcId: !Ref VPC
Tags:
- Key: Name
Value: TestSecurity_group

MyInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-061392db613a6357b
InstanceType: !Ref InstanceType
SubnetId: !Ref PublicSubnet1
KeyName: !Ref KeyName
SecurityGroupIds:
- !Ref InstanceSecurityGroup
Tags:
- Key: Name
Value: TestServer

最佳答案

在您的参数中,您有:

KeyName:
Description: The EC2 Key Pair to allow SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
Default: jenkins-test
ConstraintDescription: must be the name of an existing EC2 KeyPair.
  1. 您是否让它使用此默认参数进行构建?
  2. 您确定使用了正确的匹配 key 吗?

关于amazon-web-services - 为什么我无法通过 ssh 连接到使用 AWS CloudFormation 创建的 AWS ec2 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833514/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com