-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·67 lines (52 loc) · 1.6 KB
/
setup.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
#!/bin/bash
set -eu
export AWS_PAGER=""
# Ensure we have AWS Creds
if aws sts get-caller-identity &> /dev/null; then
user=$(aws sts get-caller-identity | jq -r '.Arn' )
account=$(aws sts get-caller-identity | jq -r '.Account')
echo "Setting up Golang API in AWS Account: $account using user: $user"
else
echo "AWS credentials not found, running aws configure"
aws configure
fi
## Run Terraform Commands
cd infra
# Init
if terraform init &> /dev/null; then
echo "Initializing Terraform..."
else
echo "Terraform Init failed...please try again"
exit 1
fi
# Apply
terraform apply
## Deploy Application
# Update Kubeconfig to the newly created Cluster
echo "Retrieving cluster Kubeconfig..."
aws eks update-kubeconfig --region us-east-1 --name golang-api-demo &> /dev/null
# Helm install the application
echo "Installing golang-api to cluster..."
cd ../charts/golang-api
helm upgrade --install golang-api .
# Sleep to make sure LB is created
sleep 10
# Get the URL for the LB Service
URL=$(kubectl get services -n default golang-api --output jsonpath='{.status.loadBalancer.ingress[0].hostname}')
# Hit the LB Service until it's up
attempt_counter=0
max_attempts=20
until $(curl --output /dev/null --silent --head --fail $URL); do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "LB failed"
exit 1
fi
printf '.'
attempt_counter=$(($attempt_counter+1))
sleep 10
done
# Provide the URL for the user to go to
status_code=$(curl --write-out %{http_code} --silent --output /dev/null $URL)
if [[ "$status_code" == "200" ]]; then
echo "API is running at $URL"
fi