Trying to connect to AWS Elasticache (valkey engine: serverless: version 8.0) from my Java springboot application. JDK 17 & springboot version 3.2.5.
Using Lettuce client to connect to Valkey cache (https://docs.aws.amazon.com/pdfs/AmazonElastiCache/latest/dg/redis-ug.pdf#BestPractices.Clients-lettuce)
Below is the code snippet I am using, which fails at line "client.connect()"
public void readLettuceCacheAuth() {
try {
AWSCredentialsProvider awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
IAMAuthTokenRequest iamAuthTokenRequest = new IAMAuthTokenRequest("iam-user-valkey-01", "my-cache", "us-east-1", true);
String iamAuthToken = iamAuthTokenRequest.toSignedRequestUri(awsCredentialsProvider.getCredentials());
log.info("iamAuthToken: " + iamAuthToken);
RedisURI redisURI = RedisURI.builder()
.withHost(VALKEY_CACHE_HOST)
.withPort(VALKEY_CACHE_PORT)
.withSsl(true)
.withAuthentication("iam-user-valkey-01", iamAuthToken)
.build();
RedisClient client = RedisClient.create(redisURI);
log.info("client => " + client);
StatefulRedisConnection<String, String> connection = client.connect(); <== exception at this line
log.info("connection => " + connection);
RedisStringCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "value");
String value = syncCommands.get("key");
} catch (Exception e) {
log.error("Lettuce error: " + e);
}
}
Below are the logs,
iamAuthToken: my-cache/?Action=connect&User=iam-user-valkey-01&ResourceType=ServerlessCache&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEOv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDmFwLXNvdXRoZWFzdC0yIkcwRQIhAO9SJdccIXP2Y2VDj0WxEqdCLQDLMUTbH8IRnbAS11QmAiAOjS
client => io.lettuce.core.RedisClient@504021e
Lettuce error: io.lettuce.core.RedisConnectionException: Unable to connect to my-cache-fpheum.serverless.apse2.cache.amazonaws.com/<unresolved>:6379
Both Cache, Springboot application are in same VPC.
Updated Cache's security group inbound rules to allow java application on port 6379.
Can someone please help what is going wrong here?
-
I mean the error you posted says <unresolved>. So check the hostname is correct?Winson Tanputraman– Winson Tanputraman2025年01月23日 06:43:28 +00:00Commented Jan 23, 2025 at 6:43
-
I copied the host name (ElastiCache Endpoint) from AWS, which is => "my-cache-fpheum.serverless.apse2.cache.amazonaws.com". I don't know why it says 'Unresolved'.alex– alex2025年01月23日 21:50:40 +00:00Commented Jan 23, 2025 at 21:50
-
yup can you double triple check this? because i tried nslookup your domain on my end and i also got unresolved (and just to clarify i know your redis is probably not open to internet, but usually the elasticache domain is resolvable by anyone on internet--even my customer production private elasticache cluster has DNS that is resolveable by anyone).Winson Tanputraman– Winson Tanputraman2025年01月24日 01:32:47 +00:00Commented Jan 24, 2025 at 1:32
-
Does the user name is the same as the user id?avifen– avifen2025年01月28日 23:54:58 +00:00Commented Jan 28, 2025 at 23:54
-
@avifen yes same.alex– alex2025年01月29日 09:11:45 +00:00Commented Jan 29, 2025 at 9:11
1 Answer 1
I got it working after correcting below:
- Adding below policies to the ecs task role
AllowElasticacheConnect: new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
actions: ['elasticache:Connect'],
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:elasticache:ap-southeast-2:${this.account}:serverlesscache:my-cache`,
`arn:aws:elasticache:ap-southeast-2:${this.account}:user:iam-user-valkey-01`,
]
}),
],
})
- Update security group of valkey cache and add inbound rules to allow java app
Comments
Explore related questions
See similar questions with these tags.