AWS Code Sample
Catalog demonstrates how to create an Amazon EC2 security group.

# Copyright 2010-2018, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the 'License'). # You may not use this file except in compliance with the License. A copy of the # License is located at # # # # This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. import boto3 from botocore.exceptions import ClientError ec2 = boto3.client('ec2') response = ec2.describe_vpcs() vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') try: response = ec2.create_security_group(GroupName='SECURITY_GROUP_NAME', Description='DESCRIPTION', VpcId=vpc_id) security_group_id = response['GroupId'] print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id)) data = ec2.authorize_security_group_ingress( GroupId=security_group_id, IpPermissions=[ {'IpProtocol': 'tcp', 'FromPort': 80, 'ToPort': 80, 'IpRanges': [{'CidrIp': ''}]}, {'IpProtocol': 'tcp', 'FromPort': 22, 'ToPort': 22, 'IpRanges': [{'CidrIp': ''}]} ]) print('Ingress Successfully Set %s' % data) except ClientError as e: print(e)

Sample Details

Service: ec2

Last tested: 2018-06-25

Author: jschwarzwalder (AWS)

Type: full-example

On this page: