| 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-security-group: |
| 12 |
|
| 13 |
########################################## |
| 14 |
Working with security groups in Amazon EC2 |
| 15 |
########################################## |
| 16 |
|
| 17 |
This Python example shows you how to: |
| 18 |
|
| 19 |
* Get information about your security groups |
| 20 |
|
| 21 |
* Create a security group to access an Amazon EC2 instance |
| 22 |
|
| 23 |
* Delete an existing security group |
| 24 |
|
| 25 |
The scenario |
| 26 |
============ |
| 27 |
|
| 28 |
An Amazon EC2 security group acts as a virtual firewall that controls the traffic for one or more instances. |
| 29 |
You add rules to each security group to allow traffic to or from its associated instances. You can |
| 30 |
modify the rules for a security group at any time; the new rules are automatically applied to all |
| 31 |
instances that are associated with the security group. |
| 32 |
|
| 33 |
In this example, Python code is used to perform several Amazon EC2 operations involving security groups. |
| 34 |
The code uses the AWS SDK for Python to manage IAM access keys using these methods of the EC2 |
| 35 |
client class: |
| 36 |
|
| 37 |
* `describe_security_groups <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_security_groups>`_. |
| 38 |
|
| 39 |
* `authorize_security_group_ingress <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.authorize_security_group_ingress>`_. |
| 40 |
|
| 41 |
* `create_security_group <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_security_group>`_. |
| 42 |
|
| 43 |
* `delete_security_group <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.delete_security_group>`_. |
| 44 |
|
| 45 |
For more information about the Amazon EC2 security groups, see |
| 46 |
`Amazon EC2 Amazon Security Groups for Linux Instances <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html>`_ |
| 47 |
in the *Amazon EC2 User Guide for Linux Instances* or |
| 48 |
`Amazon EC2 Security Groups for Windows Instances <http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/using-network-security.html>`_ |
| 49 |
in the *Amazon EC2 User Guide for Windows Instances*. |
| 50 |
|
| 51 |
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>`_. |
| 52 |
|
| 53 |
Prerequisite tasks |
| 54 |
================== |
| 55 |
|
| 56 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 57 |
|
| 58 |
Describe security groups |
| 59 |
======================== |
| 60 |
Describe one or more of your security groups. |
| 61 |
|
| 62 |
A security group is for use with instances either in the EC2-Classic platform or in a specific VPC. |
| 63 |
For more information, see `Amazon EC2 Security Groups <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html>`_ |
| 64 |
in the *Amazon Elastic Compute Cloud User Guide* and |
| 65 |
`Security Groups for Your VPC <http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html>`_ |
| 66 |
in the *Amazon Virtual Private Cloud User Guide*. |
| 67 |
|
| 68 |
.. warning:: |
| 69 |
We are retiring EC2-Classic on August 15, 2022. We recommend that you |
| 70 |
migrate from EC2-Classic to a VPC. For more information, see *Migrate from |
| 71 |
EC2-Classic to a VPC* in the `Amazon EC2 User Guide for Linux Instances <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/vpc-migrate.html>`_ or the `Amazon EC2 User Guide for Windows Users <http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/vpc-migrate.html>`_. Also see the blog post `EC2-Classic Networking is Retiring – Here's How to Prepare <https://aws.amazon.com/blogs/aws/ec2-classic-is-retiring-heres-how-to-prepare/>`_. |
| 72 |
|
| 73 |
The example below shows how to: |
| 74 |
|
| 75 |
* Describe a Security Group using |
| 76 |
`describe_security_groups <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_security_groups>`_. |
| 77 |
|
| 78 |
Example |
| 79 |
------- |
| 80 |
|
| 81 |
.. code:: python |
| 82 |
|
| 83 |
import boto3 |
| 84 |
from botocore.exceptions import ClientError |
| 85 |
|
| 86 |
ec2 = boto3.client('ec2') |
| 87 |
|
| 88 |
try: |
| 89 |
response = ec2.describe_security_groups(GroupIds=['SECURITY_GROUP_ID']) |
| 90 |
print(response) |
| 91 |
except ClientError as e: |
| 92 |
print(e) |
| 93 |
|
| 94 |
Create a security group and rules |
| 95 |
================================= |
| 96 |
|
| 97 |
* Create a security group. |
| 98 |
|
| 99 |
* Add one or more ingress rules to a security group. |
| 100 |
|
| 101 |
Rule changes are propagated to instances within the security group as quickly as possible. However, |
| 102 |
a small delay might occur. |
| 103 |
|
| 104 |
The example below shows how to: |
| 105 |
|
| 106 |
* Create a Security Group using |
| 107 |
`create_security_group <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_security_group>`_. |
| 108 |
|
| 109 |
* Add an ingress rule to a security group using |
| 110 |
`authorize_security_group_ingress <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.authorize_security_group_ingress>`_. |
| 111 |
|
| 112 |
Example |
| 113 |
------- |
| 114 |
|
| 115 |
.. code-block:: python |
| 116 |
|
| 117 |
import boto3 |
| 118 |
from botocore.exceptions import ClientError |
| 119 |
|
| 120 |
ec2 = boto3.client('ec2') |
| 121 |
|
| 122 |
response = ec2.describe_vpcs() |
| 123 |
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') |
| 124 |
|
| 125 |
try: |
| 126 |
response = ec2.create_security_group(GroupName='SECURITY_GROUP_NAME', |
| 127 |
Description='DESCRIPTION', |
| 128 |
VpcId=vpc_id) |
| 129 |
security_group_id = response['GroupId'] |
| 130 |
print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id)) |
| 131 |
|
| 132 |
data = ec2.authorize_security_group_ingress( |
| 133 |
GroupId=security_group_id, |
| 134 |
IpPermissions=[ |
| 135 |
{'IpProtocol': 'tcp', |
| 136 |
'FromPort': 80, |
| 137 |
'ToPort': 80, |
| 138 |
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}, |
| 139 |
{'IpProtocol': 'tcp', |
| 140 |
'FromPort': 22, |
| 141 |
'ToPort': 22, |
| 142 |
'IpRanges': [{'CidrIp': '0.0.0.0/0'}]} |
| 143 |
]) |
| 144 |
print('Ingress Successfully Set %s' % data) |
| 145 |
except ClientError as e: |
| 146 |
print(e) |
| 147 |
|
| 148 |
Delete a security group |
| 149 |
======================= |
| 150 |
|
| 151 |
If you attempt to delete a security group that is associated with an instance, or is referenced by |
| 152 |
another security group, the operation fails with :code:`InvalidGroup.InUse` in EC2-Classic or :code:`DependencyViolation` |
| 153 |
in EC2-VPC. |
| 154 |
|
| 155 |
.. warning:: |
| 156 |
We are retiring EC2-Classic on August 15, 2022. We recommend that you |
| 157 |
migrate from EC2-Classic to a VPC. For more information, see *Migrate from |
| 158 |
EC2-Classic to a VPC* in the `Amazon EC2 User Guide for Linux Instances <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/vpc-migrate.html>`_ or the `Amazon EC2 User Guide for Windows Users <http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/vpc-migrate.html>`_. Also see the blog post `EC2-Classic Networking is Retiring – Here's How to Prepare <https://aws.amazon.com/blogs/aws/ec2-classic-is-retiring-heres-how-to-prepare/>`_. |
| 159 |
|
| 160 |
The example below shows how to: |
| 161 |
|
| 162 |
* Delete a security group using |
| 163 |
`delete_security_group <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.delete_security_group>`_. |
| 164 |
|
| 165 |
Example |
| 166 |
------- |
| 167 |
|
| 168 |
.. code-block:: python |
| 169 |
|
| 170 |
import boto3 |
| 171 |
from botocore.exceptions import ClientError |
| 172 |
|
| 173 |
# Create EC2 client |
| 174 |
ec2 = boto3.client('ec2') |
| 175 |
|
| 176 |
# Delete security group |
| 177 |
try: |
| 178 |
response = ec2.delete_security_group(GroupId='SECURITY_GROUP_ID') |
| 179 |
print('Security Group Deleted') |
| 180 |
except ClientError as e: |
| 181 |
print(e) |
| 182 |
|
| 183 |
|