简体   繁体   中英

Using boto to connect to pre-existing windows instance

My code is the folliwing

import boto.ec2
conn = boto.ec2.connect_to_region("us-west-2",aws_access_key_id='secret1',aws_secret_access_key='secret2')




conn.run_instances(
        'i-41ffc2c0',
        key_name='MyWindowsKey',
        instance_type='t2.micro',
        security_groups=['launch-wizard-3'])


print conn

Where launch-wizard-3 allows RDP connections with no problem.

Now when I run the above I get the following error:

Traceback (most recent call last):
  File "Documents/boto_test.py", line 11, in <module>
    security_groups=['launch-wizard-3'])
  File "/Library/Python/2.7/site-packages/boto/ec2/connection.py", line 973, in run_instances
    verb='POST')
  File "/Library/Python/2.7/site-packages/boto/connection.py", line 1208, in get_object
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidParameterValue</Code><Message>Value () for parameter groupId is invalid. The value cannot be empty</Message></Error></Errors><RequestID>e9d2cb00-a49b-4de5-a60e-178067f88119</RequestID></Response>

This is my first time connecting to this via boto.

Thanks for any pointers.

From the error message, seems only provide security_groups is not enough.

Please provide security_group_ids when create a new ec2 instance. You can also create the instance without it, and add the security group later after the instance is created.

security_groups (list of strings) – The names of the EC2 classic security groups with which to associate instances

security_group_ids (list of strings) – The ID of the VPC security groups with which to associate instances.

Notes, maybe there is one more problem in your code. which @jarmod has pointed it out

i-41ffc2c0 is not ami id, it is a ec2 instance id. You need update it as well.

The boto library is used to manage AWS infrastructure, for example to launch new EC2 instances, to stop them, and to terminate them. It does not provide RDP or SSH access to those EC2 instances.

To connect to the EC2 instances, you would use your regular Windows client, for example Remote Desktop Connection (mstsc) or Putty for SSH. See Connecting to your Windows instance using RDP .

[Extra info has been provided indicating that OP seeks help on how to "launch, connect, disconnect, and wind down" an instance of ec2]

One thing you need to understand is that older AWS accounts have concepts called "EC2 Classic" and "VPC". Newer accounts only have VPC. I'll assume your account is newer, so here is how you use boto to launch an EC2 instance in a specific subnet (in a VPC, inferred from the subnet ID):

r = conn.run_instances(
        image_id='ami-xxxxxxxx',
        key_name='MyWindowsKey',
        instance_type='t2.micro',
        subnet_id='subnet-xxxxxxxx',
        security_group_ids=['sg-xxxxxxxx'])

Note specifically:

  • the first parameter is an AMI ID (not an instance ID as your original post shows)
  • you need to supply a subnet ID (that's the ID of an existing subnet in an existing VPC)
  • you need to supply the security group IDs , not names and the IDs will be in the form sg-xxxxxxxx, not launch-wizard-3 (which is a 'group name').

To stop a running EC2 instance, you need an instance ID:

conn.stop_instances(instance_ids=['i-xxxxxxxx'])

To terminate an EC2 instance, you need an instance ID:

conn.terminate_instances(instance_ids=['i-xxxxxxxx'])

For more general examples of using boto with EC2, see this tutorial .

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