Setting up MySQL on Amazon Linux 2023 for AWS Lambda containers is essential for developers looking to manage databases efficiently in cloud-based environments. This guide provides a comprehensive step-by-step approach to installing and configuring MySQL on Amazon Linux 2023, ensuring seamless database integration with AWS Lambda.

Prerequisites

Before proceeding with the installation, ensure that the following requirements are met:

  • An AWS account with necessary permissions
  • An Amazon Linux 2023 environment
  • Docker installed and configured (for containerized deployment)
  • AWS CLI set up for managing AWS Lambda

Step 1: Update the System

Before installing MySQL, update the package manager to ensure the latest packages are available:

sudo dnf update -y

Step 2: Install MySQL Server

To install MySQL on Amazon Linux 2023, execute the following command:

sudo dnf install mysql-server -y

Once installed, enable and start the MySQL service:

sudo systemctl enable mysqld

sudo systemctl start mysqld

Verify the status of the MySQL service:

sudo systemctl status mysqld

Step 3: Secure MySQL Installation

Run the security script to configure MySQL securely:

sudo mysql_secure_installation

Follow the prompts to set a root password and remove any default security vulnerabilities.

Step 4: Configure MySQL for AWS Lambda Container

To ensure MySQL runs smoothly in a containerized environment, update the MySQL configuration file located at /etc/my.cnf:

sudo nano /etc/my.cnf

Add the following configuration settings:

default-authentication-plugin=mysql_native_password

bind-address=0.0.0.0

Save and exit the file, then restart MySQL:

sudo systemctl restart mysqld

Step 5: Create a Database and User

Log into MySQL and create a database for AWS Lambda:

mysql -u root -p

Execute the following SQL commands to create a database and a user with access privileges:

CREATE DATABASE aws_lambda_db;

CREATE USER ‘lambda_user’@’%’ IDENTIFIED BY ‘StrongPassword123’;

GRANT ALL PRIVILEGES ON aws_lambda_db.* TO ‘lambda_user’@’%’;

FLUSH PRIVILEGES;

EXIT;

Step 6: Dockerize MySQL for AWS Lambda Container

If deploying MySQL in a Lambda container, create a Dockerfile for the MySQL instance:

FROM amazonlinux:2023

RUN dnf update -y && dnf install -y mysql-server

COPY my.cnf /etc/my.cnf

CMD [“mysqld”]

 

Build and run the Docker container:

docker build -t mysql-lambda .

docker run -d -p 3306:3306 –name mysql-container mysql-lambda

Conclusion

By following this guide, MySQL is successfully installed and configured on Amazon Linux 2023 for AWS Lambda container environments. This setup ensures robust database management while maintaining high availability and scalability within AWS cloud infrastructure.