In this module, you launch an Amazon ElastiCache cluster using a Redis engine and then test connecting to your instance in your application code.

Amazon ElastiCache is a fully managed service for using in-memory data stores in your application. ElastiCache provides two engines you can use -- Redis or Memcached -- and handles all the provisioning, failovers, backups, and more. Users use ElastiCache when they need fast, high-volume data access in their application.

In the following steps, you launch an Amazon ElastiCache cluster using the Redis engine. Then, you configure your cluster so that it is accessible from your Cloud9 development environment. Finally, you test this connection by connecting with your application code.

Time to Complete Module: 20 Minutes

  • Step 1. Launch an Amazon ElastiCache cluster

    First, let’s launch the Amazon ElastiCache for Redis cluster.

    Go to the ElastiCache section in the AWS Management Console and choose Get Started Now to launch the instance creation wizard.

    For Cluster engine, choose Redis.

    In the Redis settings section, for Name, type leaderboard. Then, choose Node type to open the node type selection modal. Choose t2, then choose cache.t2.micro. This instance is the smallest size. You may want a larger instance for a production application, but this instance works well for this example.

    (Click to enlarge)

    For Number of replicas, type 0. ElastiCache replicas provide a fully managed way to scale up the number of reads you can handle by replicating your data to additional instances. They are often a good idea for your production application, but you don’t need them for this tutorial.

    Your basic settings should look as follows.

    (Click to enlarge)

    In the Advanced Settings section, create a new subnet group in your VPC. A subnet is a section of your networking address space. Subnets are a way of breaking you network into smaller pieces so that they can be addressed more efficiently. A subnet group is a collection of subnets, and it is used to choose where to launch your ElastiCache instances.

    For the subnet group, specify a name of leaderboard-redis and select at least one subnet to add to your subnet group. This subnet uses the default VPC that is provided with your AWS account.

    (Click to enlarge)

    Leave the remaining settings as the defaults and choose Create.

    (Click to enlarge)

    Success! You should see your ElastiCache cluster in a creating status on the ElastiCache dashboard. 

  • Step 2. Create a security group for your AWS Lambda function

    While your ElastiCache cluster is launching, you can create a security group for your Lambda function.

    AWS Lambda is a serverless computing service where you upload code that is executed when certain events happen. You don't need to worry about server provisioning or management -- your code execution is handled entirely for you.

    For now, you need to create a security group for your Lambda function so that it can access your ElastiCache instance.

    Navigate to Security Groups in the EC2 section of the AWS Management Console and choose Create security group.

    (Click to enlarge)

    For Security group name, type leaderboard-lambda. For Description, type Security group for Leaderboard Tutorial. The security group should be in your default VPC, which is the same VPC where you launched your ElastiCache instance.

    Choose Create to create your security group.

    (Click to enlarge)

    In the Security Groups list, find your leaderboard-lambda group and copy the security group ID (this ID begins with sg-).

    Then, run the following command in your Cloud9 terminal.

    (Make sure to substitute your security group ID for <securityGroupId> when running the command.)

    echo "export SECURITY_GROUP_ID=<securityGroupId>" >> && source
  • Step 3. Configure access to your ElastiCache cluster

    Now that you have created an ElastiCache instance and your Lambda security group, you need to configure ElastiCache so that your Cloud9 development environment and Lambda function can access it.

    Amazon ElastiCache instances must be provisioned in a VPC in a private subnet. They will not receive a public IP address and cannot be accessed from the public internet. To access your ElastiCache instance, you need to do it from some compute resource -- like an EC2 instance or Lambda function -- that has access inside your VPC.

    Navigate to the ElastiCache dashboard in the AWS Management Console to see your ElastiCache instance. Expand the instance details and find the security group of your ElastiCache instance.

    (Click to enlarge)

    Next, go to Security Groups in the EC2 section of the AWS Management Console. Paste the value of your ElastiCache Security Group into the filter to find that security group.

    (Click to enlarge)

    Select that security group to open its configuration. Choose the Inbound tab, then choose Edit.

    You need to create two rules for your security group: one that gives your Cloud9 instance access to your Redis instance, and one that gives your Lambda function access to your Redis instance.

    (Click to enlarge)

    First, add a rule with a Port Range of 6379, which is the port that Redis uses by default. In the Source section, type cloud9. You should see an option that starts with aws-cloud9-Puzzle-Leaderboard. Select that option to allow traffic from your Cloud9 instance. Finally, add a description so you can remember why this rule exists.

    Second, add a similar rule with a Port Range of 6379 for your Lambda function. In the Source section, search for your group by typing leaderboard-lambda. Select the security group.

    Once you have added both rules, choose Save.

    (Click to enlarge)

    Your Cloud9 instance should now have access to your ElastiCache instance. In the next step, you test that access in your application code. 

  • Step 4. Test access to your ElastiCache cluster

    In the final step of this module, you test that you can connect to your ElastiCache instance.

    Navigate to the ElastiCache dashboard in the AWS Management Console to see your ElastiCache instance. Expand the instance details and find the primary endpoint property.

    Copy this value to your clipboard.

    (Click to enlarge)

    Export this value as an environment variable in your Cloud9 instance by running the following command in your terminal.

    Note: Make sure to replace <yourRedisEndpoint> with the value of your Redis instance’s primary endpoint.

    echo "export REDIS_ENDPOINT=<yourRedisEndpoint>" >> && source

    With this configured, you can now try to connect to your endpoint. There is a testRedis.js file in the scripts/ directory. The contents of that file are as follows:

    const redis = require('redis')
    const client = redis.createClient({
      url: `redis://${process.env.REDIS_ENDPOINT}`
    client.on('error', function(err) {
      console.log('Received Redis error:', err)
    }), resp) => {
      if (err) {
        console.log('Error doing ping: ', err);
      } else {
        console.log('Successful ping!', resp)

    This code initializes a connection to your Redis instance. Then, it runs a simple PING command to ensure a valid connection.

    Execute the script by running the following command:

    node scripts/testRedis.js

    You should see the following output in your terminal:

    Successful ping! PONG

    If you received the Redis server’s PONG response, it indicates that you are able to connect to your ElastiCache Redis instance.

In this module, you launched an ElastiCache for Redis cluster. Then, you configured the security group on your Redis instance so that you could access it from your Cloud9 development environment. Finally, you tested your configuration by connecting to your Redis instance.

In the next module, you design the data model you use in your Redis instance to handle your global leaderboard.