Skip to content

Redis

Reference doc for the `sst.aws.Redis` component.

The Redis component lets you add a Redis cluster to your app using Amazon ElastiCache.

Create the cluster

sst.config.ts
const vpc = new sst.aws.Vpc("MyVpc");
const redis = new sst.aws.Redis("MyRedis", { vpc });

You can link your cluster to other resources, like a function or your Next.js app.

sst.config.ts
new sst.aws.Nextjs("MyWeb", {
link: [redis],
vpc
});

Once linked, you can connect to it from your function code.

app/page.tsx
import { Resource } from "sst";
import { Cluster } from "ioredis";
const client = new Cluster(
[{
host: Resource.MyRedis.host,
port: Resource.MyRedis.port
}],
{
redisOptions: {
tls: { checkServerIdentity: () => undefined },
username: Resource.MyRedis.username,
password: Resource.MyRedis.password
}
}
);

Cost

By default this component uses On-demand nodes with a single cache.t4g.micro instance; $0.016 per hour.

That works out to $0.016 x 24 x 30 or $12 per month.

Adjust this for the instance type and number of nodes you are using.

The above are rough estimates for us-east-1, check out the ElastiCache pricing for more details.


Constructor

new Redis(name, args, opts?)

Parameters

RedisArgs

instance?

Type Input<string>

Default “t4g.micro”

The type of instance to use for the nodes of the Redis cluster. Check out the supported instance types.

{
instance: "m7g.xlarge"
}

nodes?

Type Input<number>

Default 1

The number of nodes to use for the Redis cluster.

{
nodes: 4
}

transform?

Type Object

Transform how this component creates its underlying resources.

transform.cluster?

Type ReplicationGroupArgs | (args: ReplicationGroupArgs, opts: ComponentResourceOptions, name: string) => void

Transform the Redis cluster.

transform.subnetGroup?

Type SubnetGroupArgs | (args: SubnetGroupArgs, opts: ComponentResourceOptions, name: string) => void

Transform the Redis subnet group.

version?

Type Input<string>

Default “7.1”

The Redis engine version. Check out the supported versions.

{
version: "6.2"
}

vpc

Type Input<Vpc | Object>

The VPC to use for the Redis cluster.

Create a VPC component.

const myVpc = new sst.aws.Vpc("MyVpc");

And pass it in.

{
vpc: myVpc
}

Or pass in a custom VPC configuration.

{
vpc: {
subnets: ["subnet-0db7376a7ad4db5fd ", "subnet-06fc7ee8319b2c0ce"],
securityGroups: ["sg-0399348378a4c256c"]
}
}

vpc.securityGroups

Type Input<Input<string>[]>

A list of VPC security group IDs.

vpc.subnets

Type Input<Input<string>[]>

A list of subnet IDs in the VPC to deploy the Redis cluster in.

Properties

clusterID

Type Output<string>

The ID of the Redis cluster.

host

Type Output<string>

The host to connect to the Redis cluster.

nodes

Type Object

nodes.cluster

password

Type Output<string>

The password to connect to the Redis cluster.

port

Type Output<number>

The port to connect to the Redis cluster.

username

Type Output<string>

The username to connect to the Redis cluster.

SDK

Use the SDK in your runtime to interact with your infrastructure.


This is accessible through the Resource object in the SDK.

  • host string

    The host to connect to the Redis cluster.

  • password string

    The password to connect to the Redis cluster.

  • port number

    The port to connect to the Redis cluster.

  • username string

    The username to connect to the Redis cluster.

Methods

static get

Redis.get(name, clusterID, opts?)

Parameters

  • name string

    The name of the component.
  • clusterID Input<string>

    The id of the existing Redis cluster.
  • opts? ComponentResourceOptions

Returns Redis

Reference an existing Redis cluster with the given cluster name. This is useful when you create a Redis cluster in one stage and want to share it in another. It avoids having to create a new Redis cluster in the other stage.

Imagine you create a cluster in the dev stage. And in your personal stage frank, instead of creating a new cluster, you want to share the same cluster from dev.

sst.config.ts
const redis = $app.stage === "frank"
? sst.aws.Redis.get("MyRedis", "app-dev-myredis")
: new sst.aws.Redis("MyRedis");

Here app-dev-myredis is the ID of the cluster created in the dev stage. You can find this by outputting the cluster ID in the dev stage.

sst.config.ts
return {
cluster: redis.clusterID
};