# Creating security groups

This page explains how to create [security groups](https://docs.cloud.google.com/identity/docs/groups.md.txt#group-types). You can create a new security
group or update a Google group to a security group.

## Before you begin

> [!NOTE]
> **Note:** Before you use any of the Cloud Identity APIs, you must set up Cloud Identity. See [Setting up Cloud Identity](https://docs.cloud.google.com/identity/docs/set-up-cloud-identity-admin) for instructions.

Perform the following tasks before proceeding with the information on this page:

- Read the [Groups API overview](https://docs.cloud.google.com/identity/docs/groups.md.txt).

- [Set up the Groups API](https://docs.cloud.google.com/identity/docs/how-to/setup.md.txt).

## Security group requirements

Security groups can only contain the following:

- Users inside or outside of your domain (while associated with a Google service)
- Service accounts inside or outside of your domain
- Security groups inside of your domain

You can't apply the security group label to a Google Group that doesn't meet
these conditions.

Only
[predefined Super Admins or Groups Admins](https://support.google.com/a/answer/2405986)
have the permissions to update security groups.

## Creating a new security group

### REST

To create a security group, call
[`groups.create()`](https://docs.cloud.google.com/identity/docs/reference/rest/v1/groups/create.md.txt) with
an instance of the new group. The group instance must include a `groupKey`,
`Parent`, and `labels` set to `cloudidentity.googleapis.com/groups.security`
and `cloudidentity.googleapis.com/groups.discussion_forum`

### Python

The following example shows a helper function to create a Google Group using
the Python client library:

```python
def create_google_group(service, customer_id, group_id, group_display_name, group_description):
  group_key = {"id": group_id}
  group = {
    "parent": "customers/" + customer_id,
    "description": group_description,
    "displayName": group_display_name,
    "groupKey": group_key,
    # Set the label to specify creation of a Google Group.
    "labels": {
      "cloudidentity.googleapis.com/groups.security": "",
      "cloudidentity.googleapis.com/groups.discussion_forum": ""
    }
  }

  try:
    request = service.groups().create(body=group)
    request.uri += "&initialGroupConfig=WITH_INITIAL_OWNER"
    response = request.execute()
    print(response)
  except Exception as e:
    print(e)
```

## Updating a Google Group to a security group

> [!WARNING]
> **Warning:** A security group cannot be changed back to a Google Group.

### REST

To update a Google Group to a security group, call
[`groups.patch()`](https://docs.cloud.google.com/identity/docs/reference/rest/v1/groups/patch.md.txt) with
`updateMask` set to `cloudidentity.googleapis.com/groups.security` and
`cloudidentity.googleapis.com/groups.discussion_forum`.

**Sample request body**

```json
{
  "labels": {
    "cloudidentity.googleapis.com/groups.security": "",
    "cloudidentity.googleapis.com/groups.discussion_forum": ""
  }
}
```

### Python

The following example shows a helper function to update a Google Group to a
security group using the Python client library:

```python
def add_security_label_to_group(service, group_name):
  group = {
    "labels": {
      "cloudidentity.googleapis.com/groups.security": "",
      "cloudidentity.googleapis.com/groups.discussion_forum": ""
    }
  }
  try:
    request = service.groups().patch(name=group_name, body=group)
    request.uri = request.uri + '&updateMask=labels'
    response = request.execute()
    print(response)
  except Exception as e:
    print(e)
```