Builds and deploys a flask and node app on AWS using Terraform.
-
Create an AWS account.
-
Create an IAM user, assign the user
Programmatic access, attach theAdministratorAccessIAM policy to the user, and save the access key ID and secret access key ID. -
Install the aws cli by running the following command:
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
-
Configure your aws profile locally by running the below command and filling out the parameters using the keys saved from step 2:
aws configure -
Import a key pair to the EC2 service in the region you will be deploying in or create a key pair, if you don't already have an ssh key.
-
Update the following variables in the manifest.tfvars file:
region - set this to the region you are deploying in
stack_name - set this to the stack name of your choosing
key_name - set this to the name of the key pair you generated in step 5
vpc_id - set this to the default vpc id in region you are deploying in
public_subnet - set this to one of the default subnet id's that exist in the region you are deploying in
account_id - set this to your AWS account id
- Mac users will need to install brew prior to running the requirements.sh script using the following command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Run the requirements install script to install the required tools on Mac OS:
chmod +x requirements.sh
./requirements.sh
Replace <path_to_variables_file> with the path to the manifest.tfvars file.
Replace <aws_profile> with the aws profile name.
Run the Terraform plan script to get a plan of the infrastructure that will be deployed:
./plan.sh <path_to_variables_file> <aws_profile>
Once you have ran the Terraform plan script, run the Terraform deploy script to deploy the infrastructure:
./deploy.sh <path_to_variables_file> <aws_profile>
To build the docker images, run the following command:
./scripts/docker_build.sh <path_to_variables_file> <aws_profile>
To view the solution, follow the steps below:
-
Identify the Public DNS name of the ecs cluster that is running by navigating to the AWS EC2 service, selecting instances, selecting the instance and finding the Public DNS(IPv4) description (ex.
ec2-<IP_ADDRESS>.<REGION>.compute.amazonaws.com) -
Open a web browser (Mozilla Firefox is preferred. Google Chrome will block requests due to the SSL cert being invalid.)
-
Navigate to the Public DNS name identified in step 1 using http. (ex.
http://ec2-<IP_ADDRESS>.<REGION>.compute.amazonaws.com) You will be redirected to https and will need to selectAdvanced, and then selectAccept the Risk and Continue. Nginx will redirect the request to the flask container serving the flask api. -
To access the node container serving the node calculator, navigate to any of the below paths in order to test the calculator (replace [number] with any number to perform the calculation):
http://ec2-<IP_ADDRESS>.<REGION>.compute.amazonaws.com/add/[number]/[number]
http://ec2-<IP_ADDRESS>.<REGION>.compute.amazonaws.com/subtract/[number]/[number]
http://ec2-<IP_ADDRESS>.<REGION>.compute.amazonaws.com/multiply/[number]/[number]
http://ec2-<IP_ADDRESS>.<REGION>.compute.amazonaws.com/divide/[number]/[number]
Once you have no further need for the infrastructure, run the Terraform destroy script to destroy the deployed infrastructure:
./destroy.sh <path_to_variables_file> <aws_profile>
- Build own VPC
- Make infrastructure highly available by including a load balancer, running multiple services in multiple AZ's.
- Create single script to run all commands.
- Tag docker images with a unique ID.
- Use ansible to generate SSL certs, build ngingx config files.