

# Subnets and subnet groups
<a name="SubnetGroups"></a>

A *subnet group* is a collection of subnets (typically private) that you can designate for your node-based clusters running in an Amazon Virtual Private Cloud (VPC) environment.

If you create a node-based cluster in an Amazon VPC, you must use a subnet group. ElastiCache uses that subnet group to choose a subnet and IP addresses within that subnet to associate with your nodes.

ElastiCache provides a default IPv4 subnet group or you can choose to create a new one. For IPv6, you need to create a subnet group with an IPv6 CIDR block. If you choose **dual stack**, you then must select a Discovery IP type, either IPv6 or IPv4.

ElastiCache Serverless does not use a subnet group resource, and instead takes a list of subnets directly during creation.

This section covers how to create and leverage subnets and subnet groups to manage access to your ElastiCache resources. 

For more information about subnet group usage in an Amazon VPC environment, see [Accessing your ElastiCache cluster or replication group](accessing-elasticache.md).

**Topics**
+ [Creating a subnet group](SubnetGroups.Creating.md)
+ [Assigning a subnet group to a cache](SubnetGroups.Assigning.md)
+ [Modifying a subnet group](SubnetGroups.Modifying.md)
+ [Deleting a subnet group](SubnetGroups.Deleting.md)

# Creating a subnet group
<a name="SubnetGroups.Creating"></a>

A *cache subnet group* is a collection of subnets that you may want to designate for your caches in a VPC. When launching a cache in a VPC, you need to select a cache subnet group. Then ElastiCache uses that cache subnet group to assign IP addresses within that subnet to each cache node in the cache.

When you create a new subnet group, note the number of available IP addresses. If the subnet has very few free IP addresses, you might be constrained as to how many more nodes you can add to a cluster. To resolve this issue, you can assign one or more subnets to a subnet group so that you have a sufficient number of IP addresses in your cluster's Availability Zone. After that, you can add more nodes to your cluster.

If you choose IPV4 as your network type, a default subnet group will be available or you can choose to create a new one. ElastiCache uses that subnet group to choose a subnet and IP addresses within that subnet to associate with your nodes. If you choose dual-stack or IPV6, you will be directed to create dual-stack or IPV6 subnets. For more information on network types, see [Choosing a network type in ElastiCache](network-type.md). For more information, see [Create a subnet in your VPC](https://docs.aws.amazon.com/vpc/latest/userguide/working-with-vpcs.html#AddaSubnet).

The following procedures show you how to create a subnet group called `mysubnetgroup` (console), the AWS CLI, and the ElastiCache API.

## Creating a subnet group (Console)
<a name="SubnetGroups.Creating.CON"></a>

The following procedure shows how to create a subnet group (console).

**To create a subnet group (Console)**

1. Sign in to the AWS Management Console, and open the ElastiCache console at [https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/).

1. In the navigation list, choose **Subnet groups**.

1. Choose **Create subnet group**.

1. In the **Create subnet group** wizard, do the following. When all the settings are as you want them, choose **Create**.

   1. In the **Name** box, type a name for your subnet group.

   1. In the **Description** box, type a description for your subnet group.

   1. In the **VPC ID** box, choose your Amazon VPC.

   1. All subnets are chosen by default. In the **Selected subnets** panel, click **Manage** and select the Availability Zones or [Local Zones](Local_zones.md) and IDs of your private subnets, and then choose **Choose**.

1. In the confirmation message that appears, choose **Close**.

Your new subnet group appears in the **Subnet Groups** list of the ElastiCache console. At the bottom of the window you can choose the subnet group to see details, such as all of the subnets associated with this group.

## Creating a subnet group (AWS CLI)
<a name="SubnetGroups.Creating.CLI"></a>

At a command prompt, use the command `create-cache-subnet-group` to create a subnet group.

For Linux, macOS, or Unix:

```
aws elasticache create-cache-subnet-group \
    --cache-subnet-group-name mysubnetgroup \
    --cache-subnet-group-description "Testing" \
    --subnet-ids subnet-53df9c3a
```

For Windows:

```
aws elasticache create-cache-subnet-group ^
    --cache-subnet-group-name mysubnetgroup ^
    --cache-subnet-group-description "Testing" ^
    --subnet-ids subnet-53df9c3a
```

This command should produce output similar to the following:

```
{
    "CacheSubnetGroup": {
        "VpcId": "vpc-37c3cd17", 
        "CacheSubnetGroupDescription": "Testing", 
        "Subnets": [
            {
                "SubnetIdentifier": "subnet-53df9c3a", 
                "SubnetAvailabilityZone": {
                    "Name": "us-west-2a"
                }
            }
        ], 
        "CacheSubnetGroupName": "mysubnetgroup"
    }
}
```

For more information, see the AWS CLI topic [create-cache-subnet-group](https://docs.aws.amazon.com/cli/latest/reference/elasticache/create-cache-subnet-group.html).

# Assigning a subnet group to a cache
<a name="SubnetGroups.Assigning"></a>

After you have created a subnet group, you can launch a cache in an Amazon VPC. For more information, see the following.
+ **Memcached cluster** – To launch a Memcached cluster, see [Creating a Memcached cluster (console)](Clusters.Create-mc.md#Clusters.Create.CON.Memcached). In step 7.a (**Advanced Memcached Settings**), choose a VPC subnet group.
+ **Standalone Valkey or Redis OSS cluster** – To launch a single-node Valkey or Redis OSS cluster, see [Creating a Valkey (cluster mode disabled) cluster (Console)](SubnetGroups.designing-cluster-pre.valkey.md#Clusters.Create.CON.valkey-gs). In step 7.a (**Advanced Redis OSS Settings**), choose a VPC subnet group.
+ **Valkey or Redis OSS (cluster mode disabled) replication group** – To launch a Valkey or Redis OSS (cluster mode disabled) replication group in a VPC, see [Creating a Valkey or Redis OSS (Cluster Mode Disabled) replication group from scratch](Replication.CreatingReplGroup.NoExistingCluster.Classic.md). In step 7.b (**Advanced Redis OSS Settings**), choose a VPC subnet group.
+ **Valkey or Redis OSS (cluster mode enabled) replication group** – [Creating a Valkey or Redis OSS (Cluster Mode Enabled) cluster (Console)](Replication.CreatingReplGroup.NoExistingCluster.Cluster.md#Replication.CreatingReplGroup.NoExistingCluster.Cluster.CON). In step 6.i (**Advanced Redis OSS Settings**), choose a VPC subnet group.

# Modifying a subnet group
<a name="SubnetGroups.Modifying"></a>

You can modify a subnet group's description, or modify the list of subnet IDs associated with the subnet group. You cannot delete a subnet ID from a subnet group if a cache is currently using that subnet.

The following procedures show you how to modify a subnet group.

## Modifying subnet groups (Console)
<a name="SubnetGroups.Modifying.CON"></a>

**To modify a subnet group**

1. Sign in to the AWS Management Console and open the ElastiCache console at [ https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/).

1. In the navigation pane, choose **Subnet groups**.

1. In the list of subnet groups, select the radio button of the one you want to modify and choose **Modify**.

1. In the **Selected subnets** panel, choose **Manage**.

1. Make any changes to the selected subnets and click **Choose**.

1. Click **Save changes** to save your changes.

## Modifying subnet groups (AWS CLI)
<a name="SubnetGroups.Modifying.CLI"></a>

At a command prompt, use the command `modify-cache-subnet-group` to modify a subnet group.

For Linux, macOS, or Unix:

```
aws elasticache modify-cache-subnet-group \
    --cache-subnet-group-name mysubnetgroup \
    --cache-subnet-group-description "New description" \
    --subnet-ids "subnet-42df9c3a" "subnet-48fc21a9"
```

For Windows:

```
aws elasticache modify-cache-subnet-group ^
    --cache-subnet-group-name mysubnetgroup ^
    --cache-subnet-group-description "New description" ^
    --subnet-ids "subnet-42df9c3a" "subnet-48fc21a9"
```

This command should produce output similar to the following:

```
{
    "CacheSubnetGroup": {
        "VpcId": "vpc-73cd3c17", 
        "CacheSubnetGroupDescription": "New description", 
        "Subnets": [
            {
                "SubnetIdentifier": "subnet-42dcf93a", 
                "SubnetAvailabilityZone": {
                    "Name": "us-west-2a"
                }
            },
            {
                "SubnetIdentifier": "subnet-48fc12a9", 
                "SubnetAvailabilityZone": {
                    "Name": "us-west-2a"
                }
            }
        ], 
        "CacheSubnetGroupName": "mysubnetgroup"
    }
}
```

For more information, see the AWS CLI topic [modify-cache-subnet-group](https://docs.aws.amazon.com/cli/latest/reference/elasticache/modify-cache-subnet-group.html).

# Deleting a subnet group
<a name="SubnetGroups.Deleting"></a>

If you decide that you no longer need your subnet group, you can delete it. You cannot delete a subnet group if it is currently in use by a cache.

The following procedures show you how to delete a subnet group.

## Deleting a subnet group (Console)
<a name="SubnetGroups.Deleting.CON"></a>

**To delete a subnet group**

1. Sign in to the AWS Management Console and open the ElastiCache console at [ https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/).

1. In the navigation pane, choose **Subnet groups**.

1. In the list of subnet groups, choose the one you want to delete and then choose **Delete**.

1. When you are asked to confirm this operation, type the name of the subnet group in the text input field and choose **Delete**.

## Deleting a subnet group (AWS CLI)
<a name="SubnetGroups.Deleting.CLI"></a>

Using the AWS CLI, call the command **delete-cache-subnet-group** with the following parameter:
+ `--cache-subnet-group-name` *mysubnetgroup*

For Linux, macOS, or Unix:

```
aws elasticache delete-cache-subnet-group \
    --cache-subnet-group-name mysubnetgroup
```

For Windows:

```
aws elasticache delete-cache-subnet-group ^
    --cache-subnet-group-name mysubnetgroup
```

This command produces no output.

For more information, see the AWS CLI topic [delete-cache-subnet-group](https://docs.aws.amazon.com/cli/latest/reference/elasticache/delete-cache-subnet-group.html).