-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60d9c57
commit 7cca611
Showing
14 changed files
with
219 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Description: Example of .env file | ||
AWS_REGION="" | ||
S3_BUCKET_NAME_INIT="" | ||
DYNAMODB_TABLE_NAME_INIT="" | ||
STATE_PATH_INIT="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
|
||
delete_all_secrets() { | ||
aws secretsmanager list-secrets --include-planned-deletion --query 'SecretList[].Name' --output text | tr '\t' '\n' | while IFS=' ' read -r secret_name; do | ||
aws secretsmanager delete-secret --secret-id "$secret_name" --force-delete-without-recovery | ||
done | ||
} | ||
|
||
delete_secrets_by_tag() { | ||
tag_name="$1" | ||
aws secretsmanager list-secrets --include-planned-deletion --query 'SecretList[?Tags[?Key==`'"$tag_name"'`]].Name' --output text | tr '\t' '\n' | while IFS=' ' read -r secret_name; do | ||
aws secretsmanager delete-secret --secret-id "$secret_name" --force-delete-without-recovery | ||
done | ||
} | ||
|
||
echo "Select an option:" | ||
echo "1. Delete all secrets" | ||
echo "2. Delete secrets by tag key" | ||
|
||
read -p "Enter your choice: " choice | ||
|
||
case $choice in | ||
1) | ||
delete_all_secrets | ||
;; | ||
2) | ||
read -p "Enter the tag name: " tag_name | ||
delete_secrets_by_tag "$tag_name" | ||
;; | ||
*) | ||
echo "Invalid choice" | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
|
||
backend "s3" {} | ||
} | ||
|
||
module "infra" { | ||
source = "../shared" | ||
stage = "dev" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ terraform { | |
version = "~> 5.0" | ||
} | ||
} | ||
|
||
backend "s3" {} | ||
} | ||
|
||
provider "aws" { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
#!/bin/bash | ||
|
||
# Check if terraform is installed | ||
if ! command -v terraform &> /dev/null | ||
then | ||
echo "Terraform could not be found. Please install Terraform to proceed." | ||
exit | ||
fi | ||
|
||
# Define the base directory where init-state, dev, and prod directories are located | ||
BASE_DIR="$(dirname $(realpath $0))" | ||
|
||
source .env | ||
|
||
# Function to check if infrastructure is initialized and get outputs | ||
# Arguments: | ||
# $1: The name of the directory to check | ||
check_infra() { | ||
if [ -d "$BASE_DIR/$1" ]; then | ||
cd "$BASE_DIR/$1" | ||
if terraform state list > /dev/null 2>&1; then | ||
echo "The infrastructure in the $1 directory is initialized." | ||
if [ "$1" = "init-state" ]; then | ||
export S3_BUCKET_NAME=$(terraform output -raw s3_bucket_name) | ||
export DYNAMODB_TABLE_NAME=$(terraform output -raw dynamodb_table_name) | ||
fi | ||
return 0 | ||
else | ||
echo "The infrastructure in the $1 directory is NOT initialized." | ||
return 1 | ||
fi | ||
else | ||
echo "The directory $1 does not exist. Please create it and try again." | ||
return 2 | ||
fi | ||
} | ||
|
||
# Function to initialize infrastructure | ||
# Arguments: | ||
# $1: The name of the directory to initialize | ||
# $2: The path to the Terraform state file | ||
# $3: The name of the S3 bucket | ||
# $4: The name of the DynamoDB table | ||
init_infra() { | ||
cd $1 | ||
echo "Initializing infrastructure in the $1 directory..." | ||
terraform init \ | ||
-backend-config "key=$2" \ | ||
-backend-config="region=$AWS_REGION" \ | ||
-backend-config="bucket=$3" \ | ||
-backend-config="dynamodb_table=$4" | ||
echo "Infrastructure initialized successfully." | ||
} | ||
|
||
# Function to apply terraform configuration | ||
# Arguments: | ||
# $1: The name of the directory to apply the configuration to | ||
# $2: The name of the .tfvars file (without the .tfvars extension) | ||
apply_infra() { | ||
cd $1 | ||
echo "Applying Terraform configuration in the $1 directory..." | ||
if [ -f "$2.tfvars" ]; then | ||
terraform apply -var-file="$2.tfvars" | ||
else | ||
terraform apply | ||
fi | ||
echo "Terraform configuration applied successfully." | ||
} | ||
|
||
# Function to destroy terraform infrastructure | ||
# Arguments: | ||
# $1: The name of the directory to destroy the infrastructure in | ||
destroy_infra() { | ||
cd $1 | ||
echo "Destroying infrastructure in the $1 directory..." | ||
terraform destroy | ||
echo "Infrastructure destroyed successfully." | ||
} | ||
|
||
# Check the initialization status of each environment | ||
echo "Checking initialization status of each environment..." | ||
echo | ||
check_infra init-state | ||
check_infra dev & | ||
check_infra prod & | ||
wait | ||
echo | ||
|
||
# Function to manage environment | ||
# Arguments: | ||
# $1: The name of the environment to manage | ||
# $2: The path to the Terraform state file | ||
# $3: The name of the S3 bucket | ||
# $4: The name of the DynamoDB table | ||
manage_env() { | ||
echo "Managing $1 environment..." | ||
echo "Select an action: " | ||
echo "1. init - Initialize the infrastructure" | ||
echo "2. apply - Apply the Terraform configuration" | ||
echo "3. destroy - Destroy the infrastructure" | ||
read action | ||
echo | ||
case $action in | ||
1) | ||
init_infra $1 $2 $3 $4 | ||
;; | ||
2) | ||
apply_infra $1 $1 | ||
;; | ||
3) | ||
destroy_infra $1 | ||
;; | ||
*) | ||
echo "Invalid action. Please select either '1', '2', or '3'." | ||
;; | ||
esac | ||
echo | ||
} | ||
|
||
# Manage environments | ||
while true; do | ||
cd $BASE_DIR | ||
echo "Select environment to manage: " | ||
echo "1. init-state - Initialize the state" | ||
echo "2. dev - Manage the development environment" | ||
echo "3. prod - Manage the production environment" | ||
echo "4. exit - Exit the script" | ||
read env | ||
echo | ||
|
||
case $env in | ||
1) | ||
manage_env init-state $STATE_PATH_INIT $S3_BUCKET_NAME_INIT $DYNAMODB_TABLE_NAME_INIT | ||
;; | ||
2) | ||
manage_env dev "env/dev/terraform.tfstate" $S3_BUCKET_NAME $DYNAMODB_TABLE_NAME | ||
;; | ||
3) | ||
manage_env prod "env/prod/terraform.tfstate" $S3_BUCKET_NAME $DYNAMODB_TABLE_NAME | ||
;; | ||
4) | ||
echo "Exiting the script. Goodbye!" | ||
break | ||
;; | ||
*) | ||
echo "Invalid environment. Please select either '1', '2', '3', or '4'." | ||
;; | ||
esac | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "~> 5.0" | ||
} | ||
} | ||
|
||
backend "s3" {} | ||
} | ||
|
||
module "infra" { | ||
source = "../shared" | ||
stage = "prod" | ||
} |
9 changes: 0 additions & 9 deletions
9
...orm/environments/shared-resources/main.tf → terraform/shared/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.