# Create a security group

Create a named set of firewall rules, add and remove rules, and delete a group no instance uses.

What a group filters is in [Networks and addresses](networks-and-addresses.md#security-groups).
Replace the ids with your own.

## Prerequisites

- Permission for `ec2:CreateSecurityGroup`. Adding a rule needs
  `ec2:AuthorizeSecurityGroupIngress` (inbound) or `ec2:AuthorizeSecurityGroupEgress` (outbound);
  removing one `ec2:RevokeSecurityGroupIngress` or `ec2:RevokeSecurityGroupEgress`; deleting the
  group `ec2:DeleteSecurityGroup`.
- In the console, also `ec2:DescribeSecurityGroups` to open **Security groups** (without it the
  page says "Access denied") and a group's page (without it the page is not found).
- Room in the security groups quota.

## Create a group

**Console**
1. Open **Compute**, then **Security groups**, and choose **Create security group**.
2. For **Name**, enter web.
3. (Optional) For **Description**, enter a description.
4. (Optional) Under **Rules**, choose a **Quick add** preset (**SSH**, **HTTP**, **HTTPS**,
   **ICMP**) or **Add rule**. For each rule, set **Direction**, **Protocol**, **Port range**,
   **Source** (or **Destination**) and (optional) **Note**. The console has no default source: a
   rule needs one, and only the **HTTP** and **HTTPS** presets fill in `0.0.0.0/0`.
5. Choose **Create**. The group appears in the list.

Without inbound rules nothing gets in; outbound traffic is allowed until you add an outbound rule.

You can also create a group from the **Security groups** picker of the launch form.

**CLI**
```sh
dcloud ec2 security-groups create --name web --description "public https"
dcloud ec2 security-groups authorize-ingress sg-uuid --protocol tcp \
  --from-port 443 --to-port 443 --cidr-ipv4 0.0.0.0/0
```

**API**
```http
POST /ec2/api/security-groups
{
  "name": "web",
  "description": "public https",
  "rules": [
    { "direction": "inbound", "protocol": "tcp", "fromPort": 22, "toPort": 22, "cidrIpv4": "203.0.113.0/24" },
    { "direction": "inbound", "protocol": "tcp", "fromPort": 443, "toPort": 443, "cidrIpv4": "0.0.0.0/0" }
  ]
}
```

**Terraform**
```hcl
resource "dcloud_security_group" "web" {
  name        = "web"
  description = "public https"
}

resource "dcloud_security_group_rule" "https" {
  security_group_id = dcloud_security_group.web.id
  direction         = "inbound"
  protocol          = "tcp"
  from_port         = 443
  to_port           = 443
  cidr_ipv4         = "0.0.0.0/0"
}
```

## Rules

| Field                | Accepted values                                                                               | Default     |
| -------------------- | --------------------------------------------------------------------------------------------- | ----------- |
| `direction`          | `inbound`, `outbound`                                                                         | required    |
| `protocol`           | `tcp`, `udp`, `icmp`                                                                          | required    |
| `fromPort`, `toPort` | 0–65535; ignored for ICMP. Without `fromPort`: every port; without `toPort`: `fromPort` alone | none        |
| `cidrIpv4`           | A CIDR block, IPv4 or IPv6, or a single address (stored as `/32` or `/128`)                   | `0.0.0.0/0` |
| `description`        | Up to 255 characters                                                                          | none        |

Another value in `cidrIpv4` is refused with `400` "Source must be an IP address (2.31.205.75) or a
network in CIDR notation (10.0.0.0/16, 0.0.0.0/0)".

## Add or remove a rule

**Console**
1. Open the group, then choose **Inbound rules** or **Outbound rules**.
2. Choose **Add rule**. In **Add rule**, choose **Inbound** or **Outbound**, a **Type** (a service
   such as **SSH**, **PostgreSQL** or **Custom TCP**), the ports for a custom type, the
   **Source CIDR** (or **Destination CIDR**, required) and (optional) a **Description**, then
   choose **Add rule**.
3. To remove a rule, choose **Remove rule** in its row menu, then **Remove**.

**CLI**
```sh
dcloud ec2 security-groups authorize-egress sg-uuid --protocol tcp --from-port 443 --to-port 443
dcloud ec2 security-groups revoke-rule sg-uuid rule-uuid
```

**API**
```http
POST /ec2/api/security-groups/0ea56c91-0000-4000-8000-000000000002/rules
{ "direction": "outbound", "protocol": "tcp", "fromPort": 443, "toPort": 443 }
```

Remove one with `DELETE /ec2/api/security-groups/{id}/rules/{ruleId}`.

**Terraform**
Add or remove `dcloud_security_group_rule` resources.

A rule change is applied to every instance the group is attached to before the call returns. If it
cannot be applied, the call answers `502` ("Rule saved but not applied: …" or "Rule removed but the
change is not applied: …") and the group shows the error until a change succeeds.

## Delete a group

**Console**
1. Open the group, then choose **Danger zone**.
2. Choose **Delete security group**, enter the group name, and choose **Delete security group**.

**CLI**
```sh
dcloud ec2 security-groups delete sg-uuid
```

**API**
```http
DELETE /ec2/api/security-groups/0ea56c91-0000-4000-8000-000000000002
```

**Terraform**
Remove the `dcloud_security_group` resource and run `terraform apply`.

The group and its rules are removed. The delete is refused with `409` "Security group is attached
to … instance(s); detach it first" while an instance that is not terminated has it; the console
says so beside **Delete security group** instead.

## Result

A group made here is not bound to a network (`vpcId` is null): any group can be attached to any
instance; see [Change an instance's security groups](change-security-groups.md).

A group that a Kubernetes cluster manages cannot be deleted: `409` "This group belongs to
Kubernetes cluster …; it goes when the cluster is deleted." Its rules can be changed, except on
the cluster's control-plane group, which is refused with `409` (change who may reach the API
endpoint in the cluster's networking settings instead).

## Related

- [Networks and addresses](networks-and-addresses.md)
- `ec2.security-groups.create` · `dcloud ec2 security-groups revoke-rule` · `dcloud_security_group` · `dcloud_security_group_rule`
