diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 993ceb10..cacb0276 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -219,7 +219,7 @@ jobs: - name: Deploy to venue id: terraform-deploy - working-directory: examples/cumulus-tf + working-directory: terraform env: AWS_DEFAULT_REGION: us-west-2 run: | diff --git a/terraform/bin/config.sh b/terraform/bin/config.sh deleted file mode 100644 index a4972d6e..00000000 --- a/terraform/bin/config.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash -set -eo pipefail -while [[ $# -gt 0 ]] -do -VENUE="$1" - -source "$(dirname $BASH_SOURCE)/../environments/$VENUE.env" - -export TF_IN_AUTOMATION=true # https://www.terraform.io/cli/config/environment-variables#tf_in_automation -export TF_INPUT=false # https://www.terraform.io/cli/config/environment-variables#tf_input - -export TF_VAR_region="$REGION" -export TF_VAR_stage="$VENUE" - -terraform init -reconfigure -backend-config="bucket=$BUCKET" -backend-config="region=$REGION" diff --git a/terraform/bin/deploy.sh b/terraform/bin/deploy.sh new file mode 100644 index 00000000..6908e4c9 --- /dev/null +++ b/terraform/bin/deploy.sh @@ -0,0 +1,65 @@ +#!/usr/bin/env bash + +set -Eexo pipefail + +# Read in args from command line + +POSITIONAL=() +while [[ $# -gt 0 ]] +do +key="$1" + +case $key in + --ticket) + ticket="$2" + shift # past argument + shift # past value + ;; + --app-version) + app_version="$2" + shift # past argument + shift # past value + ;; + --lambda_container_image_uri) + lambda_container_image_uri="$2" + shift # past argument + shift # past value + ;; + -v|--tf-venue) + tf_venue="$2" + case $tf_venue in + sit|uat|ops) ;; + *) + echo "tf_venue must be sit, uat, or ops" + exit 1;; + esac + shift # past argument + shift # past value + ;; + *) # unknown option + POSITIONAL+=("$1") # save it in an array for later + shift # past argument + ;; +esac +done +set -- "${POSITIONAL[@]}" # restore positional parameters + +# https://www.terraform.io/docs/commands/environment-variables.html#tf_in_automation +TF_IN_AUTOMATION=true + +# Terraform initialization +terraform init -reconfigure -input=false -backend-config="bucket=podaac-services-${tf_venue}-terraform" -backend-config="profile=ngap-service-${tf_venue}" + +if [[ "${ticket}" ]]; then + set +e + terraform workspace new "${ticket}" + set -e + terraform workspace select "${ticket}" +else + terraform workspace select default +fi + +terraform plan -input=false -var-file=tfvars/"${tf_venue}".tfvars -var="credentials=~/.aws/credentials" -var="profile=ngap-service-${tf_venue}" -var="app_version=${app_version}" -var="lambda_container_image_uri"=${lambda_container_image_uri} -out="tfplan" + +# Apply the plan that was created +terraform apply -input=false -auto-approve tfplan \ No newline at end of file diff --git a/terraform/bin/destroy.sh b/terraform/bin/destroy.sh new file mode 100644 index 00000000..4313e047 --- /dev/null +++ b/terraform/bin/destroy.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +set -Eexo pipefail + +POSITIONAL=() +while [[ $# -gt 0 ]] +do +key="$1" + +case $key in + --ticket) + ticket="$2" + shift # past argument + shift # past value + ;; + --app-version) + app_version="$2" + shift # past argument + shift # past value + ;; + -v|--tf-venue) + tf_venue="$2" + case $tf_venue in + sit|uat|ops) ;; + *) + echo "tf_venue must be sit, uat, or ops" + exit 1;; + esac + shift # past argument + shift # past value + ;; + *) # unknown option + POSITIONAL+=("$1") # save it in an array for later + shift # past argument + ;; +esac +done +set -- "${POSITIONAL[@]}" # restore positional parameters + +# https://www.terraform.io/docs/commands/environment-variables.html#tf_in_automation +TF_IN_AUTOMATION=true + +# Verify that you are in test workspace +current_workspace=$(terraform workspace show) + +if [ $current_workspace == ${ticket} ]; then + # Terraform initialization + terraform init -reconfigure -input=false -backend-config="bucket=podaac-services-${tf_venue}-terraform" -backend-config="profile=ngap-service-${tf_venue}" + terraform destroy -auto-approve -var-file=tfvars/"${tf_venue}".tfvars -var="credentials=~/.aws/credentials" -var="profile=ngap-service-${tf_venue}" -var="app_version=${app_version}" + terraform workspace select default + terraform workspace delete ${ticket} +else + exit 1 +fi \ No newline at end of file