Easiest way to install mysqldump on Amazon Linux 2023 running on AWS Lambda

410 views Asked by At

I need mysqldump (or mariadb-dump) inside my Lambda function.
I chose I want to go the Docker way and used the public.ecr.aws/lambda/nodejs:20 base image.

To add MySQL/MariaDB I added the following to my Dockerfile

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

This works but bloats the image size to ~800MB.
Is there a better/easier way to only add mysqldump and the necessary dependencies?

I looked at the other services AWS offers but none really fit the bill ...

Dockerfile I'm testing with

FROM public.ecr.aws/lambda/nodejs:20

# Installing mysqldump and cleaning dnf cache
RUN dnf update -y && \
    dnf -y install mysql-utilities && \
    dnf clean all && \
    # dnf autoremove -y && \ # => not working b/c this is only microdnf which doesn't have autoremove
    rm -rf /var/cache/{dnf,yumdb,metadata}

Build command I use

docker build . --platform linux/amd64 -t mysqldump-node20
1

There are 1 answers

0
Dan B On

I'm not sure it's the easiest but I think the lightweight solution is to bundle the mysqldump binary in your zip code source. Here were my steps:

  • Spin up an EC2 with the correct os based on your lambda runtime.
  • Install mysql on the EC2. I used this guide here. Those steps consolidated are:
    • sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-1.noarch.rpm
    • sudo dnf install mysql80-community-release-el9-1.noarch.rpm -y
    • sudo dnf install mysql-community-server -y
      • If you get an error on that step about GPG keys you may also need to do sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023 then rerun the last step.
  • SCP usr/bin/mysqldump to your machine to zip with your code.

Then I just needed to make sure my code referenced it in the correct directory it ended up in based on folder structure of the zip.