Skip to content

Commit

Permalink
make some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leej3 committed Jul 31, 2024
1 parent e6c2465 commit 7ecb10b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 58 deletions.
18 changes: 10 additions & 8 deletions web_api/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 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 -eux
# Set your variables in .env (see web_api/.env.template for help)
if [ -f .env ]; then
while IFS= read -r line || [[ -n "$line" ]]; do
Expand All @@ -24,22 +24,24 @@ 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="./backend-config.tf"
tofu init -reconfigure -backend-config="./backend-config-${ENVIRONMENT}.tf"


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

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

# Get the instance ID and public DNS
instance_id=$(terraform output -raw instance_id)
Expand All @@ -51,7 +53,7 @@ echo "Deploying Docker Compose on the instance..."
ssh -o StrictHostKeyChecking=no -i dsst2023.pem ubuntu@$public_dns -p 2222 << EOF
mkdir -p ~/app
cd ~/app
echo "$(<./web_api/docker-compose.yml)" > docker-compose.yml
echo "$(<./web_api/compose.yaml)" > compose.yaml
export ENVIRONMENT=$ENVIRONMENT
export MONGODB_URI=$MONGODB_URI
docker-compose up -d
Expand Down
8 changes: 0 additions & 8 deletions web_api/terraform/backend-config.tf

This file was deleted.

43 changes: 7 additions & 36 deletions web_api/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,15 @@ provider "aws" {
region = var.aws_region
}

variable "environment" {
description = "The environment to deploy to (staging or production)"
type = string
}

variable "instance_type" {
default = "t3.micro"
}

variable "ssh_port" {
description = "Non-standard port for SSH"
default = 2222
}

variable "s3_bucket" {
description = "S3 bucket for Terraform state"
default = "osm-storage"
}

variable "dynamodb_table" {
description = "DynamoDB table for Terraform state locking"
default = "terraform-locks"
}

variable "domain" {
description = "Domain for Traefik"
default = "osm.nimh.nih.gov"
terraform {
backend "s3" {
bucket = "osm-storage"
key = "terraform/staging/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
}
}

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

# Data source to find the latest Ubuntu AMI
data "aws_ami" "ubuntu" {
Expand Down Expand Up @@ -120,11 +96,6 @@ resource "aws_instance" "app" {
# S3 Bucket for Terraform State
resource "aws_s3_bucket" "terraform_state" {
bucket = var.s3_bucket

versioning {
enabled = true
}

tags = {
Name = "${var.environment}-terraform-state"
}
Expand Down
22 changes: 16 additions & 6 deletions web_api/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ variable "instance_type" {
default = "t3.micro"
}

variable "spot_instance_type" {
description = "EC2 spot instance type"
default = "t3.micro"
}

variable "subnet_ids" {
description = "List of subnet IDs"
type = list(string)
Expand All @@ -38,5 +33,20 @@ variable "mongodb_db" {

variable "domain" {
description = "Domain for Traefik"
default = ""
default = "osm.nimh.nih.gov"
}

variable "ssh_port" {
description = "Non-standard port for SSH"
default = 2222
}

variable "s3_bucket" {
description = "S3 bucket for Terraform state"
default = "osm-storage"
}

variable "dynamodb_table" {
description = "DynamoDB table for Terraform state locking"
default = "terraform-locks"
}

0 comments on commit 7ecb10b

Please sign in to comment.