简体   繁体   中英

Create AWS SG using python boto3

I'm trying to create a security group within a specific vpc by passing the variables during the code execution but I get the following errors, when I run the following command with the variables.

./create_sg.py vpc-e79569b2 dev_test_sg testing tcp 22 22 0.0.0.0/0

An error occurred (InvalidVpcID.NotFound) when calling the CreateSecurityGroup operation: The vpc ID 'VPC_ID' does not exist

    #!/usr/bin/env python

import sys
import boto3
from botocore.exceptions import ClientError
region = "us-west-1"

VPC_ID=sys.argv[0]
SECURITY_GROUP_NAME=sys.argv[1]
DESCRIPTION=sys.argv[2]
IP_PROTOCOL_1=sys.argv[3]
FROM_PORT_1=sys.argv[4]
TO_PORT_1=sys.argv[5]
CIDR_IP_1=sys.argv[6]


ec2 = boto3.client('ec2')

response = ec2.describe_vpcs()

vpc_id = 'VPC_ID'

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': 'IP_PROTOCOL_1',
             'FromPort': FROM_PORT_1,
             'ToPort': TO_PORT_1,
             'IpRanges': [{'CidrIp': 'CIDR_IP_1'}]}
        ])
    print('Ingress Successfully Set %s' % data)
except ClientError as e:
    print(e)

Please review the code and let me know if I need to change something in there to successfully create a security group.

This line is all wrong...

response = ec2.create_security_group(GroupName='SECURITY_GROUP_NAME',Description='DESCRIPTION',VpcId=vpc_id)

IMO it should be:

response = ec2.create_security_group(GroupName=SECURITY_GROUP_NAME,Description=DESCRIPTION,VpcId=VPC_ID)


Ok, here is the complete code with corrections made to the string literals:

#!/usr/bin/env python

import sys
import boto3
from botocore.exceptions import ClientError
region = "us-west-1"

VPC_ID=sys.argv[0]
SECURITY_GROUP_NAME=sys.argv[1]
DESCRIPTION=sys.argv[2]
IP_PROTOCOL_1=sys.argv[3]
FROM_PORT_1=sys.argv[4]
TO_PORT_1=sys.argv[5]
CIDR_IP_1=sys.argv[6]

ec2 = boto3.client('ec2')

response = ec2.describe_vpcs()

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': IP_PROTOCOL_1,
             'FromPort': int(FROM_PORT_1),
             'ToPort': int(TO_PORT_1),
             'IpRanges': [{'CidrIp': CIDR_IP_1}]}
        ])
    print('Ingress Successfully Set %s' % data)
except ClientError as e:
    print(e)

尝试删除'VPC_ID'周围的引号:

vpc_id = VPC_ID

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