Serverless Aurora Schema


I’ve recently started using Aurora Serverless and I have everything up and running in terms of infrastructure, but I’m having some issues when it comes to actually setting up the database itself.

So, when my stack finishes deploying I have a lambda function that runs immediately the sole responsibility of which is to set up the database tables.

I have a sql file that defines the table creation queries which as it stands, I am bundling with my lambda reading it from disk, splitting the file at ; to separate each query then using the Data API to run them in sequence.

This works and I suppose does the job, but it feels wrong. MySQL has import / export functionality, is there anyway you can define the schema of the database at creation, or run a sql file without having to manually separate the queries and manage their execution?

Thanks for any help!

What I usually do is creating a bastion host (ec2) with SSH connection only, and then stablish a connection through this ec2. In my opinion this is way more flexible and easier than managing it through lambda functions - and also can be done automatically using serverless. The only thing you’ll have to do is connecting to the bastion host and running your script - or even create a script that does it all after the serverless deploy is successful, thus making your aurora db setup happen soon after it’s deployed.

I’m really curious if there’s a better way than automating this process, because I am using serverless less than a year, so this possibility is totally plausible.

I’m also looking at starting a small proof of concept application with lambda + aurora serverless. Can anyone provide some pointers to code that will show an example of how to provision an Aurora Serverless within the serverless framework? i.e. a serverless.yml file with the right stuff to fire up the Aurora Serverless DB via cloudformation along with the other stuff in the stack? Thanks in advance…

Which langauge you use for lambda function definition.If it is python their is handy library called peewee which helps to define migration scripts and link with bootstrapper which will create underline tables.

Here is the entire section from serverless.yml which worked for me

      Type: AWS::RDS::DBCluster
        MasterUsername: *********************
        MasterUserPassword: ***************************
        #to populate secrets from secrets manager see here 
        DatabaseName: db
        Engine: aurora
        EngineMode: serverless
        EnableHttpEndpoint: true
          AutoPause: true
          MaxCapacity: 1
          MinCapacity: 1
          SecondsUntilAutoPause: 300