0

I need to connect to a redis instance in my Elasticache. As I understand from Amazon Elasticache Redis cluster - Can't get Endpoint, I can get the endpoint from this. Now suppose I get the endpoint and I use this endpoint to create a JedisClient(Since I use java) then How do I provide the AWS IAM credentials? I am going to secure ElastiCache using IAM policies. How do I ensure no other application connects to this redis?

1 Answers1

0
static AWSCredentials credentials = null;
static {
    try {
        //credentials = new ProfileCredentialsProvider("default").getCredentials();
        credentials = new SystemPropertiesCredentialsProvider().getCredentials();
    } catch (Exception e) {
        System.out.println("Got exception..........");
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/USERNAME/.aws/credentials), and is in valid format.", e);
    }       
}

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    AmazonElastiCache elasticacheClient = AmazonElastiCacheClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
    DescribeCacheClustersRequest dccRequest = new DescribeCacheClustersRequest();
    dccRequest.setShowCacheNodeInfo(true);

    DescribeCacheClustersResult clusterResult = elasticacheClient.describeCacheClusters(dccRequest);

    List<CacheCluster> cacheClusters = clusterResult.getCacheClusters();
    List<String> clusterNodes = new ArrayList <String> ();
    try {
        for (CacheCluster cacheCluster : cacheClusters) {
            for (CacheNode cacheNode : cacheCluster.getCacheNodes()) {
                String addr = cacheNode.getEndpoint().getAddress();
                int port = cacheNode.getEndpoint().getPort();
                String url =  addr + ":" + port;
                if(<ReplicationGroup Name>.equalsIgnoreCase(cacheCluster.getReplicationGroupId()))
                    clusterNodes.add(url);  
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    LettuceConnectionFactory redisConnectionFactory = new LettuceConnectionFactory(new RedisClusterConfiguration(clusterNodes));
    redisConnectionFactory.setUseSsl(true);
    redisConnectionFactory.afterPropertiesSet();
    return redisConnectionFactory;
}
funtoos
  • 295
  • 1
  • 4
  • 17