Created
February 13, 2023 20:59
-
-
Save mshafiee/edc9b18dd2c79eebf1781e7f2ef900ac to your computer and use it in GitHub Desktop.
This is a bash script that creates a CloudFormation stack for a public certificate with DNS validation, based on input parameters provided as command line flags.
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
#!/bin/bash | |
# This is a bash script that creates a CloudFormation stack for a public certificate with DNS validation, | |
# based on input parameters provided as command line flags. | |
# The script starts by defining the print_help function that displays the usage information for the script, | |
# along with the available command line flags. | |
# Next, the script uses the getopts command to parse the command line flags and store their values in variables | |
# (stack_name, csv_file, and domain_name). If any of the required flags are missing, the script will display | |
# an error message and exit. | |
# The script then reads the contents of the CSV file into a bash array (subject_alternative_names), which | |
# will be used to specify the SubjectAlternativeNames for the certificate. | |
# The script then converts the bash array into a string of comma-separated values (subject_alternative_names_string), | |
# which will be used to create the CloudFormation template file. The template file is created using the cat command, | |
# and includes the DomainName and SubjectAlternativeNames properties for the certificate. | |
# The script then uses the AWS CLI aws cloudformation create-stack command to create the CloudFormation stack, | |
# and waits for the stack creation to complete using the aws cloudformation wait stack-create-complete command. | |
# Finally, the script cleans up the CloudFormation template file using the rm command. | |
function print_help { | |
echo "Usage: $0 [-s stack_name] [-c csv_file] [-d domain_name]" | |
echo " -s stack_name Name of the CloudFormation stack to be created" | |
echo " -c csv_file Name of the CSV file containing the SubjectAlternativeNames" | |
echo " -d domain_name Domain name for the certificate" | |
exit 1 | |
} | |
# Get the command line flags | |
while getopts "s:c:d:" opt; do | |
case ${opt} in | |
s) | |
stack_name="$OPTARG" | |
;; | |
c) | |
csv_file="$OPTARG" | |
;; | |
d) | |
domain_name="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" | |
print_help | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." | |
print_help | |
;; | |
esac | |
done | |
# Check if the stack_name, csv_file, and domain_name flags were set | |
if [ -z "$stack_name" ] || [ -z "$csv_file" ] || [ -z "$domain_name" ]; then | |
echo "ERROR: Missing required flags" | |
print_help | |
fi | |
# Read the contents of the CSV file into a bash array | |
subject_alternative_names=() | |
while IFS=',' read -r line | |
do | |
subject_alternative_names+=("$line") | |
done < "$csv_file" | |
# Convert the bash array into a string of comma-separated values | |
subject_alternative_names_string="" | |
for i in "${subject_alternative_names[@]}"; do | |
subject_alternative_names_string="$subject_alternative_names_string'$i'," | |
done | |
# Remove the last comma | |
subject_alternative_names_string=${subject_alternative_names_string%?} | |
# Create the CloudFormation template file | |
template_file="template.yml" | |
cat > "$template_file" << EOL | |
--- | |
AWSTemplateFormatVersion: '2010-09-09' | |
Resources: | |
Certificate: | |
Type: AWS::CertificateManager::Certificate | |
Properties: | |
DomainName: $domain_name | |
SubjectAlternativeNames: [$subject_alternative_names_string] | |
ValidationMethod: DNS | |
EOL | |
cat $template_file | |
# Create the CloudFormation stack | |
aws cloudformation create-stack \ | |
--stack-name "$stack_name" \ | |
--template-body "file://$template_file" | |
# Wait for the stack to complete | |
aws cloudformation wait stack-create-complete \ | |
--stack-name "$stack_name" | |
# Clean up the CloudFormation template file | |
rm "$template_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment