-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-aws.sh
More file actions
executable file
·1286 lines (1106 loc) · 41.6 KB
/
Copy pathinstall-aws.sh
File metadata and controls
executable file
·1286 lines (1106 loc) · 41.6 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Script Name: install-aws.sh
# Description: Launches EC2 instance and installs Appmotel on it
# Usage: bash install-aws.sh [instance-type] [region]
# -----------------------------------------------------------------------------
set -o errexit
set -o nounset
set -o pipefail
IFS=$'\n\t'
# -----------------------------------------------------------------------------
# Constants
# -----------------------------------------------------------------------------
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly DEFAULT_INSTANCE_TYPE="t4g.micro" # ARM-based instance
readonly DEFAULT_REGION="us-west-2"
readonly DEFAULT_AMI_OWNER="137112412989" # Amazon
readonly KEY_NAME="appmotel-key"
readonly SECURITY_GROUP_NAME="appmotel-sg"
readonly INSTANCE_NAME="appmotel-server"
readonly SSH_USER="ec2-user" # Default user for Amazon Linux 2023
readonly IAM_ROLE_NAME="appmotel-route53-role"
readonly IAM_INSTANCE_PROFILE="appmotel-instance-profile"
# -----------------------------------------------------------------------------
# Global Variables (set via command line options)
# -----------------------------------------------------------------------------
AWS_PROFILE_EC2=""
AWS_PROFILE_IAM=""
REQUESTED_HOSTED_ZONE=""
FORCE_OVERWRITE=0
# -----------------------------------------------------------------------------
# AWS CLI Profile Wrapper Functions
# -----------------------------------------------------------------------------
aws_ec2() {
if [[ -n "${AWS_PROFILE_EC2}" ]]; then
aws ec2 --profile="${AWS_PROFILE_EC2}" "$@"
else
aws ec2 "$@"
fi
}
aws_iam() {
if [[ -n "${AWS_PROFILE_IAM}" ]]; then
aws iam --profile="${AWS_PROFILE_IAM}" "$@"
else
aws iam "$@"
fi
}
aws_route53() {
if [[ -n "${AWS_PROFILE_IAM}" ]]; then
aws route53 --profile="${AWS_PROFILE_IAM}" "$@"
else
aws route53 "$@"
fi
}
aws_sts() {
if [[ -n "${AWS_PROFILE_EC2}" ]]; then
aws sts --profile="${AWS_PROFILE_EC2}" "$@"
else
aws sts "$@"
fi
}
# -----------------------------------------------------------------------------
# Function: log_msg
# Description: Prints messages with timestamp
# -----------------------------------------------------------------------------
log_msg() {
local level="$1"
local msg="$2"
printf "[%(%Y-%m-%d %H:%M:%S)T] [%s] %s\n" -1 "${level}" "${msg}" >&2
}
# -----------------------------------------------------------------------------
# Function: die
# Description: Prints error message and exits
# -----------------------------------------------------------------------------
die() {
log_msg "ERROR" "$1"
exit 1
}
# -----------------------------------------------------------------------------
# Function: die_iam_permission_error
# Description: Prints IAM permission error with helpful guidance
# Arguments:
# $1 - IAM action that failed
# -----------------------------------------------------------------------------
die_iam_permission_error() {
local iam_action="$1"
log_msg "ERROR" "Access Denied: IAM permission required"
log_msg "ERROR" ""
log_msg "ERROR" "The current AWS credentials do not have permission for: ${iam_action}"
log_msg "ERROR" ""
log_msg "ERROR" "To fix this issue, you have two options:"
log_msg "ERROR" ""
log_msg "ERROR" "Option 1: Use an IAM profile with higher privileges"
log_msg "ERROR" " Run with --iam-profile using a profile that has IAM permissions:"
log_msg "ERROR" " bash install-aws.sh --iam-profile <ADMIN_PROFILE>"
log_msg "ERROR" ""
log_msg "ERROR" "Option 2: Pre-create the IAM role manually"
log_msg "ERROR" " Ask your AWS administrator to create:"
log_msg "ERROR" " - Role: ${IAM_ROLE_NAME}"
log_msg "ERROR" " - Instance Profile: ${IAM_INSTANCE_PROFILE}"
log_msg "ERROR" " - Policy: AmazonRoute53FullAccess"
log_msg "ERROR" ""
if [[ -n "${AWS_PROFILE_IAM}" ]]; then
log_msg "ERROR" "Current IAM Profile: ${AWS_PROFILE_IAM}"
else
log_msg "ERROR" "Current IAM Profile: default"
fi
exit 1
}
# -----------------------------------------------------------------------------
# Function: profile_exists
# Description: Checks if an AWS profile exists in ~/.aws/config
# Arguments:
# $1 - Profile name
# Returns: 0 if exists, 1 if not
# -----------------------------------------------------------------------------
profile_exists() {
local profile="$1"
if [[ -z "${profile}" ]]; then
return 0
fi
# Check both [profile name] and [name] formats in ~/.aws/config
if grep -q "^\[profile ${profile}\]" ~/.aws/config 2>/dev/null || \
grep -q "^\[${profile}\]" ~/.aws/config 2>/dev/null || \
grep -q "^\[${profile}\]" ~/.aws/credentials 2>/dev/null; then
return 0
fi
return 1
}
# -----------------------------------------------------------------------------
# Function: check_dependencies
# Description: Verifies required tools are installed
# -----------------------------------------------------------------------------
check_dependencies() {
local deps=(aws jq ssh ssh-keygen)
local missing=()
for cmd in "${deps[@]}"; do
if ! command -v "${cmd}" >/dev/null 2>&1; then
missing+=("${cmd}")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
die "Missing required commands: ${missing[*]}. Please install them first."
fi
# Validate AWS profiles if specified
if [[ -n "${AWS_PROFILE_EC2}" ]]; then
if ! profile_exists "${AWS_PROFILE_EC2}"; then
die "EC2 profile '${AWS_PROFILE_EC2}' not found in ~/.aws/config or ~/.aws/credentials"
fi
log_msg "INFO" "Using AWS profile for EC2 operations: ${AWS_PROFILE_EC2}"
fi
if [[ -n "${AWS_PROFILE_IAM}" ]]; then
if ! profile_exists "${AWS_PROFILE_IAM}"; then
die "IAM profile '${AWS_PROFILE_IAM}' not found in ~/.aws/config or ~/.aws/credentials"
fi
log_msg "INFO" "Using AWS profile for IAM/Route53 operations: ${AWS_PROFILE_IAM}"
fi
# Check AWS CLI is configured
if ! aws_sts get-caller-identity >/dev/null 2>&1; then
die "AWS CLI is not configured. Run 'aws configure' first."
fi
log_msg "INFO" "All dependencies satisfied"
}
# -----------------------------------------------------------------------------
# Function: get_latest_al2023_ami
# Description: Finds the latest Amazon Linux 2023 AMI for the specified architecture
# Arguments:
# $1 - Region
# $2 - Architecture (arm64 or x86_64)
# Returns: AMI ID
# -----------------------------------------------------------------------------
get_latest_al2023_ami() {
local region="$1"
local arch="$2"
log_msg "INFO" "Finding latest Amazon Linux 2023 ${arch} AMI in ${region}..."
local ami_id
ami_id=$(aws_ec2 describe-images \
--region "${region}" \
--owners "${DEFAULT_AMI_OWNER}" \
--filters \
"Name=name,Values=al2023-ami-2023.*-${arch}" \
"Name=state,Values=available" \
"Name=architecture,Values=${arch}" \
--query 'sort_by(Images, &CreationDate)[-1].ImageId' \
--output text)
if [[ -z "${ami_id}" ]] || [[ "${ami_id}" == "None" ]]; then
die "Could not find Amazon Linux 2023 ${arch} AMI in ${region}"
fi
log_msg "INFO" "Found AMI: ${ami_id}"
echo "${ami_id}"
}
# -----------------------------------------------------------------------------
# Function: create_key_pair
# Description: Creates SSH key pair if it doesn't exist
# Arguments:
# $1 - Region
# -----------------------------------------------------------------------------
create_key_pair() {
local region="$1"
local key_file="${HOME}/.ssh/${KEY_NAME}.pem"
# Check if key already exists in AWS
if aws_ec2 describe-key-pairs --region "${region}" --key-names "${KEY_NAME}" >/dev/null 2>&1; then
log_msg "INFO" "Key pair '${KEY_NAME}' already exists in AWS"
# Verify local key file exists
if [[ ! -f "${key_file}" ]]; then
die "Key pair exists in AWS but local file ${key_file} not found. Delete AWS key and re-run."
fi
return
fi
log_msg "INFO" "Creating key pair '${KEY_NAME}'..."
# Create key pair and save to file
aws_ec2 create-key-pair \
--region "${region}" \
--key-name "${KEY_NAME}" \
--query 'KeyMaterial' \
--output text > "${key_file}"
chmod 600 "${key_file}"
log_msg "INFO" "Key pair created and saved to ${key_file}"
}
# -----------------------------------------------------------------------------
# Function: add_security_group_rules
# Description: Adds ingress rules to security group
# Arguments:
# $1 - Region
# $2 - Security group ID
# -----------------------------------------------------------------------------
add_security_group_rules() {
local region="$1"
local sg_id="$2"
log_msg "INFO" "Adding ingress rules to security group..."
# SSH (22)
aws_ec2 authorize-security-group-ingress \
--region "${region}" \
--group-id "${sg_id}" \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0 >/dev/null 2>&1 || log_msg "WARN" "SSH rule may already exist"
# HTTP (80)
aws_ec2 authorize-security-group-ingress \
--region "${region}" \
--group-id "${sg_id}" \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0 >/dev/null 2>&1 || log_msg "WARN" "HTTP rule may already exist"
# HTTPS (443)
aws_ec2 authorize-security-group-ingress \
--region "${region}" \
--group-id "${sg_id}" \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0 >/dev/null 2>&1 || log_msg "WARN" "HTTPS rule may already exist"
log_msg "INFO" "Ingress rules added successfully"
}
# -----------------------------------------------------------------------------
# Function: create_security_group
# Description: Creates security group with required ports if it doesn't exist
# Arguments:
# $1 - Region
# Returns: Security group ID
# -----------------------------------------------------------------------------
create_security_group() {
local region="$1"
local sg_id
# Check if security group already exists
sg_id=$(aws_ec2 describe-security-groups \
--region "${region}" \
--filters "Name=group-name,Values=${SECURITY_GROUP_NAME}" \
--query 'SecurityGroups[0].GroupId' \
--output text 2>/dev/null || echo "")
if [[ -n "${sg_id}" ]] && [[ "${sg_id}" != "None" ]]; then
log_msg "INFO" "Security group '${SECURITY_GROUP_NAME}' already exists: ${sg_id}"
# Check if ingress rules exist
local rule_count
rule_count=$(aws_ec2 describe-security-groups \
--region "${region}" \
--group-ids "${sg_id}" \
--query 'length(SecurityGroups[0].IpPermissions)' \
--output text)
if [[ "${rule_count}" == "0" ]]; then
log_msg "WARN" "Security group has no ingress rules, adding them now..."
add_security_group_rules "${region}" "${sg_id}"
else
log_msg "INFO" "Security group has ${rule_count} ingress rule(s)"
fi
echo "${sg_id}"
return
fi
log_msg "INFO" "Creating security group '${SECURITY_GROUP_NAME}'..."
# Get default VPC
local vpc_id
vpc_id=$(aws_ec2 describe-vpcs \
--region "${region}" \
--filters "Name=is-default,Values=true" \
--query 'Vpcs[0].VpcId' \
--output text)
if [[ -z "${vpc_id}" ]] || [[ "${vpc_id}" == "None" ]]; then
die "No default VPC found in ${region}"
fi
# Create security group
sg_id=$(aws_ec2 create-security-group \
--region "${region}" \
--group-name "${SECURITY_GROUP_NAME}" \
--description "Security group for Appmotel PaaS" \
--vpc-id "${vpc_id}" \
--query 'GroupId' \
--output text)
log_msg "INFO" "Security group created: ${sg_id}"
# Add inbound rules
add_security_group_rules "${region}" "${sg_id}"
echo "${sg_id}"
}
# -----------------------------------------------------------------------------
# Function: launch_instance
# Description: Launches EC2 instance
# Arguments:
# $1 - Region
# $2 - Instance type
# $3 - AMI ID
# $4 - Security group ID
# Returns: Instance ID
# -----------------------------------------------------------------------------
launch_instance() {
local region="$1"
local instance_type="$2"
local ami_id="$3"
local sg_id="$4"
log_msg "INFO" "Launching EC2 instance (${instance_type})..."
local instance_id
instance_id=$(aws_ec2 run-instances \
--region "${region}" \
--image-id "${ami_id}" \
--instance-type "${instance_type}" \
--key-name "${KEY_NAME}" \
--security-group-ids "${sg_id}" \
--iam-instance-profile "Name=${IAM_INSTANCE_PROFILE}" \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${INSTANCE_NAME}}]" \
--query 'Instances[0].InstanceId' \
--output text)
log_msg "INFO" "Instance launched: ${instance_id}"
log_msg "INFO" "Waiting for instance to be running..."
aws_ec2 wait instance-running \
--region "${region}" \
--instance-ids "${instance_id}"
log_msg "INFO" "Instance is running"
echo "${instance_id}"
}
# -----------------------------------------------------------------------------
# Function: get_instance_ip
# Description: Gets public IP address of instance
# Arguments:
# $1 - Region
# $2 - Instance ID
# Returns: Public IP address
# -----------------------------------------------------------------------------
get_instance_ip() {
local region="$1"
local instance_id="$2"
local public_ip
public_ip=$(aws_ec2 describe-instances \
--region "${region}" \
--instance-ids "${instance_id}" \
--query 'Reservations[0].Instances[0].PublicIpAddress' \
--output text)
if [[ -z "${public_ip}" ]] || [[ "${public_ip}" == "None" ]]; then
die "Could not get public IP for instance ${instance_id}"
fi
echo "${public_ip}"
}
# -----------------------------------------------------------------------------
# Function: wait_for_ssh
# Description: Waits for SSH to be accessible
# Arguments:
# $1 - Host IP
# $2 - Key file path
# -----------------------------------------------------------------------------
wait_for_ssh() {
local host="$1"
local key_file="$2"
local max_attempts=40
local attempt=0
log_msg "INFO" "Checking SSH accessibility..."
while [[ ${attempt} -lt ${max_attempts} ]]; do
if ssh -i "${key_file}" \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
-o ConnectTimeout=3 \
-o BatchMode=yes \
"${SSH_USER}@${host}" \
"echo 'SSH ready'" >/dev/null 2>&1; then
log_msg "INFO" "SSH is ready (attempt ${attempt}/${max_attempts})"
return 0
fi
attempt=$((attempt + 1))
if [[ ${attempt} -lt ${max_attempts} ]]; then
sleep 3
fi
done
die "SSH did not become accessible after ${max_attempts} attempts (2 minutes)"
}
# -----------------------------------------------------------------------------
# Function: create_iam_role
# Description: Creates IAM role for EC2 to access Route53
# Returns: Success (0) or failure (1)
# -----------------------------------------------------------------------------
create_iam_role() {
log_msg "INFO" "Setting up IAM role for Route53 access..."
# Check if role already exists
local role_check_output
role_check_output=$(aws_iam get-role --role-name "${IAM_ROLE_NAME}" 2>&1) && {
log_msg "INFO" "IAM role '${IAM_ROLE_NAME}' already exists"
} || {
# Check for access denied error
if echo "${role_check_output}" | grep -q "AccessDenied"; then
die_iam_permission_error "iam:GetRole"
fi
log_msg "INFO" "Creating IAM role - this may take a few seconds..."
# Trust policy - allows EC2 to assume this role
local trust_policy
trust_policy=$(cat <<'EOF'
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}]
}
EOF
)
# Create the role
local create_output
create_output=$(aws_iam create-role \
--role-name "${IAM_ROLE_NAME}" \
--assume-role-policy-document "${trust_policy}" \
--description "Allows Appmotel/Traefik to manage Route53 for Let's Encrypt DNS-01 challenge" \
2>&1) || {
if echo "${create_output}" | grep -q "AccessDenied"; then
die_iam_permission_error "iam:CreateRole"
fi
die "Failed to create IAM role: ${create_output}"
}
log_msg "INFO" "IAM role created"
}
# Check if policy is attached
if aws_iam list-attached-role-policies --role-name "${IAM_ROLE_NAME}" 2>/dev/null | grep -q "Route53FullAccess"; then
log_msg "INFO" "Route53 policy already attached"
else
log_msg "INFO" "Attaching Route53 policy to role..."
# Attach AWS managed Route53 policy
local attach_output
attach_output=$(aws_iam attach-role-policy \
--role-name "${IAM_ROLE_NAME}" \
--policy-arn "arn:aws:iam::aws:policy/AmazonRoute53FullAccess" \
2>&1) || {
if echo "${attach_output}" | grep -q "AccessDenied"; then
die_iam_permission_error "iam:AttachRolePolicy"
fi
die "Failed to attach policy: ${attach_output}"
}
log_msg "INFO" "Route53 policy attached"
fi
# Create instance profile if it doesn't exist
if aws_iam get-instance-profile --instance-profile-name "${IAM_INSTANCE_PROFILE}" >/dev/null 2>&1; then
log_msg "INFO" "Instance profile '${IAM_INSTANCE_PROFILE}' already exists"
else
log_msg "INFO" "Creating instance profile..."
local profile_output
profile_output=$(aws_iam create-instance-profile \
--instance-profile-name "${IAM_INSTANCE_PROFILE}" \
2>&1) || {
if echo "${profile_output}" | grep -q "AccessDenied"; then
die_iam_permission_error "iam:CreateInstanceProfile"
fi
die "Failed to create instance profile: ${profile_output}"
}
# Add role to instance profile
aws_iam add-role-to-instance-profile \
--instance-profile-name "${IAM_INSTANCE_PROFILE}" \
--role-name "${IAM_ROLE_NAME}" || die "Failed to add role to instance profile"
log_msg "INFO" "Instance profile created"
# Wait for instance profile to be ready
log_msg "INFO" "Waiting for IAM resources to propagate - 10 seconds..."
sleep 10
fi
return 0
}
# -----------------------------------------------------------------------------
# Function: list_all_route53_zones
# Description: Lists all Route53 hosted zones for user selection
# -----------------------------------------------------------------------------
list_all_route53_zones() {
log_msg "INFO" ""
log_msg "INFO" "Available Route53 hosted zones:"
log_msg "INFO" "================================"
local zones
zones=$(aws_route53 list-hosted-zones \
--query 'HostedZones[].[Id,Name]' \
--output text 2>/dev/null)
if [[ -z "${zones}" ]]; then
log_msg "INFO" " No zones found"
return
fi
while IFS=$'\t' read -r zone_id zone_name; do
local clean_id clean_name
clean_id="${zone_id#/hostedzone/}"
clean_name="${zone_name%.}"
log_msg "INFO" " --hosted-zone ${clean_id} # ${clean_name}"
done <<< "${zones}"
log_msg "INFO" ""
}
# -----------------------------------------------------------------------------
# Function: get_route53_hosted_zone
# Description: Gets hosted zone from Route53
# Arguments:
# $1 - Region
# $2 - Requested zone ID (optional, from --hosted-zone)
# Returns: Hosted zone ID and domain name (format: "ID DOMAIN")
# -----------------------------------------------------------------------------
get_route53_hosted_zone() {
local region="$1"
local requested_zone_id="${2-}"
log_msg "INFO" "Looking for Route53 hosted zones..."
# Get all hosted zones
local all_zones zone_count
all_zones=$(aws_route53 list-hosted-zones \
--query 'HostedZones[].[Id,Name]' \
--output text 2>/dev/null)
if [[ -z "${all_zones}" ]] || [[ "${all_zones}" == "None" ]]; then
log_msg "WARN" "No Route53 hosted zones found"
return 1
fi
zone_count=$(echo "${all_zones}" | wc -l)
# If a specific zone was requested, validate and use it
if [[ -n "${requested_zone_id}" ]]; then
local found_zone=""
while IFS=$'\t' read -r zone_id zone_name; do
local clean_id="${zone_id#/hostedzone/}"
if [[ "${clean_id}" == "${requested_zone_id}" ]]; then
local clean_name="${zone_name%.}"
found_zone="${clean_id} ${clean_name}"
break
fi
done <<< "${all_zones}"
if [[ -z "${found_zone}" ]]; then
log_msg "ERROR" "Hosted zone '${requested_zone_id}' not found"
list_all_route53_zones
die "Please use one of the zone IDs listed above with --hosted-zone"
fi
log_msg "INFO" "Using requested hosted zone: ${found_zone}"
echo "${found_zone}"
return 0
fi
# If only one zone exists, use it automatically
if [[ "${zone_count}" -eq 1 ]]; then
local zone_id zone_name
zone_id=$(echo "${all_zones}" | awk '{print $1}' | sed 's|/hostedzone/||')
zone_name=$(echo "${all_zones}" | awk '{print $2}' | sed 's/\.$//')
log_msg "INFO" "Found hosted zone: ${zone_name} - ${zone_id}"
echo "${zone_id} ${zone_name}"
return 0
fi
# Multiple zones found - require user to select one
log_msg "ERROR" "Multiple Route53 hosted zones found"
list_all_route53_zones
die "Please specify which zone to use with --hosted-zone <ZONE_ID>"
}
# -----------------------------------------------------------------------------
# Function: check_existing_appmotel_records
# Description: Checks if appmotel DNS records already exist in the zone
# Arguments:
# $1 - Hosted zone ID
# $2 - Domain name
# Returns: 0 if records exist, 1 if not
# -----------------------------------------------------------------------------
check_existing_appmotel_records() {
local zone_id="$1"
local domain="$2"
log_msg "INFO" "Checking for existing appmotel DNS records..."
# Route53 returns wildcard names octal-escaped (* -> \052), so the
# wildcard comparison must use the escaped form or it never matches and
# an existing production wildcard record would be silently UPSERTed over.
local records
records=$(aws_route53 list-resource-record-sets \
--hosted-zone-id "${zone_id}" \
--query "ResourceRecordSets[?Name=='appmotel.${domain}.' || Name=='*.${domain}.' || Name=='\\052.${domain}.'].Name" \
--output text 2>/dev/null)
if [[ -n "${records}" ]] && [[ "${records}" != "None" ]]; then
log_msg "WARN" ""
log_msg "WARN" "Existing appmotel DNS records found in zone ${zone_id}:"
for record in ${records}; do
log_msg "WARN" " - ${record}"
done
log_msg "WARN" ""
return 0 # Records exist
fi
return 1 # No records
}
# -----------------------------------------------------------------------------
# Function: validate_route53_early
# Description: Validates Route53 configuration before launching EC2
# Arguments:
# $1 - Region
# $2 - Requested zone ID (optional)
# Returns: zone_id and domain_name via global variables
# -----------------------------------------------------------------------------
VALIDATED_ZONE_ID=""
VALIDATED_DOMAIN=""
validate_route53_early() {
local region="$1"
local requested_zone_id="${2-}"
log_msg "INFO" "============================================"
log_msg "INFO" "Phase 1: Validating Route53 configuration"
log_msg "INFO" "============================================"
log_msg "INFO" "Looking for Route53 hosted zones..."
# Get all hosted zones
local all_zones zone_count
all_zones=$(aws_route53 list-hosted-zones \
--query 'HostedZones[].[Id,Name]' \
--output text 2>/dev/null)
if [[ -z "${all_zones}" ]] || [[ "${all_zones}" == "None" ]]; then
log_msg "INFO" "No Route53 hosted zones found - skipping DNS configuration"
return 1
fi
zone_count=$(echo "${all_zones}" | wc -l)
# If a specific zone was requested, validate it
if [[ -n "${requested_zone_id}" ]]; then
local found_zone=""
while IFS=$'\t' read -r zone_id zone_name; do
local clean_id="${zone_id#/hostedzone/}"
if [[ "${clean_id}" == "${requested_zone_id}" ]]; then
local clean_name="${zone_name%.}"
VALIDATED_ZONE_ID="${clean_id}"
VALIDATED_DOMAIN="${clean_name}"
found_zone="yes"
break
fi
done <<< "${all_zones}"
if [[ -z "${found_zone}" ]]; then
log_msg "ERROR" "Hosted zone '${requested_zone_id}' not found"
list_all_route53_zones
die "Please use one of the zone IDs listed above with --hosted-zone"
fi
log_msg "INFO" "Using requested hosted zone: ${VALIDATED_ZONE_ID} ${VALIDATED_DOMAIN}"
# If only one zone exists, use it automatically
elif [[ "${zone_count}" -eq 1 ]]; then
VALIDATED_ZONE_ID=$(echo "${all_zones}" | awk '{print $1}' | sed 's|/hostedzone/||')
VALIDATED_DOMAIN=$(echo "${all_zones}" | awk '{print $2}' | sed 's/\.$//')
log_msg "INFO" "Found hosted zone: ${VALIDATED_DOMAIN} - ${VALIDATED_ZONE_ID}"
# Multiple zones found - require user to select one (STOP HERE!)
else
log_msg "ERROR" "Multiple Route53 hosted zones found"
list_all_route53_zones
die "Please specify which zone to use with --hosted-zone <ZONE_ID>"
fi
# Check for existing records
if check_existing_appmotel_records "${VALIDATED_ZONE_ID}" "${VALIDATED_DOMAIN}"; then
if [[ "${FORCE_OVERWRITE}" -eq 1 ]]; then
log_msg "INFO" "Existing records will be overwritten due to --force flag"
else
log_msg "ERROR" ""
log_msg "ERROR" "DNS records for appmotel already exist in this zone."
log_msg "ERROR" ""
log_msg "ERROR" "To resolve this, you have two options:"
log_msg "ERROR" ""
log_msg "ERROR" "Option 1: Use --force to overwrite existing records"
log_msg "ERROR" " bash install-aws.sh --force"
log_msg "ERROR" ""
log_msg "ERROR" "Option 2: Manually remove the existing records from Route53"
log_msg "ERROR" " - appmotel.${VALIDATED_DOMAIN}"
log_msg "ERROR" " - *.${VALIDATED_DOMAIN}"
log_msg "ERROR" ""
die "Aborting to prevent DNS conflicts"
fi
fi
log_msg "INFO" "Route53 validation passed"
log_msg "INFO" " Zone ID: ${VALIDATED_ZONE_ID}"
log_msg "INFO" " Domain: ${VALIDATED_DOMAIN}"
return 0
}
# -----------------------------------------------------------------------------
# Function: configure_route53_dns
# Description: Configures DNS records in Route53 for appmotel
# Arguments:
# $1 - Hosted zone ID
# $2 - Domain name
# $3 - Server IP address
# -----------------------------------------------------------------------------
configure_route53_dns() {
local zone_id="$1"
local domain="$2"
local server_ip="$3"
log_msg "INFO" "Configuring Route53 DNS records..."
# Create change batch JSON for both records
local change_batch
change_batch=$(cat <<EOF
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "appmotel.${domain}",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{"Value": "${server_ip}"}]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "*.${domain}",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{"Value": "${server_ip}"}]
}
}
]
}
EOF
)
# Apply changes
local change_id
change_id=$(aws_route53 change-resource-record-sets \
--hosted-zone-id "${zone_id}" \
--change-batch "${change_batch}" \
--query 'ChangeInfo.Id' \
--output text 2>/dev/null)
if [[ -z "${change_id}" ]] || [[ "${change_id}" == "None" ]]; then
log_msg "ERROR" "Failed to create DNS records"
return 1
fi
log_msg "INFO" "DNS records created:"
log_msg "INFO" " A record: appmotel.${domain} → ${server_ip}"
log_msg "INFO" " Wildcard: *.${domain} → ${server_ip}"
log_msg "INFO" "Change ID: ${change_id}"
return 0
}
# -----------------------------------------------------------------------------
# Function: configure_base_domain
# Description: Updates BASE_DOMAIN in remote .env file
# Arguments:
# $1 - Host IP
# $2 - Key file path
# $3 - Domain name
# -----------------------------------------------------------------------------
configure_base_domain() {
local host="$1"
local key_file="$2"
local domain="$3"
log_msg "INFO" "Configuring BASE_DOMAIN=${domain} on remote server..."
ssh -i "${key_file}" \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
"${SSH_USER}@${host}" \
"sudo sed -i 's|^BASE_DOMAIN=.*|BASE_DOMAIN=\"${domain}\"|' /home/appmotel/.config/appmotel/.env"
log_msg "INFO" "BASE_DOMAIN configured successfully"
}
# -----------------------------------------------------------------------------
# Function: configure_dns01_mode
# Description: Enables Let's Encrypt DNS-01 challenge mode with Route53
# Arguments:
# $1 - Host IP
# $2 - Key file path
# $3 - Hosted zone ID
# $4 - Region
# -----------------------------------------------------------------------------
configure_dns01_mode() {
local host="$1"
local key_file="$2"
local zone_id="$3"
local region="$4"
log_msg "INFO" "Configuring Let's Encrypt DNS-01 mode with Route53..."
ssh -i "${key_file}" \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
"${SSH_USER}@${host}" \
"sudo sed -i \
-e 's|^USE_LETSENCRYPT=.*|USE_LETSENCRYPT=\"yes\"|' \
-e 's|^LETSENCRYPT_MODE=.*|LETSENCRYPT_MODE=\"dns\"|' \
-e 's|^AWS_HOSTED_ZONE_ID=.*|AWS_HOSTED_ZONE_ID=\"${zone_id}\"|' \
-e 's|^AWS_REGION=.*|AWS_REGION=\"${region}\"|' \
/home/appmotel/.config/appmotel/.env"
log_msg "INFO" "DNS-01 mode configured (using IAM instance role for credentials)"
}
# -----------------------------------------------------------------------------
# Function: install_appmotel
# Description: Uploads and runs install.sh on remote instance
# Arguments:
# $1 - Host IP
# $2 - Key file path
# -----------------------------------------------------------------------------
install_appmotel() {
local host="$1"
local key_file="$2"
local install_script="${SCRIPT_DIR}/install.sh"
local env_default="${SCRIPT_DIR}/.env.default"
if [[ ! -f "${install_script}" ]]; then
die "install.sh not found at ${install_script}"
fi
log_msg "INFO" "Uploading install.sh to remote instance..."
scp -i "${key_file}" \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
"${install_script}" \
"${SSH_USER}@${host}:/tmp/install.sh"
# Upload .env.default if it exists
if [[ -f "${env_default}" ]]; then
log_msg "INFO" "Uploading .env.default..."
scp -i "${key_file}" \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
"${env_default}" \
"${SSH_USER}@${host}:/tmp/.env.default"
fi
log_msg "INFO" "Running system-level installation (as root)..."
ssh -i "${key_file}" \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR \
"${SSH_USER}@${host}" \
"sudo bash /tmp/install.sh"
log_msg "INFO" "Installation completed successfully!"
}
# -----------------------------------------------------------------------------
# Function: display_summary
# Description: Displays connection and next steps information
# Arguments:
# $1 - Host IP
# $2 - Key file path
# $3 - Instance ID
# $4 - Region
# $5 - Domain name (optional)
# -----------------------------------------------------------------------------
display_summary() {
local host="$1"
local key_file="$2"
local instance_id="$3"
local region="$4"
local domain="${5:-}"
log_msg "INFO" "======================================"
log_msg "INFO" "Appmotel EC2 Installation Complete!"
log_msg "INFO" "======================================"
log_msg "INFO" ""
log_msg "INFO" "Instance Details:"
log_msg "INFO" " Instance ID: ${instance_id}"
log_msg "INFO" " Region: ${region}"
log_msg "INFO" " Public IP: ${host}"
log_msg "INFO" " OS: Amazon Linux 2023"
if [[ -n "${domain}" ]]; then
log_msg "INFO" " Base Domain: ${domain}"
log_msg "INFO" ""
log_msg "INFO" "DNS Configuration:"
log_msg "INFO" " ✓ Route53 records configured automatically"
log_msg "INFO" " ✓ A record: appmotel.${domain} → ${host}"
log_msg "INFO" " ✓ Wildcard: *.${domain} → ${host}"
log_msg "INFO" " ✓ BASE_DOMAIN configured in ~/.config/appmotel/.env"
log_msg "INFO" ""