AWS 无法连接到端点 URL

AWS Could not connect to the endpoint URL

即使我在我的脚本和 .aws/config 文件中将区域更改为 eu-west-1b,它仍然给我这个错误,我的实例一直在 eu-west-1b 中启动不确定为什么会出现此错误。

#!/usr/bin/env python3
import sys
import boto3
import time
ec2 = boto3.resource('ec2', region_name = 'eu-west-1b')
s3 = boto3.resource('s3')
keyname = 'key1.pem'
s3_resource = boto3.resource('s3')
user_data = '''#!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd'''

try:
        res = Resp = s3.create_bucket(ACL='private',Bucket='buket2',CreateBucketConfiguration={'LocationConstraint': 'eu-west-1b'})
        print (res)

try:
        s3_resource.Bucket('buket2').upload_file('image.jpg', 'image.jpg', ExtraArgs={'ACL': 'public-read'})


try:
        gg = ec2.create_security_group(GroupName='Server', Description = 'Serv1', VpcId='vpc-f3fs4095')

        print (gg)
except Exception as error:
    print (error)

instance = ec2.create_instances(
 ImageId='ami-03odd1b743b23e5d2',
 MinCount=1,
 MaxCount=1,
 InstanceType='t2.nano',
 KeyName = 'key1.pem',
 UserData = user_data, 
 SecurityGroupIds=[sg.group_id] 
)


from datetime import datetime, timedelta
time.sleep(600)
client = boto3.client('cloudwatch', region_name='eu-west-1b')
response = client.get_metric_statistics(
            Namespace='AWS/EC2',
            MetricName='CPUUtilization',
            Dimensions=[
                {
                'Name': 'AMIID',
                'Value': 'ami-03odd1b743b23e5d2'
                },
            ],
            StartTime=datetime.now() - timedelta(seconds=600),
            EndTime=datetime.now(),
            Period=300,
            Statistics=[
                'Average',
            ],
            Unit='Percent'
        )
print(response)

response = sg.authorize_ingress(
    IpPermissions=[
        {
            "FromPort": 22,
            "ToPort": 22,
            "IpProtocol": "tcp",
            "IpRanges": [
                {"CidrIp": "0.0.0.0/0", "Description": "Server"},
            ],
        },
        {
            "FromPort": 80,
            "ToPort": 80,
            "IpProtocol": "tcp",
            "IpRanges": [
                {"CidrIp": "0.0.0.0/0", "Description": "Server1"},
            ],
        },
    ],
)

我收到了一条很长的错误消息,但我只会包含重要的部分,因为除非您希望我也包含其中,否则大部分内容都指向文件。

Could not connect to the endpoint URL: "https://buket2.s3.eu-west-1b.amazonaws.com/"
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 73, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

    raise EndpointConnectionError(endpoint_url=request.url, error=e)
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "https://buket2.s3.eu-west-1b.amazonaws.com/image.jpg"

地区名称是eu-west-1

b 标识一个可用区eu-west-1b

在 boto3 中创建资源或客户端时,使用不带 AZ 标识符的区域:

ec2 = boto3.resource('ec2', region_name = 'eu-west-1')

要在特定可用区启动 Amazon EC2 实例,请在 create_instances()run_instances() 中指定:

Placement={'AvailabilityZone': 'eu-west-1b'}

或:

SubnetId = 'YOUR SUBNET ID'

“如果未指定,将根据区域的负载平衡标准自动为您选择一个可用区。”