简体   繁体   中英

AWS-BOTO security group error

I have the following code to spin up new instances:

conn = boto.ec2.connect_to_region("us-east-1", security_token = "xx", aws_access_key_id= "xx",  aws_secret_access_key= "xx")


security_groups = conn.get_all_security_groups()
for security_group in security_groups:
    if str(security_group)[14:] == "xx":
        conn.run_instances(
                'ami-da2cd5b2',
                key_name='fornax_keypair',
                instance_type='c1.xlarge',
                security_groups=security_group)
    else:
        continue

It finds the security group and then gives the error :

TypeError: 'SecurityGroup' object is not iterable

If I change it to str(security_group), it then gives the error:

<Response><Errors><Error><Code>InvalidGroup.NotFound</Code><Message>The security groups 'f', 'g', 'd', 'e', 'c', 'n', 'o', 'j', '.', 'i', 'v', 'u', 't', 's', 'r', 'p', '
:', 'y' do not exist</Message></Error></Errors><RequestID>c96afd3c-de3f-4441-be65-c6a85fbe7868</RequestID></Response>

Also how do I attach the connection to an already established vpc connection and subnet ?

The security_groups parameter to run_instances is supposed to be list of security group names. You are passing a scalar string value. Try this instead:

conn.run_instances(
    'ami-da2cd5b2',
    key_name='fornax_keypair,
    instance_type='c1.xlarge',
    security_groups=[security_group])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM