AWS Lambda timeout

java

#1

I am running Couchbase in an EC2 Ubuntu instance. I have opened all the necessary ports. I can connect to it from the developer laptop. However, when I deploy very similar code as an AWS Lambda I get this exception:

WARNING: [null][KeyValueEndpoint]: Socket connect took longer than specified timeout.
java.util.concurrent.TimeoutException
java.lang.RuntimeException: java.util.concurrent.TimeoutException
at com.couchbase.client.core.utils.Blocking.blockForSingle(Blocking.java:74)
at com.couchbase.client.java.CouchbaseCluster.openBucket(CouchbaseCluster.java:309)
at com.couchbase.client.java.CouchbaseCluster.openBucket(CouchbaseCluster.java:293)
at com.couchbase.client.java.CouchbaseCluster.openBucket(CouchbaseCluster.java:282)
at com.acme.user.AddUserLambda.getBucket(AddUserLambda.java:51)

The getBucket method is as follows.

public static Bucket getBucket() {

  CouchbaseCluster cluster = CouchbaseCluster.create(
      System.getenv(COUCHBASE_IP));
  Bucket bucket = cluster.openBucket(
      System.getenv(COUCHBASE_BUCKET), 
      System.getenv(COUCHBASE_PASSWORD));

  return bucket;
}

I have logged all the environment variables and they seem to be just fine. The exact same code works fine from my developer laptop.

The Lambda has “Simple Microservice Permission”. Any idea what I could be doing wrong? Thank you.


#2

I found the answer here. Basically I had to do two things:

  1. Increase Couchbase connection timeout as shown in that post.
  2. Increase memory for the Lambda to 512MB.

My updated getBucket() method is now:

public static Bucket getBucket() {
  CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
      .connectTimeout(10000) //10000ms = 10s, default is 5s
      .build();
  
  CouchbaseCluster cluster = CouchbaseCluster.create(
      env,
      System.getenv(COUCHBASE_IP));
  Bucket bucket = cluster.openBucket(
      System.getenv(COUCHBASE_BUCKET), 
      System.getenv(COUCHBASE_PASSWORD));

  return bucket;
}