| 1 |
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto-ec2-example-key-pairs: |
| 12 |
|
| 13 |
################################# |
| 14 |
Working with Amazon EC2 key pairs |
| 15 |
################################# |
| 16 |
|
| 17 |
This Python example shows you how to: |
| 18 |
|
| 19 |
* Get information about your key pairs |
| 20 |
|
| 21 |
* Create a key pair to access an Amazon EC2 instance |
| 22 |
|
| 23 |
* Delete an existing key pair |
| 24 |
|
| 25 |
The scenario |
| 26 |
============ |
| 27 |
|
| 28 |
Amazon EC2 uses public–key cryptography to encrypt and decrypt login information. Public–key cryptography |
| 29 |
uses a public key to encrypt data, then the recipient uses the private key to decrypt the data. The |
| 30 |
public and private keys are known as a key pair. |
| 31 |
|
| 32 |
In this example, Python code is used to perform several Amazon EC2 key pair management |
| 33 |
operations. The code uses the AWS SDK for Python to manage IAM access keys using these methods of the EC2 client class: |
| 34 |
|
| 35 |
* `describe_key_pairs <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_key_pairs>`_. |
| 36 |
|
| 37 |
* `create_key_pair <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_key_pair>`_. |
| 38 |
|
| 39 |
* `delete_key_pair <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.delete_key_pair>`_. |
| 40 |
|
| 41 |
For more information about the Amazon EC2 key pairs, see `Amazon EC2 Key Pairs <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html>`_ |
| 42 |
in the *Amazon EC2 User Guide for Linux Instances* |
| 43 |
or `Amazon EC2 Key Pairs and Windows Instances <http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-key-pairs.html>`_ |
| 44 |
in the *Amazon EC2 User Guide for Windows Instances*. |
| 45 |
|
| 46 |
All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_. |
| 47 |
|
| 48 |
Prerequisite tasks |
| 49 |
================= |
| 50 |
|
| 51 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 52 |
|
| 53 |
Describe key pairs |
| 54 |
================== |
| 55 |
|
| 56 |
Describe one or more of your key pairs. |
| 57 |
|
| 58 |
The example below shows how to: |
| 59 |
|
| 60 |
* Describe keypairs using |
| 61 |
`describe_key_pairs <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_key_pairs>`_. |
| 62 |
|
| 63 |
Example |
| 64 |
------- |
| 65 |
|
| 66 |
.. code-block:: python |
| 67 |
|
| 68 |
import boto3 |
| 69 |
|
| 70 |
ec2 = boto3.client('ec2') |
| 71 |
response = ec2.describe_key_pairs() |
| 72 |
print(response) |
| 73 |
|
| 74 |
|
| 75 |
Create a key pair |
| 76 |
================= |
| 77 |
|
| 78 |
Create a 2048-bit RSA key pair with the specified name. Amazon EC2 stores the public key and displays |
| 79 |
the private key for you to save to a file. The private key is returned as an unencrypted PEM encoded |
| 80 |
PKCS#8 private key. If a key with the specified name already exists, Amazon EC2 returns an error. |
| 81 |
|
| 82 |
The example below shows how to: |
| 83 |
|
| 84 |
* Create a 2048-bit RSA key pair with a specified name using |
| 85 |
`create_key_pair <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_key_pair>`_. |
| 86 |
|
| 87 |
Example |
| 88 |
------- |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
import boto3 |
| 93 |
|
| 94 |
ec2 = boto3.client('ec2') |
| 95 |
response = ec2.create_key_pair(KeyName='KEY_PAIR_NAME') |
| 96 |
print(response) |
| 97 |
|
| 98 |
|
| 99 |
Delete a key pair |
| 100 |
================= |
| 101 |
|
| 102 |
Delete the specified key pair, by removing the public key from Amazon EC2. |
| 103 |
|
| 104 |
The example below shows how to: |
| 105 |
|
| 106 |
* Delete a key pair by removing the public key from Amazon EC2 using |
| 107 |
`delete_key_pair <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.delete_key_pair>`_. |
| 108 |
|
| 109 |
Example |
| 110 |
------- |
| 111 |
|
| 112 |
.. code-block:: python |
| 113 |
|
| 114 |
import boto3 |
| 115 |
|
| 116 |
ec2 = boto3.client('ec2') |
| 117 |
response = ec2.delete_key_pair(KeyName='KEY_PAIR_NAME') |
| 118 |
print(response) |
| 119 |
|
| 120 |
|