Created
March 2, 2023 05:26
-
-
Save vinodjayachandran/e4dc6d1695cbf9dc561d141ac4bf6395 to your computer and use it in GitHub Desktop.
Connect to ElastiCache (Redis) with Encryption in Transit and Auth token enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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