Skip to content

Instantly share code, notes, and snippets.

@vinodjayachandran
Created March 2, 2023 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinodjayachandran/e4dc6d1695cbf9dc561d141ac4bf6395 to your computer and use it in GitHub Desktop.
Save vinodjayachandran/e4dc6d1695cbf9dc561d141ac4bf6395 to your computer and use it in GitHub Desktop.
Connect to ElastiCache (Redis) with Encryption in Transit and Auth token enabled
package com.elasticache;
import lombok.extern.log4j.Log4j2;
import redis.clients.jedis.Jedis;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
import java.net.URI;
/**
* @Todo - Close Jedis Connection on Lambda Exit after completing all writes
* Reference : https://github.com/aws-samples/graceful-shutdown-with-aws-lambda/tree/main/java-demo
*/
@Log4j2
public class SampleJedisConnection {
// The Jedis object. Initialize it from connectToElastiCache() method.
private Jedis jedis = null;
/**
* Initializes Jedis Object on successful connection with DB
*/
public void connectToElastiCache() {
final URI uri = URI.create(System.getenv("REDIS_CLUSTER_URI"));
final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
// These SSL parameters ensure that we use the same hostname verifier used
// for HTTPS.
final SSLParameters sslParameters = new SSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
jedis = new Jedis(uri,sslSocketFactory,sslParameters,null);
// Pass the AUTH Token (Password)
jedis.auth(System.getenv("REDIS_AUTH_TOKEN"));
log.info("Connection to ElastiCache Successful");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment