Skip to content

Instantly share code, notes, and snippets.

@mdfranz
Created July 6, 2024 18:39
Show Gist options
  • Save mdfranz/33f3f18488bfcbce416f66321f13d8f2 to your computer and use it in GitHub Desktop.
Save mdfranz/33f3f18488bfcbce416f66321f13d8f2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to check if AWS CLI tools are available
function check_aws_cli() {
if ! which aws &> /dev/null; then
echo "Error: AWS CLI not found. Please install AWS CLI and configure credentials."
exit 1
fi
}
# Function to create a CloudFormation stack
function create_stack() {
local stack_name="$1"
local template_file="$2"
local parameters_file="$3"
check_aws_cli
aws cloudformation create-stack \
--stack-name "$stack_name" \
--template-body "file://$template_file" \
(test -f "$parameters_file" && echo "--parameters file://$parameters_file")
echo "Stack '$stack_name' creation initiated."
}
# Function to update a CloudFormation stack
function update_stack() {
local stack_name="$1"
local template_file="$2"
local parameters_file="$3"
check_aws_cli
aws cloudformation update-stack \
--stack-name "$stack_name" \
(test -f "$template_file" && echo "--template-body file://$template_file") \
(test -f "$parameters_file" && echo "--parameters file://$parameters_file")
echo "Stack '$stack_name' update initiated."
}
# Function to delete a CloudFormation stack
function delete_stack() {
local stack_name="$1"
check_aws_cli
aws cloudformation delete-stack --stack-name "$stack_name"
echo "Stack '$stack_name' deletion initiated."
}
# Usage message
if [ $# -lt 2 ]; then
echo "Usage: $0 {create|update|delete} <stack_name> [template_file] [parameters_file]"
echo " create: Create a new stack."
echo " update: Update an existing stack."
echo " delete: Delete an existing stack."
echo " stack_name: The name of the CloudFormation stack."
echo " template_file (optional): Path to the CloudFormation template file (YAML format)."
echo " parameters_file (optional): Path to a file containing stack parameters (JSON format)."
exit 1
fi
# Call the appropriate function based on the first argument
case $1 in
create)
shift
create_stack "$@"
;;
update)
shift
update_stack "$@"
;;
delete)
shift
delete_stack "$@"
;;
*)
echo "Invalid command: '$1'"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment