这可能是由于未在AMI中打开所需的端口或配置安全组而导致。在启动AMI的实例中,您需要检查安全组设置和部署应用程序的端口配置。
以下是一个Python脚本示例,用于创建EC2实例,并在启动实例后更新安全组以允许入站SSH流量:
import boto3
# Set the region
region = 'us-west-2'
# Set the AMI ID and instance type
ami_id = 'ami-0323c3dd2da7fb37d' # Example AMI ID
instance_type = 't2.micro'
# Set the key pair name and SSH key file path
key_pair_name = 'example_key_pair'
ssh_key_path = 'path/to/example_key_pair.pem'
# Set the security group name and port number
security_group_name = 'example_security_group'
port_number = 22
ec2 = boto3.resource('ec2', region_name=region)
# Create a security group
security_group = ec2.create_security_group(
GroupName=security_group_name,
Description='Example security group'
)
# Authorize inbound SSH traffic
security_group.authorize_ingress(
IpProtocol='tcp',
FromPort=port_number,
ToPort=port_number,
CidrIp='0.0.0.0/0'
)
# Start the instance
instance = ec2.create_instances(
ImageId=ami_id,
InstanceType=instance_type,
KeyName=key_pair_name,
MinCount=1,
MaxCount=1,
SecurityGroups=[security_group_name]
)[0]
instance.wait_until_running()
print(f'Instance {instance.id} is now running.')
# Update the security group to allow inbound SSH traffic
security_group.authorize_ingress(
IpProtocol='tcp',
FromPort=port_number,
ToPort=port_number,
CidrIp='0.0.0.0/0'
)
print(f'Inbound SSH traffic is now allowed for security group {security_group.group_name}.')
请注意,这是一个简
下一篇:AMI如何创建进程?