-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·151 lines (130 loc) · 4.92 KB
/
deploy.sh
File metadata and controls
executable file
·151 lines (130 loc) · 4.92 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
#!/bin/bash
set -e
# Configuration
ECR_REPOSITORY_NAME="pandoc-lambda"
LAMBDA_FUNCTION_NAME="pandoc-lambda-function"
AWS_REGION=${AWS_REGION:-"us-east-1"}
# Enable debug mode
set -x
# Check if AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "AWS CLI not found. Please install it first."
exit 1
fi
# Check if Docker is installed
if ! command -v docker &> /dev/null; then
echo "Docker not found. Please install it first."
exit 1
fi
# Get AWS account ID
echo "Getting AWS account ID..."
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
if [ $? -ne 0 ]; then
echo "Failed to get AWS account ID. Make sure your AWS credentials are configured correctly."
exit 1
fi
echo "AWS Account ID: ${AWS_ACCOUNT_ID}"
# Full ECR repository URI
ECR_REPOSITORY_URI="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${ECR_REPOSITORY_NAME}"
echo "ECR Repository URI: ${ECR_REPOSITORY_URI}"
# Create ECR repository if it doesn't exist
echo "Checking if ECR repository exists..."
aws ecr describe-repositories --repository-names ${ECR_REPOSITORY_NAME} --region ${AWS_REGION} || \
aws ecr create-repository --repository-name ${ECR_REPOSITORY_NAME} --region ${AWS_REGION}
# Login to ECR with debugging
echo "Attempting to get ECR login password..."
ECR_PASSWORD=$(aws ecr get-login-password --region ${AWS_REGION})
if [ $? -ne 0 ]; then
echo "Failed to get ECR login password"
exit 1
fi
echo "Attempting Docker login..."
echo "${ECR_PASSWORD}" | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
if [ $? -ne 0 ]; then
echo "Failed to login to ECR"
exit 1
fi
# Build and push Docker image
echo "Building and pushing Docker image directly to ECR..."
IMAGE_TAG=$(date +%Y%m%d%H%M%S)
docker buildx build \
--platform linux/amd64 \
--provenance=false \
--push \
-t ${ECR_REPOSITORY_URI}:latest \
-t ${ECR_REPOSITORY_URI}:${IMAGE_TAG} \
.
# Verify the image was pushed to ECR
echo "Verifying image in ECR..."
aws ecr describe-images --repository-name ${ECR_REPOSITORY_NAME} --region ${AWS_REGION}
# Check if Lambda function exists
echo "Checking if Lambda function exists..."
LAMBDA_EXISTS=$(aws lambda list-functions --region ${AWS_REGION} --query "Functions[?FunctionName=='${LAMBDA_FUNCTION_NAME}'].FunctionName" --output text)
if [ -z "${LAMBDA_EXISTS}" ]; then
# Create Lambda function
echo "Creating Lambda function..."
aws lambda create-function \
--function-name ${LAMBDA_FUNCTION_NAME} \
--package-type Image \
--code ImageUri=${ECR_REPOSITORY_URI}:latest \
--role "arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda-pandoc-execution-role" \
--timeout 300 \
--memory-size 2048 \
--architectures x86_64 \
--region ${AWS_REGION} \
--publish || {
echo "Failed to create Lambda function"
exit 1
}
else
# Update Lambda function code
echo "Updating Lambda function code..."
aws lambda update-function-code \
--function-name ${LAMBDA_FUNCTION_NAME} \
--image-uri ${ECR_REPOSITORY_URI}:latest \
--region ${AWS_REGION} \
--publish || {
echo "Failed to update Lambda function code"
exit 1
}
# Wait for the function code update to complete
echo "Waiting for function code update to complete..."
aws lambda wait function-updated \
--function-name ${LAMBDA_FUNCTION_NAME} \
--region ${AWS_REGION}
# Update Lambda function configuration with retry logic
echo "Updating Lambda function configuration..."
retry_count=0
max_retries=5
retry_delay=10
while [ $retry_count -lt $max_retries ]; do
if aws lambda update-function-configuration \
--function-name ${LAMBDA_FUNCTION_NAME} \
--timeout 300 \
--memory-size 2048 \
--region ${AWS_REGION}; then
echo "Lambda function configuration updated successfully."
break
else
retry_count=$((retry_count+1))
if [ $retry_count -lt $max_retries ]; then
echo "Update failed. Retrying in ${retry_delay} seconds... (Attempt ${retry_count}/${max_retries})"
sleep ${retry_delay}
# Increase delay with each retry (exponential backoff)
retry_delay=$((retry_delay*2))
else
echo "Failed to update Lambda function configuration after ${max_retries} attempts."
exit 1
fi
fi
done
# Wait for the update to complete
echo "Waiting for function update to complete..."
aws lambda wait function-updated \
--function-name ${LAMBDA_FUNCTION_NAME} \
--region ${AWS_REGION}
fi
# Get function configuration
echo "Getting Lambda function configuration..."
aws lambda get-function --function-name ${LAMBDA_FUNCTION_NAME} --region ${AWS_REGION}
echo "Deployment completed successfully!"