CreateSecurityGroup - Amazon Elastic Compute Cloud

CreateSecurityGroup

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 Elastic Compute Cloud User Guide and Security groups for your VPC in the Amazon Virtual Private Cloud User Guide.

When you create a security group, you specify a friendly name of your choice. You can have a security group for use in EC2-Classic with the same name as a security group for use in a VPC. However, you can't have two security groups for use in EC2-Classic with the same name or two security groups for use in a VPC with the same name.

You have a default security group for use in EC2-Classic and 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.

Note

We are retiring EC2-Classic. We recommend that you migrate from EC2-Classic to a VPC. For more information, see Migrate from EC2-Classic to a VPC in the Amazon Elastic Compute Cloud User Guide.

Request Parameters

The following parameters are for this specific action. For more information about required and optional parameters that are common to all actions, see Common Query Parameters.

DryRun

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.

Type: Boolean

Required: No

GroupDescription

A description for the security group.

Constraints: Up to 255 characters in length

Constraints for EC2-Classic: ASCII characters

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

Type: String

Required: Yes

GroupName

The name of the security group.

Constraints: Up to 255 characters in length. Cannot start with sg-.

Constraints for EC2-Classic: ASCII characters

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

Type: String

Required: Yes

TagSpecification.N

The tags to assign to the security group.

Type: Array of TagSpecification objects

Required: No

VpcId

[EC2-VPC] The ID of the VPC. Required for EC2-VPC.

Type: String

Required: No

Response Elements

The following elements are returned by the service.

groupId

The ID of the security group.

Type: String

requestId

The ID of the request.

Type: String

tagSet

The tags assigned to the security group.

Type: Array of Tag objects

Errors

For information about the errors that are common to all actions, see Common client error codes.

Examples

Example for EC2-Classic

This example creates a security group named websrv for EC2-Classic.

Sample Request

https://ec2.amazonaws.com/?Action=CreateSecurityGroup &GroupName=websrv &GroupDescription=Web Servers &AUTHPARAMS

Sample Response

<CreateSecurityGroupResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <return>true</return> <groupId>sg-1a2b3c4d</groupId> </CreateSecurityGroupResponse>

Example for EC2-VPC

This example creates a security group named WebServerSG for the specified VPC.

Sample Request

https://ec2.amazonaws.com/?Action=CreateSecurityGroup &GroupName=WebServerSG &GroupDescription=Web Servers &VpcId=vpc-3325caf2 &AUTHPARAMS

Sample Response

<CreateSecurityGroupResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/"> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <return>true</return> <groupId>sg-0a42d66a</groupId> </CreateSecurityGroupResponse>

See Also

For more information about using this API in one of the language-specific AWS SDKs, see the following: