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.

    leaderboard-ec-node-type

    (Click to enlarge)

    leaderboard-ec-node-type

    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.

    leaderboard-ec-settings

    (Click to enlarge)

    leaderboard-ec-settings

    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.

    leaderboard-ec-advanced-settings

    (Click to enlarge)

    leaderboard-ec-advanced-settings

    Leave the remaining settings as the defaults and choose Create.

    leaderboard-ec-maintenance

    (Click to enlarge)

    leaderboard-ec-maintenance

    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.

    leaderboard-sg

    (Click to enlarge)

    leaderboard-sg

    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.

    leaderboard-sg-settings

    (Click to enlarge)

    leaderboard-sg-settings

    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>" >> env.sh && source env.sh
  • 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.

    leaderboard-ec-details

    (Click to enlarge)

    leaderboard-ec-details

    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.

    leaderboard-ec-sg-search

    (Click to enlarge)

    leaderboard-ec-sg-search

    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.

    leaderboard-ec-sg-inbound

    (Click to enlarge)

    leaderboard-ec-sg-inbound

    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.

    leaderboard-ec-inbound-rules

    (Click to enlarge)

    leaderboard-ec-inbound-rules

    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.

    leaderboard-ec-endpoint

    (Click to enlarge)

    leaderboard-ec-endpoint

    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>" >> env.sh && source env.sh

    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)
    })
    
    client.ping((err, resp) => {
      if (err) {
        console.log('Error doing ping: ', err);
      } else {
        console.log('Successful ping!', resp)
      }
      client.quit()
    

    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.