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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With