Skip to content

create_security_group

create_security_group(
    *,
    config_overrides: Optional[EC2ClientConfig] = None,
    description: Optional[String] = None,
    group_name: Optional[String] = None,
    vpc_id: Optional[VpcId] = None,
    tag_specifications: Optional[TagSpecificationList] = None,
    dry_run: Optional[Boolean] = None,
) -> CreateSecurityGroupResult

Creates a security group.

A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. For more information, see Amazon EC2 security groups in the Amazon EC2 User Guide and Security groups for your VPC in the Amazon VPC User Guide.

When you create a security group, you specify a friendly name of your choice. You can't have two security groups for the same VPC with the same name.

You have a default security group for use in your VPC. If you don't specify a security group when you launch an instance, the instance is launched into the appropriate default security group. A default security group includes a default rule that grants instances unrestricted network access to each other.

You can add or remove rules from your security groups using AuthorizeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, and RevokeSecurityGroupEgress.

For more information about VPC security group limits, see Amazon VPC Limits.

Parameters:

  • description (Optional[String], default: None ) –

    A description for the security group.

    Constraints: Up to 255 characters in length

    Valid characters: a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*

  • group_name (Optional[String], default: None ) –

    The name of the security group. Names are case-insensitive and must be unique within the VPC.

    Constraints: Up to 255 characters in length. Can't start with sg-.

    Valid characters: a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*

  • vpc_id (Optional[VpcId], default: None ) –

    The ID of the VPC. Required for a nondefault VPC.

  • tag_specifications (Optional[TagSpecificationList], default: None ) –

    The tags to assign to the security group.

  • dry_run (Optional[Boolean], default: None ) –

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Raises:

Examples:

To create a security group for a VPC This example creates a security group for the specified VPC.

>>> client.create_security_group(description='My security group', group_name='my-security-group', vpc_id='vpc-1a2b3c4d')
Source code in docs_src/capo_ec2/_services/ec2.py
def create_security_group(
    self,
    *,
    config_overrides: Optional[EC2ClientConfig] = None,
    description: Optional["capo_ec2.types.string.String"] = None,
    group_name: Optional["capo_ec2.types.string.String"] = None,
    vpc_id: Optional["capo_ec2.types.vpc_id.VpcId"] = None,
    tag_specifications: Optional[
        "capo_ec2.types.tag_specification_list.TagSpecificationList"
    ] = None,
    dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
) -> "capo_ec2.types.create_security_group_result.CreateSecurityGroupResult":
    """<p>Creates a security group.</p> <p>A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html">Amazon EC2 security groups</a> in the <i>Amazon EC2 User Guide</i> and <a href="https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_SecurityGroups.html">Security groups for your VPC</a> in the <i>Amazon VPC User Guide</i>.</p> <p>When you create a security group, you specify a friendly name of your choice. You can't have two security groups for the same VPC with the same name.</p> <p>You have a default security group for use in your VPC. If you don't specify a security group when you launch an instance, the instance is launched into the appropriate default security group. A default security group includes a default rule that grants instances unrestricted network access to each other.</p> <p>You can add or remove rules from your security groups using <a>AuthorizeSecurityGroupIngress</a>, <a>AuthorizeSecurityGroupEgress</a>, <a>RevokeSecurityGroupIngress</a>, and <a>RevokeSecurityGroupEgress</a>.</p> <p>For more information about VPC security group limits, see <a href="https://docs.aws.amazon.com/vpc/latest/userguide/amazon-vpc-limits.html">Amazon VPC Limits</a>.</p>

    Args:
        description: <p>A description for the security group.</p> <p>Constraints: Up to 255 characters in length</p> <p>Valid characters: a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*</p>
        group_name: <p>The name of the security group. Names are case-insensitive and must be unique within the VPC.</p> <p>Constraints: Up to 255 characters in length. Can't start with <code>sg-</code>.</p> <p>Valid characters: a-z, A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*</p>
        vpc_id: <p>The ID of the VPC. Required for a nondefault VPC.</p>
        tag_specifications: <p>The tags to assign to the security group.</p>
        dry_run: <p>Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is <code>DryRunOperation</code>. Otherwise, it is <code>UnauthorizedOperation</code>.</p>

    Raises:
        capo_ec2.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To create a security group for a VPC
        This example creates a security group for the specified VPC.

        >>> client.create_security_group(description='My security group', group_name='my-security-group', vpc_id='vpc-1a2b3c4d')
    """

    def _handler(
        req: "OperationRequest[capo_ec2.types.create_security_group_request.CreateSecurityGroupRequest]",
    ) -> OperationResponse[
        "capo_ec2.types.create_security_group_result.CreateSecurityGroupResult"
    ]:
        import capo_ec2._operations.amazon_ec2.create_security_group

        output, http_response = (
            capo_ec2._operations.amazon_ec2.create_security_group.create_security_group(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("CreateSecurityGroup")
    )
    input_: capo_ec2.types.create_security_group_request.CreateSecurityGroupRequest = {}
    if description is not None:
        input_["description"] = description
    if group_name is not None:
        input_["group_name"] = group_name
    if vpc_id is not None:
        input_["vpc_id"] = vpc_id
    if tag_specifications is not None:
        input_["tag_specifications"] = tag_specifications
    if dry_run is not None:
        input_["dry_run"] = dry_run

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output