PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/boto-2.5.2/docs/source/ec2_tut.rst

#
ReStructuredText | 90 lines | 66 code | 24 blank | 0 comment | 0 complexity | 0eea157d98a91f5f373b772c2e375356 MD5 | raw file
  1. .. _ec2_tut:
  2. =======================================
  3. An Introduction to boto's EC2 interface
  4. =======================================
  5. This tutorial focuses on the boto interface to the Elastic Compute Cloud
  6. from Amazon Web Services. This tutorial assumes that you have already
  7. downloaded and installed boto.
  8. Creating a Connection
  9. ---------------------
  10. The first step in accessing EC2 is to create a connection to the service.
  11. There are two ways to do this in boto. The first is::
  12. >>> from boto.ec2.connection import EC2Connection
  13. >>> conn = EC2Connection('<AWS_ACCESS_KEY_ID>', '<AWS_SECRET_ACCESS_KEY>')
  14. At this point the variable conn will point to an EC2Connection object. In
  15. this example, the AWS access key and AWS secret key are passed in to the
  16. method explicitely. Alternatively, you can set the boto config environment variables
  17. and then call the constructor without any arguments, like this::
  18. >>> conn = EC2Connection()
  19. There is also a shortcut function in the boto package, called connect_ec2
  20. that may provide a slightly easier means of creating a connection::
  21. >>> import boto
  22. >>> conn = boto.connect_ec2()
  23. In either case, conn will point to an EC2Connection object which we will
  24. use throughout the remainder of this tutorial.
  25. Launching Instances
  26. -------------------
  27. Possibly, the most important and common task you'll use EC2 for is to launch,
  28. stop and terminate instances. In its most primitive form, you can launch an
  29. instance as follows::
  30. >>> conn.run_instances('<ami-image-id>')
  31. This will launch an instance in the specified region with the default parameters.
  32. You will not be able to SSH into this machine, as it doesn't have a security
  33. group set. See :doc:`security_groups` for details on creating one.
  34. Now, let's say that you already have a key pair, want a specific type of
  35. instance, and you have your :doc:`security group <security_groups>` all setup.
  36. In this case we can use the keyword arguments to accomplish that::
  37. >>> conn.run_instances(
  38. '<ami-image-id>',
  39. key_name='myKey',
  40. instance_type='c1.xlarge',
  41. security_groups=['your-security-group-here'])
  42. The main caveat with the above call is that it is possible to request an
  43. instance type that is not compatible with the provided AMI (for example, the
  44. instance was created for a 64-bit instance and you choose a m1.small instance_type).
  45. For more details on the plethora of possible keyword parameters, be sure to
  46. check out boto's :doc:`EC2 API reference <ref/ec2>`.
  47. Stopping Instances
  48. ------------------
  49. Once you have your instances up and running, you might wish to shut them down
  50. if they're not in use. Please note that this will only de-allocate virtual
  51. hardware resources (as well as instance store drives), but won't destroy your
  52. EBS volumes -- this means you'll pay nominal provisioned EBS storage fees
  53. even if your instance is stopped. To do this, you can do so as follows::
  54. >>> conn.stop_instances(instance_ids=['instance-id-1','instance-id-2', ...])
  55. This will request a 'graceful' stop of each of the specified instances. If you
  56. wish to request the equivalent of unplugging your instance(s), simply add
  57. ``force=True`` keyword argument to the call above. Please note that stop
  58. instance is not allowed with Spot instances.
  59. Terminating Instances
  60. ---------------------
  61. Once you are completely done with your instance and wish to surrender both
  62. virtual hardware, root EBS volume and all other underlying components
  63. you can request instance termination. To do so you can use the call bellow::
  64. >>> conn.terminate_instances(instance_ids=['instance-id-1','instance-id-2', ...])
  65. Please use with care since once you request termination for an instance there
  66. is no turning back.