An Interest In:
Web News this Week
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
- March 24, 2024
- March 23, 2024
- March 22, 2024
Getting started with Redis on AWS - the easy way!
This quick-start uses AWS Cloud9 IDE to help you get up and running with MemoryDB for Redis, quickly
Like most of the services, Amazon MemoryDB for Redis is fully integrated with Amazon VPC and always launches your cluster in a VPC. It means that you cannot access it from outside your VPC. While initially exploring these services (includes MSK, Elasticache for Redis etc.), I usually followed the documentation that involved setting up EC2, SSH-ing into the instance, install/copy stuff (language, runtime, code, client etc.) and then try things out.
Most often, the first step is the hardest, and it's important for developers to have the least amount of friction as possible to "get going". I looked for simpler ways and found AWS Cloud9 to be super useful. It was quick, predictable and had a bunch of useful tooling readily available.
In this blog, I will provide step-by-step instructions on how you can easily and quickly get started (and continue experimenting/building/developing) with Amazon MemoryDB using Cloud9
.
You don't need to install anything your local machine to work through this tutorial.
The only thing you need is an AWS account (of course!) - so make sure you have one (even a free tier might work)
Setup Cloud9 and MemoryDB
If you're new to AWS in general (or MemoryDB/any other service), I would advise you to go through the setup manually using the AWS Console (as opposed to using CloudFormation or other tooling). This gives you an overview of the options available and will be helpful when you try to automate the same using AWS CLI, CDK, CloudFormation etc.
Cloud9 environment
This is quite simple - the documentation worked as expected.
Go to the AWS console > Cloud9:
Just enter the name your environment:
You can safely choose the defaults on the second screen:
- You will have a
t2.micro
node type (1 GiB RAM + 1 vCPU) with Amazon Linux 2 which will be auto-hibernated after 30 mins (if not used) - The instance will be placed in the default VPC (any subnet in any AZ). - A security group will also be created
This is good enough for now.
On the last page, review your settings, click Create environment and you should be off to the races!
MemoryDB for Redis
Again, the documentation works as expected. there are a few config knobs, but i will advice you to keep it simple:
- Single node cluster - select
db.t4g.small
node type (it's sufficient for now) - Place the default vpc - you will be choosing this (along with the subnets) while creating a subnet group (in MemoryDB)
- Make sure to setup the ACL and credentials (username and password to connect to MemoryDB) as well
Be patient, the cluster should be ready in a few mins :)
Security configuration
You need to add configuration to allow access from Cloud9
instance to your MemoryDB cluster.
First, copy the the security group ID for your Cloud9
instance:
Then, open the security group for your MemoryDB cluster:
Add an Inbound security rule:
The rule says: Allow instance associated with the source security group (
Cloud9
in this case) to access TCP port6379
of instance associated with target security group (MemoryDB in this case)
You're all set!
Navigate to your Cloud9 IDE:
Go to AWS console > Cloud9:
Your Cloud9
env should open up - you should see a terminal.
Connect to MemoryDB - the easy way
The simplest way is to use redis-cli.
You don't need to install it separately - let's just use Docker since it's already pre-installed for us!
redis-cli
is available in the redis
container itself, so you can start it up and use it from there. Pull the Redis Docker image from DockerHub - docker pull redis
Admin:~/environment $ docker pull redisUsing default tag: latestlatest: Pulling from library/redis214ca5fb9032: Pull complete 9eeabf2ad250: Pull complete b8eb79a9f3c4: Pull complete 0ba9bf1b547e: Pull complete 2d2e2b28e876: Pull complete 3e45fcdfb831: Pull complete Digest: sha256:180582894be9a7d5f1201877744b912945a8f9a793a65cd66dc1af5ec3fff0fcStatus: Downloaded newer image for redis:latestdocker.io/library/redis:latest
Run the container:
Admin:~/environment $ docker run --rm -it redis /bin/bashroot@429f8fabaf09:/data#
Now you are inside a terminal (in the container) of a terminal (Cloud9 IDE) ;)
Copy the cluster endpoint of your MemoryDB cluster and set it as an environment variable
Make sure you remove the port (
:6379
) from the cluster endpoint sinceredis-cli
appends that automatically:
export MEMORYDB_CLUSTER_ENDPOINT=<memorydb cluster endpoint without the :6379 part)redis-cli -c --user <memorydb username> --askpass -h $MEMORYDB_CLUSTER_ENDPOINT --tls --insecure
--askpass
will prompt you for the password - enter it.
Wohoo! You are now connected to your MemoryDB cluster from inside a Docker container in your Cloud9 instance.
Time for the customary hello world
dance!
In the terminal:
SET hello worldSET foo bar
You should get an
OK
response from MemoryDB
So far so good! You were able to use standard tooling (redis-cli
) to connect with your freshly minted MemoryDB cluster. This is good for sanity/connectivity testing, but you can also so some "lightweight" development and run some programs to execute operations on MemoryDB - this is the next logical step.
So let's do that. The example below shows a Go program, but you could use a language of your choice. After all, most language runtimes (like Java, Python, Node.js, Go etc.) come pre-installed in Cloud9 environment! Check this out https://docs.aws.amazon.com/cloud9/latest/user-guide/language-support.html
Run a program to connect with MemoryDB
The code is on GitHub, so simply clone it and change to the right folder:
git clone https://github.com/abhirockzz/memorydb-cloud9-quickstartcd memorydb-cloud9-quickstart
Set the environment variables and run the program!
export MEMORYDB_CLUSTER_ENDPOINT=<memorydb cluster endpoint (with the port)>export MEMORYDB_USERNAME=<memorydb username>export MEMORYDB_PASSWORD=<memorydb password>go run main.go
Here is the output when I ran it:
Admin:~/environment/memorydb-cloud9-quickstart (master) $ go run main.go go: downloading github.com/go-redis/redis/v8 v8.11.5go: downloading github.com/gorilla/mux v1.8.0go: downloading github.com/cespare/xxhash/v2 v2.1.2go: downloading github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f2022/05/12 04:53:46 connecting to cluster ****************(redacted)2022/05/12 04:53:46 successfully connected to cluster2022/05/12 04:53:46 started HTTP server....
This will start a HTTP server that exposes a few endpoints. Let's try them out.
Open a separate terminal in Cloud9 to run the commands below
First, take a look at the cluster info:
curl -i http://localhost:8080/HTTP/1.1 200 OKDate: Thu, 12 May 2022 04:57:03 GMTContent-Length: 354Content-Type: text/plain; charset=utf-8[{"Start":0,"End":16383,"Nodes":[{"ID":"3a0ef99406d4165fab450fde6c0a4eac3ee8f215","Addr":"****************.amazonaws.com:6379"},{"ID":"2b5a4663a9183f7921517c6f14195e9d26a6ca79","Addr":"****************.amazonaws.com:6379"}]}]
We got back info about the Shards in our cluster along with individual nodes.
The result will be different in your case
Remember we had executed SET hello world
with redis-cli
before? Let's GET
that value now:
# get the value for the key "hello"Admin:~/environment $ curl -i localhost:8080/helloHTTP/1.1 200 OKDate: Thu, 12 May 2022 04:54:45 GMTContent-Length: 32Content-Type: text/plain; charset=utf-8{"Key":"hello","Value":"world"}
Do the same for the key foo
:
Admin:~/environment $ curl -i localhost:8080/fooHTTP/1.1 200 OKDate: Thu, 12 May 2022 04:55:44 GMTContent-Length: 28Content-Type: text/plain; charset=utf-8{"Key":"foo","Value":"bar"}
Works as expected - what about a key that does not exist?
Admin:~/environment $ curl -i localhost:8080/notthereHTTP/1.1 404 Not FoundDate: Thu, 12 May 2022 04:56:23 GMTContent-Length: 0
HTTP 404
- fair enough. Finally, you can set your own key-value:
Admin:~/environment $ curl -i -X POST -d 'redis' localhost:8080/awsomeHTTP/1.1 200 OKDate: Thu, 12 May 2022 04:59:25 GMTContent-Length: 0Admin:~/environment $ curl -i localhost:8080/awsomeHTTP/1.1 200 OKDate: Thu, 12 May 2022 05:00:51 GMTContent-Length: 33Content-Type: text/plain; charset=utf-8{"Key":"awsome","Value":"redis"}
Alright, everything works!
The
Dockerfile
for the sample app is also present in the Github repo in case you want to build a docker image and run that instead.
Clean up
Once you're done, don't forget to:
- Delete the MemoryDB cluster, and
- the Cloud9 environment
That's all for this blog. I hope you were able to follow along - setup a MemoryDB cluster and a Cloud9 environment (along with a ready to use IDE and terminal!). After doing some initial connectivity testing using redis-cli
in docker, you also ran a test program to experiment a little more.
All this with just your browser!
I hope this was useful and you can re-use this for your particular setup, requirements and programming language.
Happy coding!
Original Link: https://dev.to/aws/getting-started-with-redis-on-aws-the-easy-way-jol
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To