Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
leej3 committed Jul 31, 2024
1 parent 39f1510 commit e6c2465
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 25 deletions.
1 change: 1 addition & 0 deletions web_api/.env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Used for local development of the web API
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# AWS_REGION="us-east-1"
Expand Down
6 changes: 4 additions & 2 deletions web_api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM tiangolo/uvicorn-gunicorn:python3.11

WORKDIR /app

COPY ./docker_images/web_api/requirements.txt /app/app/requirements.txt
COPY ./web_api/requirements.txt /app/app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/app/requirements.txt

# Consider installing from pypi
Expand All @@ -12,5 +12,7 @@ COPY osm /opt/osm/osm
ARG PSEUDO_VERSION=0.0.1 # strongly recommended to update based on git describe
RUN SETUPTOOLS_SCM_PRETEND_VERSION_FOR_OSM=${PSEUDO_VERSION} pip install -e /opt/osm
RUN --mount=source=.git,target=/opt/osm/.git,type=bind pip install -e /opt/osm
EXPOSE 8000
COPY ./web_api/main.py /app/app/main.py

COPY ./docker_images/web_api/main.py /app/app/main.py
# relies on the fact that the uvicorn-gunicorn image has a default CMD
6 changes: 3 additions & 3 deletions web_api/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ services:
osm_web_api:
image: osm_web_api
environment:
- MONGODB_URI="mongodb+srv://johnlee:<password>@cluster0.6xo8ws7.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
- MONGODB_URI="mongodb+srv://the_rest_of_the_connection_string"
build:
context: ../..
dockerfile: ./docker_images/web_api/Dockerfile
context: ..
dockerfile: ./web_api/Dockerfile
ports:
- 80:80
traefik:
Expand Down
49 changes: 30 additions & 19 deletions web_api/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#!/bin/bash
# Script for testing the deployment during local development... ideally this
# should be done in a CI/CD pipeline but sometimes that breaks...
# 1. Run the script from the project root directory i.e. bash web_api/deploy.sh
# 2. Ensure you have the necessary environment variables set in .env

# Set your variables here
# Set your variables in .env (see web_api/.env.template for help)
if [ -f .env ]; then
while IFS= read -r line || [[ -n "$line" ]]; do
export "$line"
done < .env
else
echo "No .env file found. Please create one using the .env.template file."
exit 1
fi

# Export AWS credentials
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY

# Log in to Docker Hub
echo "Logging in to Docker Hub..."
Expand All @@ -15,25 +24,27 @@ echo "Building and pushing Docker image..."
DOCKER_BUILDKIT=1 docker build -t $DOCKER_IMAGE_TAG -f ./web_api/docker_images/web_api/Dockerfile .
docker push $DOCKER_IMAGE_TAG

# Set up Terraform
echo "Setting up Terraform..."
terraform -install-autocomplete
pushd web_api/terraform
# Set up Terraform
echo "Setting up Terraform..."
terraform -install-autocomplete

# Initialize Terraform
echo "Initializing Terraform..."
terraform init -backend-config="./web_api/terraform/backend-config.tf"
# Initialize Terraform
echo "Initializing Terraform..."
terraform init -backend-config="./backend-config.tf"

# Plan Terraform changes
echo "Planning Terraform changes..."
terraform plan -var-file="./web_api/terraform/$ENVIRONMENT.tfvars"
# Plan Terraform changes
echo "Planning Terraform changes..."
terraform plan -var-file="./$ENVIRONMENT.tfvars"

# Apply Terraform changes
echo "Applying Terraform changes..."
terraform apply -var-file="./web_api/terraform/$ENVIRONMENT.tfvars" -auto-approve
# Apply Terraform changes
echo "Applying Terraform changes..."
terraform apply -var-file="./$ENVIRONMENT.tfvars" -auto-approve

# Get the instance ID and public DNS
instance_id=$(terraform output -raw instance_id)
public_dns=$(terraform output -raw public_dns)
# Get the instance ID and public DNS
instance_id=$(terraform output -raw instance_id)
public_dns=$(terraform output -raw public_dns)
popd

# Deploy Docker Compose on the instance
echo "Deploying Docker Compose on the instance..."
Expand Down
35 changes: 34 additions & 1 deletion web_api/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
provider "aws" {
region = "us-east-1"
region = var.aws_region
}

variable "environment" {
Expand Down Expand Up @@ -31,6 +31,12 @@ variable "domain" {
default = "osm.nimh.nih.gov"
}

variable "aws_region" {
description = "AWS region"
default = "us-east-1"
}

# Data source to find the latest Ubuntu AMI
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
Expand All @@ -40,6 +46,7 @@ data "aws_ami" "ubuntu" {
}
}

# VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"

Expand All @@ -48,6 +55,7 @@ resource "aws_vpc" "main" {
}
}

# Subnet
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
Expand All @@ -58,6 +66,7 @@ resource "aws_subnet" "main" {
}
}

# Security Group
resource "aws_security_group" "app" {
vpc_id = aws_vpc.main.id

Expand Down Expand Up @@ -87,6 +96,7 @@ resource "aws_security_group" "app" {
}
}

# EC2 Instance
resource "aws_instance" "app" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
Expand All @@ -107,6 +117,20 @@ resource "aws_instance" "app" {
EOF
}

# S3 Bucket for Terraform State
resource "aws_s3_bucket" "terraform_state" {
bucket = var.s3_bucket

versioning {
enabled = true
}

tags = {
Name = "${var.environment}-terraform-state"
}
}

# DynamoDB Table for Terraform State Locking
resource "aws_dynamodb_table" "terraform_locks" {
name = var.dynamodb_table
billing_mode = "PAY_PER_REQUEST"
Expand All @@ -121,3 +145,12 @@ resource "aws_dynamodb_table" "terraform_locks" {
Name = "${var.environment}-terraform-locks"
}
}

# Outputs
output "instance_id" {
value = aws_instance.app.id
}

output "public_dns" {
value = aws_instance.app.public_dns
}

0 comments on commit e6c2465

Please sign in to comment.