[파이썬] AWS Lambda와 Aurora Serverless 연동

Serverless architecture has gained popularity in recent years due to its scalability, cost-effectiveness, and simplicity. AWS Lambda is a popular serverless compute service provided by Amazon Web Services (AWS). It allows you to run your code without provisioning or managing servers. Aurora Serverless is another AWS service that provides an on-demand, auto-scaling, serverless version of the Aurora relational database engine.

In this blog post, we will explore how to connect AWS Lambda and Aurora Serverless in Python. We will write a Lambda function that interacts with an Aurora Serverless database using the Python programming language.

Prerequisites

Before we start, make sure you have the following prerequisites in place:

Setting up Aurora Serverless

First, let’s set up an Aurora Serverless database in the AWS Management Console. Follow these steps:

  1. Sign in to the AWS Management Console.
  2. Open the Amazon RDS console.
  3. Click on Create database and select Amazon Aurora.
  4. Choose Amazon Aurora with MySQL compatibility.
  5. Select Serverless under Capacity type.
  6. Configure the necessary settings like DB instance identifier, Credentials, and Network & Security.
  7. Click on Create database to create the Aurora Serverless database.

Once the database is created, note down the Endpoint and Credentials as we will need them later.

Creating an AWS Lambda function

Next, let’s create an AWS Lambda function that will connect to the Aurora Serverless database. Follow these steps:

  1. Open the AWS Management Console.
  2. Open the AWS Lambda console.
  3. Click on Create function.
  4. Choose a Function name and select Runtime as Python.
  5. Under Permissions, create or select an IAM role that has the necessary permissions to access the Aurora Serverless database.
  6. Click on Create function to create the Lambda function.

Installing the necessary dependencies

To connect to the Aurora Serverless database, we need to install the boto3 library, which is the AWS SDK for Python. We also need to install the PyMySQL library to interact with the MySQL-compatible Aurora Serverless database. Run the following command to install the dependencies:

pip install boto3 PyMySQL

Writing the Lambda function

Now, let’s write the code for the Lambda function. We will start by importing the necessary libraries and establishing a connection to the Aurora Serverless database.

import boto3
import pymysql

def lambda_handler(event, context):
  # Establish a connection to the Aurora Serverless database
  conn = pymysql.connect(
    host='your-aurora-endpoint',
    port=3306,
    user='your-username',
    password='your-password',
    database='your-database-name'
  )
  
  # Do something with the database connection
  
  # Close the database connection
  conn.close()

Replace the placeholders (your-aurora-endpoint, your-username, your-password, your-database-name) with the actual values of your Aurora Serverless database.

You can now use the conn object to execute SQL queries, perform database operations, and retrieve data from the Aurora Serverless database.

Testing the Lambda function

To test the Lambda function, you can either manually trigger it from the AWS Lambda console or use sample test event data. Make sure you provide the necessary input data required by your function.

Summary

In this blog post, we learned how to connect AWS Lambda and Aurora Serverless in Python. We set up an Aurora Serverless database, created an AWS Lambda function, installed the necessary dependencies, and wrote code to establish a connection to the database.

AWS Lambda and Aurora Serverless provide a powerful combination for building scalable and cost-effective serverless applications. With the potential for auto-scaling and on-demand capacity, you can focus on your application logic without worrying about infrastructure management.

Happy coding!