【问题标题】:AWS Could not connect to the endpoint URLAWS 无法连接到终端节点 URL
【发布时间】:2021-07-20 06:04:17
【问题描述】:

即使我在我的脚本和 .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"

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto3 amazon-cloudwatch


    【解决方案1】:

    区域名称是eu-west-1

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

    在 boto3 中创建 Resource 或 Client 时,使用不带 AZ 标识符的 Region:

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

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

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

    或:

    SubnetId = 'YOUR SUBNET ID'
    

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

    【讨论】:

    • 哦,这是有道理的,我已经更改了它,但是当输入 Placement={'AvailabilityZone': 'eu-west-1b'} ^ SyntaxError: invalid syntax 是我得到的错误,我有create_instances 而不是 run_instances,我也假设这同样适用于我的 /.aws/config 文件,我改为 eu-west-1
    • 啊!它是resource.create_instances()client.run_instances()。类似的命令。 client 方法映射到 API 调用,而 client 方法更 Pythonic。有关语法,请参阅create_instances() documentation。如果您仍然有错误,请编辑您的问题以显示您当前的代码并返回 exact 错误。
    • 啊,工作得很好,谢谢,但不幸的是没有解决更大的问题,我在另一篇文章的“get_metric_statistics”中调用时没有显示任何数据点或平均数。
    • John 你知道我可以将哪个命令添加到我的 create_instance 中,以便为我启用详细监控吗?
    • Monitoring={'Enabled': True} -- 在 boto3 文档中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多