Getting started with Redis on AWS – the easy way!

This quick-start makes use of AWS Cloud9 IDE that will help you rise up and working with MemoryDB for Redis, rapidly

Like a lot of the companies, Amazon MemoryDB for Redis is totally built-in with Amazon VPC and at all times launches your cluster in a VPC. It signifies that you can not entry it from outdoors your VPC. Whereas initially exploring these companies (consists of MSK, Elasticache for Redis and many others.), I often adopted the documentation that concerned establishing EC2, SSH-ing into the occasion, set up/copy stuff (language, runtime, code, consumer and many others.) after which strive issues out.

Most frequently, step one is the toughest, and it is necessary for builders to have the least quantity of friction as attainable to “get going”. I regarded for easier methods and located AWS Cloud9 to be tremendous helpful. It was fast, predictable and had a bunch of helpful tooling available.

On this weblog, I’ll present step-by-step directions on how one can simply and rapidly get began (and proceed experimenting/constructing/creating) with Amazon MemoryDB utilizing Cloud9.

You needn’t set up something your native machine to work by this tutorial.

The one factor you want is an AWS account (after all!) – so ensure you have one (even a free tier might work)

Setup Cloud9 and MemoryDB

For those who’re new to AWS normally (or MemoryDB/another service), I’d advise you to undergo the setup manually utilizing the AWS Console (versus utilizing CloudFormation or different tooling). This offers you an outline of the choices out there and will likely be useful once you attempt to automate the identical utilizing AWS CLI, CDK, CloudFormation and many others.

Cloud9 atmosphere

That is fairly easy – the documentation labored as anticipated.

Go to the AWS console > Cloud9:

Simply enter the title your atmosphere:

Image description

You possibly can safely select the defaults on the second display:

  • You’ll have a t2.micro node kind (1 GiB RAM + 1 vCPU) with Amazon Linux 2 which will likely be auto-hibernated after 30 minutes (if not used)
  • The occasion will likely be positioned within the default VPC (any subnet in any AZ). – A safety group will even be created

That is ok for now.

On the final web page, evaluation your settings, click on Create atmosphere and try to be off to the races!

Image description

MemoryDB for Redis

Once more, the documentation works as anticipated. there are a number of config knobs, however i’ll recommendation you to maintain it easy:

  • Single node cluster – choose db.t4g.small node kind (it is ample for now)
  • Place the default vpc – you can be selecting this (together with the subnets) whereas making a subnet group (in MemoryDB)
  • Be certain to setup the ACL and credentials (username and password to connect with MemoryDB) as nicely

Be affected person, the cluster must be prepared in a number of minutes 🙂

Image description

Safety configuration

It is advisable to add configuration to permit entry from Cloud9 occasion to your MemoryDB cluster.

First, copy the the safety group ID to your Cloud9 occasion:

Image description

Then, open the safety group to your MemoryDB cluster:

Image description

Add an Inbound safety rule:

Image description

The rule says: Permit occasion related to the supply safety group (Cloud9 on this case) to entry TCP port 6379 of occasion related to goal safety group (MemoryDB on this case)

You are all set!

Navigate to your Cloud9 IDE:

Go to AWS console > Cloud9:

Image description

Your Cloud9 env ought to open up – it is best to see a terminal.

Hook up with MemoryDB – the straightforward manner

The best manner is to make use of redis-cli.
You needn’t set up it individually – let’s simply use Docker because it’s already pre-installed for us!

redis-cli is out there within the redis container itself, so you can begin it up and use it from there. Pull the Redis Docker picture from DockerHubdocker pull redis

Admin:~/atmosphere $ docker pull redis

Utilizing default tag: newest
newest: Pulling from library/redis
214ca5fb9032: Pull full 
9eeabf2ad250: Pull full 
b8eb79a9f3c4: Pull full 
0ba9bf1b547e: Pull full 
2d2e2b28e876: Pull full 
3e45fcdfb831: Pull full 
Digest: sha256:180582894be9a7d5f1201877744b912945a8f9a793a65cd66dc1af5ec3fff0fc
Standing: Downloaded newer picture for redis:newest
Enter fullscreen mode

Exit fullscreen mode

Run the container:

Admin:~/atmosphere $ docker run --rm -it redis /bin/bash
Enter fullscreen mode

Exit fullscreen mode

Now you might be inside a terminal (within the container) of a terminal (Cloud9 IDE) 😉

Copy the cluster endpoint of your MemoryDB cluster and set it as an atmosphere variable

Image description

Be sure you take away the port (:6379) from the cluster endpoint since redis-cli appends that mechanically:

export MEMORYDB_CLUSTER_ENDPOINT=<memorydb cluster endpoint with out the :6379 half)

redis-cli -c --user <memorydb username> --askpass -h $MEMORYDB_CLUSTER_ENDPOINT --tls --insecure
Enter fullscreen mode

Exit fullscreen mode

--askpass will immediate you for the password – enter it.

Image description

Wohoo! You are actually related to your MemoryDB cluster from inside a Docker container in your Cloud9 occasion.

Time for the customary hey world dance!

Within the terminal:

SET hey world
SET foo bar
Enter fullscreen mode

Exit fullscreen mode

It’s best to get an OK response from MemoryDB

Thus far so good! You have been in a position to make use of customary tooling (redis-cli) to attach together with your freshly minted MemoryDB cluster. That is good for sanity/connectivity testing, however you too can so some “light-weight” improvement and run some packages to execute operations on MemoryDB – that is the following logical step.

So let’s do this. The instance beneath reveals a Go program, however you would use a language of your selection. In spite of everything, most language runtimes (like Java, Python, Node.js, Go and many others.) come pre-installed in Cloud9 atmosphere! Test this out

Image description

Run a program to attach with MemoryDB

The code is on GitHub, so merely clone it and alter to the correct folder:

git clone
cd memorydb-cloud9-quickstart
Enter fullscreen mode

Exit fullscreen mode

Set the atmosphere variables and run this system!

export MEMORYDB_CLUSTER_ENDPOINT=<memorydb cluster endpoint (with the port)>
export MEMORYDB_USERNAME=<memorydb username>
export MEMORYDB_PASSWORD=<memorydb password>

go run fundamental.go
Enter fullscreen mode

Exit fullscreen mode

Right here is the output once I ran it:

Admin:~/atmosphere/memorydb-cloud9-quickstart (grasp) $ go run fundamental.go 

go: downloading v8.11.5
go: downloading v1.8.0
go: downloading v2.1.2
go: downloading v0.0.0-20200823014737-9f7001d12a5f
2022/05/12 04:53:46 connecting to cluster ****************(redacted)
2022/05/12 04:53:46 efficiently related to cluster
2022/05/12 04:53:46 began HTTP server....
Enter fullscreen mode

Exit fullscreen mode

It will begin a HTTP server that exposes a number of endpoints. Let’s strive them out.

Open a separate terminal in Cloud9 to run the instructions beneath

First, check out the cluster data:

curl -i http://localhost:8080/

HTTP/1.1 200 OK
Date: Thu, 12 Could 2022 04:57:03 GMT
Content material-Size: 354
Content material-Kind: textual content/plain; charset=utf-8

Enter fullscreen mode

Exit fullscreen mode

We acquired again data in regards to the Shards in our cluster together with particular person nodes.

The end result will likely be completely different in your case

Keep in mind we had executed SET hey world with redis-cli earlier than? Let’s GET that worth now:

# get the worth for the important thing "hey"

Admin:~/atmosphere $ curl -i localhost:8080/hey
HTTP/1.1 200 OK
Date: Thu, 12 Could 2022 04:54:45 GMT
Content material-Size: 32
Content material-Kind: textual content/plain; charset=utf-8

Enter fullscreen mode

Exit fullscreen mode

Do the identical for the important thing foo:

Admin:~/atmosphere $ curl -i localhost:8080/foo
HTTP/1.1 200 OK
Date: Thu, 12 Could 2022 04:55:44 GMT
Content material-Size: 28
Content material-Kind: textual content/plain; charset=utf-8

Enter fullscreen mode

Exit fullscreen mode

Works as anticipated – what a couple of key that doesn’t exist?

Admin:~/atmosphere $ curl -i localhost:8080/notthere
HTTP/1.1 404 Not Discovered
Date: Thu, 12 Could 2022 04:56:23 GMT
Content material-Size: 0
Enter fullscreen mode

Exit fullscreen mode

HTTP 404 – honest sufficient. Lastly, you possibly can set your personal key-value:

Admin:~/atmosphere $ curl -i -X POST -d 'redis' localhost:8080/awsome
HTTP/1.1 200 OK
Date: Thu, 12 Could 2022 04:59:25 GMT
Content material-Size: 0

Admin:~/atmosphere $ curl -i localhost:8080/awsome
HTTP/1.1 200 OK
Date: Thu, 12 Could 2022 05:00:51 GMT
Content material-Size: 33
Content material-Kind: textual content/plain; charset=utf-8

Enter fullscreen mode

Exit fullscreen mode

Alright, all the pieces works!

The Dockerfile for the pattern app can be current within the Github repo in case you wish to construct a docker picture and run that as a substitute.

Clear up

When you’re achieved, do not forget to:

  • Delete the MemoryDB cluster, and
  • the Cloud9 atmosphere

That is all for this weblog. I hope you have been in a position to observe alongside – setup a MemoryDB cluster and a Cloud9 atmosphere (together with a prepared to make use of IDE and terminal!). After performing some preliminary connectivity testing utilizing redis-cli in docker, you additionally ran a check program to experiment slightly extra.

All this with simply your browser!

I hope this was helpful and you’ll re-use this to your explicit setup, necessities and programming language.
Blissful coding!

Add a Comment

Your email address will not be published. Required fields are marked *