I am trying to do local development setup for serverless architecture. Amazon provide SAM Local Beta for doing so. But in my project we are using mysql database. I am trying to make a connection to my local mysql server it failed saying.
module initialization error:
(2003, "Can't connect to MySQL server on 'localhost'
([Errno 99] Cannot assign requested address)")
Below is my python code:
import json
import pymysql
def lambda_sandbox_down_handler(event, context):
rds_host = 'localhost'
name = 'localhost'
password = 'localhost'
db_name = 'localhost'
conn = pymysql.connect(rds_host,port=3306, user=name, passwd=password,
db=db_name,charset='utf8mb4',connect_timeout=5)
return conn
I am able to make connection using command line as well as pycharm.
My question is this possible in SAM local.
I have installed a docker image of mysql and started it. After this I am still getting the same error.
My question is this possible in SAM local: Yes, you can run Lambda etc in SAM local, SAM local provides the environment by running it inside the docker container.
So to make SAM build successful, you need to install docker in your local machine and do a SAM build.
Once SAM build is successful you can run you program. To connect to localhost in mac please use host.docker.internal in place of localhost if you are using docker 18.03 or greater, for previous versions of docker you can use docker.for.mac.localhost.
Also to connect to your mysql, you don't need to run mysql inside the container.
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