Adding a new rule to a security group
You do not need to restart a VM when adding or deleting rules. The rules are applied to all the resources associated with a group at the same time.
To add a rule:
-
In the management console
, go to the folder where you need to update the security group. -
In the list of services, select Virtual Private Cloud.
-
In the left-hand panel, select
Security groups. -
Click
in the row of the security group you need to add a rule to. Select Edit. -
Under Rules, create traffic management rules:
-
Select the Egress or Ingress tab.
-
Click Add.
-
In the Port range field of the window that opens, specify a single port or a range of ports open for inbound or outbound traffic.
-
In the Protocol field, specify the appropriate protocol or leave
Anyto allow traffic transmission over any protocol. -
In the Destination name or Source field, select the rule purpose:
-
CIDR: Rule will apply to the range of IP addresses. In the CIDR blocks field, specify the CIDRs and masks of subnets traffic will move to/from. To add multiple CIDRs, click Add. -
Security group. Select one of the following:Current: Rule will apply to the VMs from the current group.From list: Rule will apply to the VMs from the selected group. Make sure that source and destination IP addresses used for traffic exchange are from private ranges. For more information, see Concepts.
-
Load balancer healthchecks: Rule that allows checking the health of resources from Network Load Balancer or Application Load Balancer.
-
-
-
Click Save.
-
Click Save once again.
To add a rule to an existing group:
-
Get the name or ID of the group to edit:
yc vpc security-groups listResult:
+----------------------+---------------------------------+------------------------------------+----------------------+ | ID | NAME | DESCRIPTION | NETWORK-ID | +----------------------+---------------------------------+------------------------------------+----------------------+ | enp9bmjge93b******** | default-sg-enp509crtquf******** | Default security group for network | enp509crtquf******** | | enp9rs9u4h6j******** | sg-1 | | enp509crtquf******** | | enp9d8m73d1c******** | sg-2 | | enp509crtquf******** | +----------------------+---------------------------------+------------------------------------+----------------------+ -
Add a rule using the
update-rulescommand and the--add-ruleparameter:yc vpc security-group update-rules <group_name_or_ID> --add-rule "direction=ingress,port=443,protocol=tcp,v4-cidrs=[10.0.0.0/24,10.10.0.0/24]"Result:
done (12s) id: enp9rs9i4h9j******** folder_id: b1gau98l79li******** created_at: "2022-06-24T15:46:31Z" name: sg-1 network_id: enp559cr9q9f******** status: ACTIVE rules: - id: enp68o6cdi87******** direction: INGRESS ports: from_port: "443" to_port: "443" protocol_name: TCP protocol_number: "6" cidr_blocks: v4_cidr_blocks: - 10.0.0.0/24 - 10.10.0.0/24 ...Note
You can use
predefined=self_security_groupto apply the rule to the VMs inside the security group you are updating.For more information about the
Selfrule, see the types of security group rules.To learn about the
--add-ruleparameter, run theyc vpc security-group update-rules --helpcommand.
If you do not have Terraform yet, install it and configure the Yandex Cloud provider.
With Terraform
Terraform is distributed under the Business Source License
For more information about the provider resources, see the relevant documentation on the Terraform
-
Open the Terraform configuration file and add the
ingressoregresssection to the security group description:... resource "yandex_vpc_security_group" "test-sg" { name = "Test security group" description = "Description for security group" network_id = "${yandex_vpc_network.lab-net.id}" ingress { protocol = "TCP" description = "Rule description 1" v4_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24"] port = 8080 } egress { protocol = "ANY" description = "Rule description 2" v4_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24"] from_port = 8090 to_port = 8099 } egress { protocol = "UDP" description = "rule3 description" v4_cidr_blocks = ["10.0.1.0/24"] from_port = 8090 to_port = 8099 } } ...For more information about
yandex_vpc_security_groupproperties, see this Terraform article. -
Check the configuration using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Confirm the changes: type
yesinto the terminal and press Enter.You can check the security group update using the management console
or this CLI command:yc vpc security-group get <security_group_name>
Adding a new rule using the yandex_vpc_security_group_rule resource
You can also add a new rule to an existing security group using the yandex_vpc_security_group_rule resource.
Warning
Both methods provide the same result but they are incompatible: concurrent use of the yandex_vpc_security_group_rule and yandex_vpc_security_group resources will cause a configuration rule conflict.
-
In the configuration file, describe the following parameters:
security_group_binding: Security group ID.direction: Incoming or outgoing traffic. The possible values areingressoregress.description: Rule description.v4_cidr_blocks: List of CIDRs and masks of subnets the traffic will come to or from.port: Traffic port.from_port: First port in the traffic port range.to_port: Last port in the traffic port range.protocol: Traffic transmission protocol. The possible values areTCP,UDP,ICMP, orANY.
... resource "yandex_vpc_security_group_rule" "rule1" { security_group_binding = <security_group_ID> direction = "ingress" description = "<rule_description>" v4_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24"] port = 8080 protocol = "TCP" } resource "yandex_vpc_security_group_rule" "rule2" { security_group_binding = yandex_vpc_security_group.group1.id direction = "egress" description = "rule2 description" v4_cidr_blocks = ["10.0.1.0/24"] from_port = 8090 to_port = 8099 protocol = "UDP" } ...For more information about
yandex_vpc_security_group_ruleproperties, see this Terraform article. -
Check the configuration using this command:
terraform validateIf the configuration is correct, you will get this message:
Success! The configuration is valid. -
Run this command:
terraform planYou will see a detailed list of resources. No changes will be made at this step. If the configuration contains any errors, Terraform will show them.
-
Apply the changes:
terraform apply -
Confirm the changes: type
yesinto the terminal and press Enter.You can check the security group update using the management console
or this CLI command:yc vpc security-group get <security_group_name>
To add a rule, use the updateRules REST API method for the SecurityGroup resource or the SecurityGroupService/UpdateRules gRPC API call, and provide the following in the request:
-
ID of the security group you want to add rules to, in the
securityGroupIdparameter.To get the security group ID, use the list REST API method for the SecurityGroup resource or the SecurityGroupService/List gRPC API call and provide the folder ID in the
folderIdrequest parameter.To learn how to find out the folder ID, see Getting the folder ID.
-
New security group rules, in the
additionRuleSpecs[]array:-
Traffic direction for which the rule is created, in the
additionRuleSpecs[].directionparameter. The possible values are:ingress: Incoming traffic.egress: Outgoing traffic.
-
Name of the traffic transmission protocol, in the
additionRuleSpecs[].protocolNameparameter. The possible values aretcp,udp,icmp,esp,ah, orany. -
List of CIDRs and masks of subnets the traffic will come to or from, in the
additionRuleSpecs[].cidrBlocks.v4CidrBlocks[]parameter. If you set the rule for the traffic to a security group, provide the security group ID in theadditionRuleSpecs[].securityGroupIdparameter instead. -
First port in the traffic port range, in the
additionRuleSpecs[].ports.fromPortparameter. The values range from0to65535. -
Last port in the traffic port range, in the
additionRuleSpecs[].ports.toPortparameter. The values range from0to65535.
-