-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·67 lines (52 loc) · 1.64 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
# to ensure the script terminate immediately it encounters an error
set -o pipefail
BLUE=`tput setaf 2`
GREEN=`tput setaf 4`
RESET=`tput sgr0`
# load the variables declared in the .env file into the shell
source .env
# a function to display a message of what action is going on
function display_message() {
echo " "
echo -e "${1} ## ${2} ## ${RESET}"
echo " "
}
# a function to display completion message when an action is complete
function display_success_message() {
echo " "
echo -e "${GREEN} *** ${1} *** ${RESET}"
echo " "
}
# function to build the AMI
function build_ami_image() {
display_message $BLUE "BUILDING IMAGES"
# packer command to start the building of the AMI
cd packer
packer build packer.json
# conditional statement to check if the command above failed
if [ $? -gt 0 ]; then
echo "Image building failed"
exit 1
else
display_success_message "IMAGES BUILD WAS SUCCESSFUL"
# function that handles creation of EC2 instances with Terraform
create_instance
fi
}
# function to create the instance after creating the AMI
function create_instance() {
display_message $BLUE "CREATING EC2 INSTANCES"
# this change directory to the terraform folder containing the terraform script
cd ../terraform
# terraform commands to initialize, make a plan and then create the EC2 instances
terraform init
terraform plan -out=tfplan -lock=false
terraform apply -auto-approve -lock=false
display_success_message "INSTANCES CREATION DONE"
}
# the script starting point
function main () {
build_ami_image
}
main