-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy_cdk.sh
173 lines (147 loc) · 4.38 KB
/
deploy_cdk.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
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
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
# Run the deploy script with your stream group identifier
#
# Usage: ./deploy_cdk.sh <stream-group-id>
# Example: ./deploy_cdk.sh sg-000000000
#
if [ $# -ne 1 ]; then
echo "Usage: $0 <stream-group-id>"
echo "Example: $0 sg-000000000"
exit 1
fi
# Validate StreamGroup ID format
if ! [[ $1 =~ ^sg-[a-zA-Z0-9]{9}$ ]]; then
echo "Error: Stream Group ID must match pattern sg-XXXXXXXXX"
echo "Where X is 9 alphanumeric characters"
echo "Example valid ID: sg-000000000"
exit 1
fi
# Store the StreamGroup ID
export STREAM_GROUP_ID="$1"
echo "Using Stream Group ID: $STREAM_GROUP_ID"
# Function to check minimum version requirements
check_node_version() {
local required_major=14
local node_version=$(node -v | cut -d 'v' -f2)
local major_version=${node_version%%.*}
if [[ $major_version -lt $required_major ]]; then
echo "Node.js version $required_major or higher is required (current: $node_version)"
return 1
fi
return 0
}
# Function to check AWS CLI version
check_aws_version() {
local aws_version=$(aws --version 2>&1 | cut -d/ -f2 | cut -d. -f1)
if [[ $aws_version -lt 2 ]]; then
echo "AWS CLI version 2 or higher is required (current: $aws_version)"
return 1
fi
return 0
}
# Get the directory where the script is located
SCRIPT_DIR=$(dirname "$(readlink -f "$0" 2>/dev/null || echo "$0")")
cd "$SCRIPT_DIR"
echo "Starting deployment process..."
# Check Node.js installation and version
if ! command -v node &> /dev/null; then
echo "Node.js is not installed"
echo "Please install Node.js from https://nodejs.org/"
read -p "Press Enter to continue..."
exit 1
fi
if ! check_node_version; then
read -p "Press Enter to continue..."
exit 1
fi
echo "Node.js version: $(node -v)"
# Check AWS CLI installation and version
if ! command -v aws &> /dev/null; then
echo "AWS CLI is not installed"
echo "Please install AWS CLI from https://aws.amazon.com/cli/"
read -p "Press Enter to continue..."
exit 1
fi
if ! check_aws_version; then
read -p "Press Enter to continue..."
exit 1
fi
echo "AWS CLI version: $(aws --version)"
# Simple AWS credential check
echo "Verifying AWS credentials..."
if ! aws sts get-caller-identity &> /dev/null; then
echo "AWS credentials verification failed"
echo "Please ensure you have valid AWS credentials configured"
read -p "Press Enter to continue..."
exit 1
fi
# Install project dependencies
echo "Installing project dependencies..."
if ! npm install; then
echo "Failed to install project dependencies"
read -p "Press Enter to continue..."
exit 1
fi
# Check if CDK is installed in the project
if ! npm list aws-cdk &> /dev/null; then
echo "Installing AWS CDK..."
if ! npm install --save-dev aws-cdk; then
echo "Failed to install AWS CDK"
read -p "Press Enter to continue..."
exit 1
fi
fi
# Verify CDK version
echo "Verifying CDK version..."
if ! npx cdk --version; then
echo "Failed to verify CDK installation"
read -p "Press Enter to continue..."
exit 1
fi
# Build TypeScript files
echo "Building TypeScript files..."
if ! npm run build; then
echo "Failed to build TypeScript files"
read -p "Press Enter to continue..."
exit 1
fi
# Install server dependencies
echo "Installing server dependencies..."
if ! (cd server && npm install --omit=dev); then
echo "Failed to install server dependencies"
read -p "Press Enter to continue..."
exit 1
fi
# CDK deployment steps
echo "Starting CDK deployment process..."
echo "Bootstrapping CDK stack..."
if ! npx cdk bootstrap; then
echo "CDK bootstrap failed"
read -p "Press Enter to continue..."
exit 1
fi
echo "Synthesizing CDK stack..."
if ! npx cdk synth; then
echo "CDK synthesis failed"
read -p "Press Enter to continue..."
exit 1
fi
# Set deployment environment
echo "Configuring deployment environment..."
export IS_LOCAL=false
if ! node build.js; then
echo "Failed to run build script"
read -p "Press Enter to continue..."
exit 1
fi
# Deploy
echo "Deploying CDK stack..."
if ! npx cdk deploy --require-approval never; then
echo "CDK deployment failed"
read -p "Press Enter to continue..."
exit 1
fi
echo "Deployment completed successfully!"
read -p "Press Enter to continue..."