This repo uses terraform and AWS to host the spring-experiments application using a lambda function and API gateway: https://aws.jeremygreen.me.uk.
Static analysis is run on GitHub actions:
- The spring-experiments app is built using a CI job and the jar is pushed into an AWS S3 bucket.
- API gateway requests are configured with heavy rate limiting (to cap lambda costs and to simulate an overloaded API), so it gives 429 HTTP responses if make too many requests. E.g. if scroll through list of local authorities too fast. Retries in the frontend app hide this from the user.
- Monitoring is done with UptimeRobot (the free plan, so there's no scheduled downtime facility).
- Lambda functions are not a good way to host a production Spring server since the startup time is long - from e.g. JVM and Spring auto-configuration etc.
- Startup latency could be partially mitigated with Lambda SnapStart (or an external system polling the API to keep lambda instance alive, though this wouldn't help if/when need more than one instance).
- For this site's very low usage levels, lambda functions are cheaper than a permanently running EC2 instance. The aim here is to learn about things: e.g. how to handle a slow server.
These instructions are for Ubuntu and bash.
- Create an AWS account.
- Configure an AWS IAM user with appropriate permissions.
- Download required version of terraform.
- Unzip terraform.
- Rename terraform executable to add version suffix - e.g. terraform-1.2.3.
- Put renamed terraform executable in directory that is on PATH.
- Create ~/.dns-api-password and ~/.dns-api-url with appropriate content and permissions etc.
- Run (from a bash shell):
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
eval "$(~/.pyenv/bin/pyenv init -)"
~/.pyenv/bin/pyenv install
pyenv exec python -m venv --clear .venv
. environment
pip install --upgrade pip==23.3.1
pip install -r requirements.txt
aws configure # eu-west-1, json.
Close and reopen terminal, then just need to run:
. environment
To configure emacs:
- Add melpa package-source configuration to ~/.emacs
- From emacs,
M-x list-packagesand install terraform-mode.
To deploy:
cd terraform
terraform init
terraform plan
terraform apply
An S3 bucket is used to store the terraform state. If this bucket doesn't exist yet, temporarily comment out the "backend" configuration in main.tf while deploying, and it will be created for you (and then your temporary local state will get migrated to S3). This trick will only work if are deploying for the first time.
Update requirements.txt, perhaps clearing or editing constraints.txt too, then:
pip install -r requirements.txt
pip freeze > constraints.txt