如何使用附加已创建的安全组的python创建EC2-instance

时间:2019-07-11 12:39:38

标签: python amazon-web-services amazon-ec2 aws-sdk boto3

这是我的代码:

image_id='ami-0bbc25e23a7640b9b'
instance_type='t2.micro'
keypair_name='my_key'
security_group_id='my_security_group_id'

ec2 = boto3.resource('ec2')
response = ec2.create_instances(ImageId=image_id,InstanceType=instance_type,KeyName=keypair_name,MinCount=1,MaxCount=1,SecurityGroups=[security_group_id])

我在输出中看到

Traceback (most recent call last):
  File "2.py", line 98, in <module>
    response = ec2.create_instances(ImageId=image_id,InstanceType=instance_type,KeyName=keypair_name,MinCount=1,MaxCount=1,SecurityGroups=[security_group_id])
  File "/usr/local/lib/python3.6/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the RunInstances operation: Value () for parameter groupId is invalid. The value cannot be empty

我在哪里出错? 我尝试将boto3.clientec2.run_instances一起使用,但结果相同。你能帮我吗?

1 个答案:

答案 0 :(得分:1)

如果您使用的是安全组ID(例如sg-93db39f6),则应在SecurityGroupIds(而不是SecurityGroups)中传递它。