开发者

How do I write a Lamdda function in AWS that allows me to connect to an EC2 instance that is running MySQL

开发者 https://www.devze.com 2022-12-07 22:22 出处:网络
I have created an EC2 i开发者_如何学Gonstance, installed and configured MySQL on it (not RDS), I have created a database (airpollutiondata) and a table named (CO2).

I have created an EC2 i开发者_如何学Gonstance, installed and configured MySQL on it (not RDS), I have created a database (airpollutiondata) and a table named (CO2).

I am trying to write a Lambda function (running node.js) that will connect to MySQL (which is running on my EC2 instance) and run a select statement to pull some data.

I have tried everything that I can think of (sample code below without the select statement).

I am hoping that someone might be able to steer me in the correct direction. Here is the code that I have tested unsuccessfully in AWS Lamda.

var con = mysql.createConnection({ 
host: "localhost", 
user: "yourusername", 
password: "yourpassword", 
database: 'airpollutiondata' 
}); 
con.connect(function(err) { 
if (err) throw err; 
console.log("Connected!"); 
});

I tried to find resources online that would steer me in the correct direction, but everything seems to be geared towards using RDS with MySql (unfortunately this isn't the way that I set this up)


You do not provide any information about what exactly does happen. But I will assume that your code does work except for the host field, where you need to replace localhost with the private IP address of your instance.

  • Make sure that mysql does listen on all interfaces - see https://unix.stackexchange.com/questions/43025/how-to-allow-mysql-remote-connections-via-particular-interface
  • An AWS lambda does not have by default any access to your VPC, which means it has no way how to connect there. https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html
  • Make sure that the security group you assign to your lambda can create outbound connections to your EC2.
  • Make sure that the security group you assign to your EC2 accepts connection from security group of your lambda.
0

精彩评论

暂无评论...
验证码 换一张
取 消