diff --git a/web_api/deploy.sh b/web_api/deploy.sh index 1e27f8db..aead6969 100755 --- a/web_api/deploy.sh +++ b/web_api/deploy.sh @@ -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 @@ -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) @@ -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 diff --git a/web_api/terraform/backend-config.tf b/web_api/terraform/backend-config.tf deleted file mode 100644 index d145d2ea..00000000 --- a/web_api/terraform/backend-config.tf +++ /dev/null @@ -1,8 +0,0 @@ -terraform { - backend "s3" { - bucket = "osm-storage" - key = "terraform/${ENVIRONMENT}/terraform.tfstate" - region = "us-east-1" - dynamodb_table = "terraform-locks" - } -} diff --git a/web_api/terraform/main.tf b/web_api/terraform/main.tf index da3c0c07..3bcbe46e 100644 --- a/web_api/terraform/main.tf +++ b/web_api/terraform/main.tf @@ -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" { @@ -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" } diff --git a/web_api/terraform/variables.tf b/web_api/terraform/variables.tf index aae631b2..76abee1b 100644 --- a/web_api/terraform/variables.tf +++ b/web_api/terraform/variables.tf @@ -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) @@ -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" }