Hi Team,
I'm currently trying to test my Lambda function locally using the SAM CLI with the command sam local invoke -e events/requests/event.json
or through the VSCode debugger by specifying the paths for template.yaml
and the payload event file in launch.json
.
My Lambda code needs to connect to an AWS Redis cluster. Locally, I created a Redis server via Docker, testing multiple image types (e.g., redis:7.4.0-alpine
, redis-stack
, redis:latest
).
this my docker-compose file
version: "3.8"
services:
redis:
image: "redis:latest" #image: redis:7.4.0-alpine # Lightweight Alpine version of Redis
command: ["redis-server", "--bind", "redis", "--port", "6379"]
container_name: redis-server
restart: always
ports:
- "6379:6379"
expose:
- "6379"
volumes:
- redis-data:/data
- ./redis.conf:/tmp/redis.conf
#command: ["redis-server", "--appendonly", "yes"] # Enable persistence to mimic ElastiCache behavior
networks:
- myNet
volumes:
redis-data:
# driver: local # Use the local driver for volume (default)
networks:
myNet:
driver: bridge
name: sam-network
also tried tired turn redis container directly with docker run cmd : docker run --name local-redis -p 6379:6379 -d redis
However, each time I encounter the following error:
48d7985d-ccd8-4dee-b01c-bf6da0d35679 ERROR ERROR while attempting to connect to Redis cluster! exec the cmd: 'process.exit(1)' Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:128:17) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
I also tried installing Redis directly on my machine without using Docker, but I still encounter the same issue. I modified the redis.conf
file, changing bind 127.0.0.1 ::1
to bind 0.0.0.0
, but the error persists.
this is my lambda code that tries to connect to Redis (typescript):
import { createClient, RedisClientType } from "redis";
export function createClient(
REDIS_HOST: string,
REDIS_PORT: string
) {
//try-1
const redisClient2 = redis.createClient(6379, "localhost"); //const redis = require("redis");
//try-2
const redisClient2 = redis.createClient(6379, "127.0.0.1");//const redis = require("redis");
//try-3
const redisClient2 = createClient(); //import { createClient, RedisClientType } from "redis";
redisClient2.connect();
console.log(redisClient2.isOpen);
//try-4
const redisClient2 = createClient({
url: `redis://localhost:${REDIS_PORT}`, //=> redis:// => one 's'
});
//try-5
const redisClient2 = createClient({
url: `rediss://${REDIS_HOST}:${REDIS_PORT}`, //==> rediss:// => tow 's'
});
//const redisClient2 = createClient();
//try-6
const redisClient2 = createClient({
socket: {
host: "localhost",
port: 6379,
tls: false,
},
password: "1234",
//username: "username",
});
redisClient2.on("error", (err: any) => {
console.error(
"ERROR while attempting to connect to Redis cluster! ",
err
);
process.exit(1);
});
return redisClient2;
}
all of those tris gives the same error above
ps: when I connect a redis GUI installed locally to the redis server docker container or the redis server installed locally (and unchecked the tls checkbox) I can connect the client GUI successfully. only with code I had the above error
Could you please assist me in resolving this? How can I properly connect to my local Redis server running inside a Docker container with SAM local?
I followed the solutions proposed on these links, but none of the suggested solutions worked.
https://stackoverflow.com/questions/50818146/docker-cant-connect-to-redis-from-another-service/58891367#58891367
https://forums.docker.com/t/connecting-redis-from-my-network-in-docker-net-core-application/92405/4
https://github.com/redis/ioredis/issues/763
https://github.com/redis/node-redis/issues/2416
https://github.com/aws/aws-sam-cli/issues/4042
thank you!!