Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect RDS instance when running SAM locally?

I am using SAM (Serverless application model) to test Lambda functions locally that connect to an Aurora RDS instance in the cloud.

Using the following command:

sam local invoke "lambda function name" --event event.json

The Lambda function is being executed but when it comes to returning SQL results, it's returning null as output.

How can I configure the Docker container to communicate with the RDS instance?

like image 901
Jignesh Patel Avatar asked Feb 21 '19 03:02

Jignesh Patel


1 Answers

As mentioned in the help for sam local invoke, you can connect your Docker container to an existing Docker network:

▶ sam local invoke --help                
...
  --docker-network TEXT           Specifies the name or id of an existing
                                  docker network to lambda docker containers
                                  should connect to, along with the default
                                  bridge network. If not specified, the Lambda
                                  containers will only connect to the default
                                  bridge docker network.

So, to list your Docker networks:

▶ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
25a03c8453a6        bridge              bridge              local
00de89cf09d0        host                host                local
41597d91a389        none                null                local

Then, to connect your Lambda function's Docker container to the host network:

▶ sam local invoke "lambda function name" --event event.json \
    --docker-network 00de89cf09d0

Note that you can also use the environment variable SAM_DOCKER_NETWORK:

▶ SAM_DOCKER_NETWORK=00de89cf09d0 sam local invoke "lambda function name" \
    --event event.json

As mentioned here.

Assuming the host network can access the RDS instance, that should fix your problem.

like image 116
Alex Harvey Avatar answered Sep 22 '22 11:09

Alex Harvey