I DID it! AWS Lightsail MySQL Django Setup Automation

Binary Belle
4 min readSep 2, 2020
Photo by Alex Knight on Unsplash

I admit, it took me days to do this. Days. I could have typed it in a hundred plus times, in the time it took me to “automate it”.

BUT, I struggle with (edit: am bored senseless) doing things manually, when I could automate them…even when I have existential crises at multiple times along the way, KNOWING I could have just TYPED THE INFORMATION INTO THE COMMAND LINE.

Nope…I can’t make me. I’ve tried. I’m driven to automate. I justify my automation by promising myself I’ll use it again. I sell it to myself, by reminding myself I learn SOO much when I automate.

Those are both true. I do.

I almost quit this several times. And I already figured out how I could have done the whole thing in python. I even started to re-do it in python, before I even had it FINISHED in bash. I made myself stop.

And tonight, after three days of working on this…my automation WORKED!!

What does it do? Does it matter? I hit the GO button, and it RUNS all the way to the end!

Okay, maybe it does matter a little what it does…

What I’ve automated (so far)…are all the steps it takes from the time I setup the Django AWS Lightsail instance in the UI (yeah, I know…I figured out this afternoon that I can automate that, too…..sigh. I stopped myself from going down that rabbit hole…so far.)

Anyway, I create the instance in the Console, and attach it to a static IP. Then I login…I sftp a few files to it…and I type in ./run_setup.sh

…and wait.

…and try not to slam my head on the desk when it fails, which it did for the last three days, until tonight.

When the script is done running, I have:

  1. All the directories I need for the project
  2. python3-pip, wheel, and virtualenvwrapper installed
  3. created and activated a virtual environment for python.
  4. upgraded pip, and installed django and mysqlclient inside the virtual environment
  5. created a shiny new database, using the credentials the instance created (NO hard-coded passwords in the script!) (ALSO, btw, I am not using the AWS managed DB right now)
  6. run mysql_secure_installation, passing answers in programmatically (WARNING: BE CAREFUL with this command! You can lock yourself right out of that fancy new database you made with this! Guess how I know this.)
  7. started the project with django-admin, run migrate the first time, and started a django app.
  8. installed boto3, which allows access to the AWS Lightsail API
  9. used the account credentials, passed into the script, to configure the AWS CLI tool.
  10. programmatically created a PORT without using the UI (I am way too excited about this)
  11. retrieved the instance’s static IP address, to use in the ALLOWED_HOSTS field of the Django settings file.
  12. created a local_settings file for the Django project, which has the database configuration, the INSTALLED_APPS, TIME_ZONE, and the ALLOWED_HOSTS in it.
  13. written both urls.py files in the project and the apps directories
  14. run makemigrations and migrate (again)

When it was done running without failing….I logged into mysql (holding my breath), and was STUNNED to discover the tables it made are actually IN the database it also made!

I still have more to automate. Of course I do. I’m going to put the initial pieces of the application in the setup script, too. Why wouldn’t I?

I’m planning to put the whole hot mess in github so someone else can use it instead of spending days and days automating it, like I did…

…which is another way I justified this automation project to myself…someone ELSE can use this, too! Right!? :)

I’m positive someone’s already done this somewhere. But I couldn’t find it. Plus, I learned a bunch of bash and more python and how to pass things back and forth between them. And I learned how to connect to AWS’s API.

I’ll write another post with more tech details about this (when I’m “done” — as if there’s such a thing…automating it more.)

Maybe sometime I’ll “learn” how to find someone else’s already existing automation code, so I spend less time on this…although I suspect I’ll just go busy myself automating something else, hoping ONE of these days all that automation will automate itself, so I don’t have to.

What have you automated that really took a bit to do, but THRILLED you when you were done with it? I’d love to hear about it.

--

--

Binary Belle

Senior Software Engineer, Inventor, Writer, Zumba Instructor, Storm and Sky Photographer, Drone Pilot, Shar Pei Lover & Owner