How can I configure and access ElastiCache for Redis from my Lightsail instance?

4 minute read
0

I want to connect to Amazon ElastiCache for Redis endpoints from my Amazon Lightsail instance.

Resolution

To connect to an ElastiCache for Redis cluster from a Lightsail instance, complete the following steps:

  1. (Optional) Create the Lightsail instance and ElastiCache for Redis resources.
  2. Configure the security group on the cluster to allow the connection.
  3. Turn on VPC peering on your Lightsail instance.
  4. Connect to the Redis cluster endpoint from the Lightsail instance.

(Optional) Create the Lightsail instance and ElastiCache Redis resources

For information on how to create a Lightsail instance and connecting to the instance, see Create an Amazon Lightsail instance. For information on how to create an ElastiCache for Redis cluster, see Create a cluster.
Note: Create the Lightsail and ElastiCache resources in the same Region. You must create the ElastiCache resources on the default VPC.

Configure the security group on the cluster to allow the connection

The security group of the ElastiCache for Redis cluster must allow incoming TCP connections on port 6379 from the Lightsail instance's private IP address.

To add inbound port 6379 to your ElastiCache for Redis cluster, complete the following steps:

  1. Open the Amazon ElastiCache console, and then select the AWS Region where the cluster is located.
  2. In the ElastiCache console dashboard, choose Redis.
  3. Select your cluster, and then note the security group associated with the cluster.
  4. Open the Amazon VPC console and then select the security group that you noted.
  5. Add a new custom TCP rule for port 6379 from the source IP. In this example, the source IP is the private IP of the Lightsail instance.

Turn on VPC peering on the Lightsail instance

For information on VPC peering on the Lightsail instance, see Set up Amazon VPC peering to work with AWS resources outside of Amazon Lightsail.

Note: Turn on VPC peering for the Region that you created your resources in.

Configure the ElastiCache Route Tables for the VPC Peering Connection

To configure the ElastiCache route tables for the VPC peering connection, complete the following steps:

  1. Open the Amazon ElastiCache console, and then select the AWS Region where the cluster is located.

  2. In the ElastiCache console dashboard, choose Redis.

  3. Select your cluster, and then navigate to the Network and security section.

  4. Within the Network and Security section, choose the subnet group. Configure each subnet individually. Click on the subnets to open the Amazon VPC Console.

  5. Update the route tables of the subnets. The route tables of the subnets must have the following configuration:

    From the Destination to the Private IP address of the Lightsail Instance

    From the Target to the Peering connection ('pcx-xxxxx')

For more information, see Update your route tables for a VPC peering connection.

Connect to the Redis cluster endpoint from the Lightsail instance

To connect to the Redis cluster endpoint from the Lightsail instance, complete the following steps:

  1. Download and install redis-cli on your Lightsail instance. To install redis-cli on an Amazon Linux 2 instance, run the following command:
    $ sudo yum install gcc
    $ sudo wget http://download.redis.io/redis-stable.tar.gz
    $ sudo tar xvzf redis-stable.tar.gz
    $ cd redis-stable$ sudo make
    Note: For instructions on installing redis-cli on other Linux distribution, see Download and install redis-cli.
  2. To connect to the cluster, run the following command:
    $ src/redis-cli -h cluster-endpoint -c -p port number
    Note: Replace cluster-endpoint and port number with the endpoint of your cluster and your port number. The default port for Redis is 6379.

The command returns a Redis command prompt similar to the following:

cluster-endpoint: port number

Note: You can run the telnet command to test the connection from your Lightsail instance to the Redis cluster endpoint:

$ telnet <redis_endpoint> 6379
AWS OFFICIAL
AWS OFFICIALUpdated 3 months ago