【发布时间】: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