使用 Python 来管理 AWS
Using Python to Manage AWS
我正在尝试使用 Python 创建 EC2 实例,但我不断收到这些错误。
这是我的代码:
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
ImageId='ami-0922553b7b0369273',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro')
print instance[0].id
这是我遇到的错误
Traceback (most recent call last):
File "./createinstance.py", line 8, in <module>
InstanceType='t2.micro')
File "/usr/lib/python2.7/site-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 320, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 623, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAMIID.NotFound) when calling the RunInstances operation: The image id '[ami-0922553b7b0369273]' does not exist
我在尝试创建密钥对时也遇到错误
这是我创建密钥对的代码
import boto3
ec2 = boto3.resource('ec2')
# create a file to store the key locally
outfile = open('ec2-keypair.pem','w')
# call the boto ec2 function to create a key pair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')
# capture the key and store it in a file
KeyPairOut = str(key_pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)
response = ec2.instance-describe()
print response
这是错误消息
./createkey.py: line 1: import: command not found
./createkey.py: line 2: syntax error near unexpected token `('
./createkey.py: line 2: `ec2 = boto3.resource('ec2')'
我错过了什么?
对于您的第一个脚本,可能会出现以下两种可能性之一:
1、你引用的ID引用的AMI不可用,因为key不正确或者AMI不存在
2. AMI 在您的机器设置的区域不可用
您很可能 运行 从未针对正确区域配置的计算机上运行您的脚本。如果您 运行 在本地或未配置角色的服务器上运行脚本,并且您正在使用 aws-cli,则可以 运行 aws configure
命令来配置您的访问密钥和区域适当。如果您 运行 在配置了角色的服务器上部署您的实例,您的服务器需要 运行 在正确的区域,并且您的角色需要允许访问 EC2 AMI。
关于你的第二个问题(将来可能应该单独发布),你的脚本中的语法错误是没有遵循与你编写第一个脚本的格式相同的副作用。您的 python 脚本很可能实际上并未被解释为 python 脚本。您应该在文件顶部添加 shebang 并删除 import boto3
语句之前的空格。
#!/usr/bin/env python
import boto3
# create a file to store the key locally
outfile = open('ec2-keypair.pem','w')
# call the boto ec2 function to create a key pair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')
# capture the key and store it in a file
KeyPairOut = str(key_pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)
response = ec2.instance-describe()
print response
我正在尝试使用 Python 创建 EC2 实例,但我不断收到这些错误。
这是我的代码:
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
ImageId='ami-0922553b7b0369273',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro')
print instance[0].id
这是我遇到的错误
Traceback (most recent call last):
File "./createinstance.py", line 8, in <module>
InstanceType='t2.micro')
File "/usr/lib/python2.7/site-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 320, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/lib/python2.7/site-packages/botocore/client.py", line 623, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidAMIID.NotFound) when calling the RunInstances operation: The image id '[ami-0922553b7b0369273]' does not exist
我在尝试创建密钥对时也遇到错误
这是我创建密钥对的代码
import boto3
ec2 = boto3.resource('ec2')
# create a file to store the key locally
outfile = open('ec2-keypair.pem','w')
# call the boto ec2 function to create a key pair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')
# capture the key and store it in a file
KeyPairOut = str(key_pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)
response = ec2.instance-describe()
print response
这是错误消息
./createkey.py: line 1: import: command not found
./createkey.py: line 2: syntax error near unexpected token `('
./createkey.py: line 2: `ec2 = boto3.resource('ec2')'
我错过了什么?
对于您的第一个脚本,可能会出现以下两种可能性之一: 1、你引用的ID引用的AMI不可用,因为key不正确或者AMI不存在 2. AMI 在您的机器设置的区域不可用
您很可能 运行 从未针对正确区域配置的计算机上运行您的脚本。如果您 运行 在本地或未配置角色的服务器上运行脚本,并且您正在使用 aws-cli,则可以 运行 aws configure
命令来配置您的访问密钥和区域适当。如果您 运行 在配置了角色的服务器上部署您的实例,您的服务器需要 运行 在正确的区域,并且您的角色需要允许访问 EC2 AMI。
关于你的第二个问题(将来可能应该单独发布),你的脚本中的语法错误是没有遵循与你编写第一个脚本的格式相同的副作用。您的 python 脚本很可能实际上并未被解释为 python 脚本。您应该在文件顶部添加 shebang 并删除 import boto3
语句之前的空格。
#!/usr/bin/env python
import boto3
# create a file to store the key locally
outfile = open('ec2-keypair.pem','w')
# call the boto ec2 function to create a key pair
key_pair = ec2.create_key_pair(KeyName='ec2-keypair')
# capture the key and store it in a file
KeyPairOut = str(key_pair.key_material)
print(KeyPairOut)
outfile.write(KeyPairOut)
response = ec2.instance-describe()
print response