A security group is a collection of access control rules for cloud resources, such as cloud servers, containers, and databases, that have the same security protection requirements and that are mutually trusted. After a security group is created, you can configure access rules that will apply to all cloud resources added to this security group.
Many cloud services can use security groups, such as ECS, RDS, DDS, DDM, MRS, CCE, CSS, DCS (Redis 3.0), DMS, WAF, and SFS.
When creating an instance (for example, an ECS), you must associate it with a security group. If there are no security groups yet, a default security group will be automatically created and associated with the instance. For details, see Default Security Groups. You can also create a security group based on service requirements and associate it with the instance. A cloud resource can be associated with multiple security groups, and traffic to and from the cloud resource is matched by priority in a descending order.
Each security group can have both inbound and outbound rules. You need to specify the source, port, and protocol for each inbound rule and specify the destination, port, and protocol for each outbound rule to control the inbound and outbound traffic to and from the instances in the security group. As shown in Figure 1, you have a VPC (VPC-A) with a subnet (Subnet-A) in region A. An ECS (ECS-A) is running in Subnet-A and associated with security group Sg-A.
Figure 1 A security group architecture

The source or destination can be an IP address, security group, or IP address group.
If you add, delete, or update rules in a security group, or add or remove instances in a security group, the details are as follows:
Scenario | Clearing Policy |
|---|---|
Adding instances to security group A |
|
Removing instances from security group A |
|
Adding rules to security group A | If a Deny rule is added in the inbound or outbound direction, clear inbound connection tracking entries of all instances in security group A. |
Deleting rules from security group A | If an Allow rule is deleted in the inbound direction, clear inbound connection tracking entries of all instances in security group A. |
Modifying rules in security group A | If the priority, action, protocol, port, or source address of a rule is modified in the inbound direction, clear inbound connection tracking entries of all instances in security group A. |
Changing IP address entries in an IP address group | If an IP address group is associated with an inbound rule in security group A, deleting or adding an IP address entry from or to the IP address group will clear inbound connection tracking entries of all instances in security group A. |
After a persistent connection is disconnected, new connections will not be established immediately until the timeout period of connection tracking expires. For example, after an ICMP persistent connection is disconnected, a new connection will be established and a new rule will be applied when the timeout period (30s) expires.
The rules in Table 2 ensure that instances in a security group can communicate with each other. Do not delete or modify these rules.
Direction | Action | Type | Protocol & Port | Source/Destination |
|---|---|---|---|---|
Inbound | Allow | IPv4 | All | Source: current security group |
Inbound | Allow | IPv6 | All | Source: current security group |
Outbound | Allow | IPv4 | All | Destination: 0.0.0.0/0 |
Outbound | Allow | IPv6 | All | Destination: ::/0 |
An instance can have multiple security groups associated, and a security group can contain multiple security group rules. Security group rules are matched first by priority and then by action. Deny rules take precedence over allow rules. The following takes inbound traffic as an example to match security group rules:
If the sequence number of security group A is 1 and that of security group B is 2, the priority of security group A is higher than that of security group B. Traffic preferentially matches the inbound rules of security group A.
If the priority of security group rule A is 1 and that of security group rule B is 2, the priority of security group rule A is higher than that of security group rule B. Therefore, traffic preferentially matches security group rule A.
Figure 2 Security group matching sequence

You can allow given IP addresses to access instances in a security group, or allow access from another security group to enable instances in different security groups to communicate with each other. You can add security group rules to flexibly control the traffic in and out of a network to ensure network security. The following provides some examples on how security groups can be used.
In Figure 3, there are two subnets (Subnet-A and Subnet-B) in VPC-X. ECSs in Subnet-A are associated with Sg-A because these ECSs are used to run the same services and have the same network communication requirements. Similarly, ECSs in Subnet-B are associated with security group Sg-B.
Figure 3 Allowing traffic from given IP addresses and security groups

Security Group Examples lists more security group rule configuration examples.
If you use an intermediate network instance to forward traffic between instances in different subnets, setting the source of the inbound rule to the security group associated with the peer instance does not allow the instances to communicate with each other. To enable communications, set the source to the private IP address or subnet CIDR block of the intermediate network instance. For example, to connect ECSs in Subnet-A and Subnet-B in Figure 4, set the source of the inbound rule to the virtual IP address.
In Figure 4, VPC-X has two subnets: Subnet-A and Subnet-B. ECSs in Subnet-A are associated with security group Sg-A, and ECSs in Subnet-B are associated with security group Sg-B. ECS-A01 and ECS-A02 work in active/standby pair, forming a Keepalived HA cluster. The ECSs use virtual IP address 192.168.0.21 to communicate with external networks.
Figure 4 Allowing traffic from a virtual IP address

Security Group Examples lists more security group rule configuration examples.
In Figure 5, VPC-A and VPC-B in region A are connected by VPC peering connection peering-AB. After routes are configured for the VPC peering connection, Subnet-A01 and Subnet-B01 can communicate with each other. However, the ECSs in the two subnets are associated with different security groups. To allow ECSs in Sg-A and Sg-B to communicate with each other, you can add the following rules:
Figure 5 Allowing communications between ECSs in two VPCs connected by a VPC peering connection

Security Group Examples lists more security group rule configuration examples.
Figure 6 Process of using a security group

No. | Step | Description | Reference |
|---|---|---|---|
1 | Create a security group. | When creating a security group, you can use the preset rules. For details about the preset security group rules, see Table 1. | |
2 | Configure security group rules. | After a security group is created, if its rules cannot meet your service requirements, you can add new rules to the security group or modify original rules. | |
3 | Add instances to the security group. | When you create an instance, the system automatically adds the instance to a security group for protection. If one security group cannot meet your requirements, you can add an instance to multiple security groups. |
The limits on outbound security group rules are the same as those on inbound rules.
For example, to add inbound IPv4 rules to a security group (Sg-A), you can refer to Table 4 for rules that meet the restrictions. Of these rules, rule A02 uses inconsecutive ports (TCP: 22,25,27) and security group Sg-B as the source. In this case, only one quota is occupied.
Rule No. | Action | Type | Protocol & Port | Source |
|---|---|---|---|---|
Rule A01 | Allow | IPv4 | All | Current security group: Sg-A |
Rule A02 | Allow | IPv4 | TCP: 22,25,27 | Another security group: Sg-B |
Rule A03 | Allow | IPv4 | TCP: 80-82 | IP address group: ipGroup-A |
Rule A04 | Allow | IPv4 | TCP: 22-24,25 | IP address: 192.168.0.0/16 |
Transfer Client IP Address is enabled for the listeners of a load balancer.
The load balancer can still forward traffic to backend servers, even if there is a rule that denies traffic from the load balancer to the backend servers.
If a security group rule does not take effect after being configured, see Why Are My Security Group Rules Not Working?