A deployable Go API with an automated setup process for an AWS Auto-Scaling Group (high-availability and performance), via an ELB endpoint.
The entire build (and destroy) process runs inside a docker container, initiated via Makefile targets.
If you'd like to run the internal build scripts on your native OS environment (without Docker), it requires you have installed:
- Python 3.7+
- aws-cli 1.16+
- Git 2.1+
- Golang 1.10+
- Packer 1.4.2+
- Terraform 1.12+
- jq 1.6+
- wrk 4.1.0+
Homebrew: brew install python awscli git go packer terraform jq wrk
- With Docker: All steps have been tested using Docker Desktop v2.1
- Without Docker: All internal scripts have been tested on MacOS v10.14 - Mojave
Obtain your private AWS credentials, then either setup aws configure
or export the environment variables.
export AWS_ACCESS_KEY_ID=.....
export AWS_SECRET_ACCESS_KEY=....
Ensure your credentials has IAM "AdministratorAccess"
permissions.
(optional) You may specify your desired AWS region (us-east-1, eu-west-2, etc) via env var:
export AWS_REGION=us-east-1
Default region: us-east-1
Run the Makefile target:
make docker_build_deploy_all
This will start the following sequence:
-
Build a docker container with tag and run it: "api_build_deploy"
-
Inside the Docker container, it will perform the following actions:
- Init local files, ssh keys, etc
- Run test and compile the Golang API binary
- Build an AWS AMI (image) using Packer
- Build an AWS Auto-Scaling Group (ASG) to deploy and manage the AMI image using Terraform
- Run health check & automated load test on the ELB API endpoint for 30 seconds
⭐ Recommended to run the above Makefile target using the predefined Dockerfile.
However, you can run the above build process without Docker with the Makefile target: make external_build_deploy_all
-
AMI image info:
- Instance Type:
t2.micro
- OS:
Debian
- Tags:
Name:API - {date}, Release:api-latest
- Instance Type:
-
Auto-Scaling Group (ASG) will start with one instance, with up to five instances capacity.
-
CloudWatch alarms are created to notify ASG when any EC2 "CPU utilization" average is:
- High - above the 60% CPU threshold, which increases ASG capacity by one instance at a time.
- Low - below the 10% CPU threshold, which decreases ASG capacity by one instance at a time.
The above (and more) can be configurable via the variables files:
- Packer:
infras/packer/variables.json
- Terraform:
infras/terraform/variables.json
Run the Makefile target:
make docker_destroy_all
Cleanup script will perform the following sequence:
- Terraform will destroy this specific AWS (API) deployment configuration (based on existing 'terraform.tfstate' file)
- Deregisters all related API images via tag
"Release:api-latest"
- Deletes all related Snapshots via tag
"Release:api-latest"
⭐ Recommended to run the above Makefile target using the predefined Dockerfile.
However, you can run the above destroy process without Docker with the Makefile target: make external_destroy_all
- Docker run is configured to mount this project directory as a volume.
- Generated files (ie: terraform.tfstate, ssh keys, etc) on build time should be available in the current directory, should you need it for backup purposes.
🤓 For questions or comments, please create an Issue or contact me anytime.
Enjoy!