AWS Lambda and GCP MySQL Database Integration
A lot of us would have worked on different services offered by AWS and GCP independently, But what if there’s a requirement to integrate the services to get the desired results? Well in this article we will see how can we access a MySQL database instance present in a GCP project from an AWS Lambda function. We will follow the below steps to achieve this.
Setup done in GCP
- Create a MySQL instance in a GCP project with the default configuration.
2. Open the Cloudshell, and SSH into the DB instance using the below command.
gcloud sql connect tutorial-instance --user=root --quiet
3. Once you are successfully connected, Create a database and a table with some records using these SQL statements and run the SELECT query to view all the records from the table.
Now we are successfully done with the GCP part so let’s see what needs to be done on AWS!
Setup done in AWS
1. First we will create a lambda function with a Python 3.7 runtime and the required execution role. Once this is done, Add a custom lambda layer of MySQL to include all the MySQL libraries in the function.
2. Now we need to set up the network configuration in AWS so that the lambda function can access the internet and have a successful connection with our MySQL instance.
3. Create a VPC and associate the subnets (1 Public and 2 Private Subnets), with the required CIDR range.
4. Now, We will create two Route tables to direct the network traffic from our subnets. Create private and public route tables and associate them with the private and public subnets respectively.
5. Create an Internet Gateway to allow communication between our VPC and the Internet. Once this is done, Associate it with our public route table.
6. Now we need to have a static, public IPv4 address i.e. an Elastic IP to enable communication with the internet. Once this is done, We will create a NAT Gateway which will enable the lambda function in the private subnet to connect to the internet. The NAT Gateway is created in the public subnet and associated with the EIP.
7. Now we need to associate this NAT Gateway with our Private route table
8. This completes our whole networking setup and now we are ready to attach this VPC to our Lambda function.
9. Select the Configuration tab in our mysql-tutorial-lambda function that we have created and click on VPC. Add the tutorial-vpc that we have created, Select the 2 private subnets associated with our VPC and finally select the Default security group. Once this is done, Save the configuration.
10. Now we need to add the Elastic IP that we created above, to the MySQL instance present in our GCP project.
11. Note the public IP address associated with our SQL instance, As we will use it in our Lambda function code for making the connection.
12. Edit the Lambda function and paste the below code. The host value in the code will be the public IP address of our SQL instance that we copied from above.
13. Deploy the function and test it. You will get the success message and the desired records from the database instance if your entire setup is error-free.
There you go! We just created a fully functional Lambda function in AWS and successfully connected it to the MySQL instance in GCP. In the next article, we will see how to use a custom MySQL lambda layer to reduce the dependency of the code.
I hope you have enjoyed it. Please do not hesitate to give feedback, share your own lessons, or simply like it :)