Unable to connect to Elasticache serverless memcached from Lambda within same VPC

147 Views Asked by At

I'm trying to implement AWS Elasticache serverless memcached for Lambda. I have successfully provisioned serverless Elasticache inside VPC spanning two private subnets. I have enabled dns_hostnames for VPC as well. I have successfully deployed lambda inside same VPC using same 2 private subnets. I have attached security group allowing 11211 TCP & 11212 TCP inbound traffic to my Elasticache. I'm using memcache-plus NPM module inside lambda to connect to Elasticache server. I have even attached elasticache:* IAM permission on lambda. The problem is, whenever I invoke my lambda it is failing at this step:

const MemcachePlus = require('memcache-plus');
const client = new MemcachePlus({
  hosts: [process.env.ELASTICACHE_ENDPOINT],
  autodiscover: true,
  onNetError: function (err) {
    console.error(err);
  },
});

Lambda Security Group:

resource "aws_security_group" "vpc_enabled_lambda_sg" {
  name        = "bp-${terraform.workspace}-vpc-lambda-sg"
  description = "Managed by Terraform"
  vpc_id      = aws_vpc.db_proxy_vpc.id

  # To Allow Port 80 Transport
  ingress {
    from_port   = 80
    protocol    = "tcp"
    to_port     = 80
    cidr_blocks = ["10.0.0.0/16"]
    description = "Allow HTTP traffic from within VPC"
  }

  # Open port 8000 for external access
  ingress {
    from_port   = 443
    protocol    = "tcp"
    to_port     = 443
    cidr_blocks = ["10.0.0.0/16"]
    description = "Allow HTTPS traffic from within VPC"
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  lifecycle {
    create_before_destroy = true
  }
}

Elasticache Security Group:

resource "aws_security_group" "elasticache_memcached_sg" {
  name        = "bp-${terraform.workspace}-elasticache-memcached-sg"
  description = "Managed by Terraform"
  vpc_id      = aws_vpc.db_proxy_vpc.id

  # To Allow Port 11211 Transport
  ingress {
    from_port   = 11211
    protocol    = "tcp"
    to_port     = 11211
    cidr_blocks = ["10.0.0.0/16"]
    description = "Allow memcache traffic from within VPC"
  }

  # Open port 8000 for external access
  ingress {
    from_port   = 11212
    protocol    = "tcp"
    to_port     = 11212
    cidr_blocks = ["10.0.0.0/16"]
    description = "Allow memcache read traffic from within VPC"
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  lifecycle {
    create_before_destroy = true
  }
}

What is am I missing here, to successfully connect to Elasticache?

Tried invoking lambda, moving MemcachePlus class inside & outside lambda handler.

0

There are 0 best solutions below