-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildspec-unified.yml
More file actions
202 lines (185 loc) · 8.01 KB
/
buildspec-unified.yml
File metadata and controls
202 lines (185 loc) · 8.01 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
version: 0.2
phases:
install:
runtime-versions:
python: 3.12
nodejs: 18
commands:
- echo "Installing dependencies..."
- npm install -g aws-cdk@latest
- pip install --upgrade pip
- echo "Deployment Type is $DEPLOYMENT_TYPE"
pre_build:
commands:
- echo "Pre-build phase started on $(date)"
- echo "Deployment Type is $DEPLOYMENT_TYPE"
- aws --version
- cdk --version
- |
if [ "$DEPLOYMENT_TYPE" = "pdf2html" ]; then
echo "PDF-to-HTML pre-build steps..."
echo "Environment variables:"
echo " ACCOUNT_ID=$ACCOUNT_ID"
echo " REGION=$REGION"
echo " BUCKET_NAME=$BUCKET_NAME"
echo " BDA_PROJECT_ARN=$BDA_PROJECT_ARN"
docker --version
echo "Current directory contents:"
ls -la
echo "Navigating to pdf2html directory:"
cd pdf2html
ls -la
echo "Logging in to Amazon ECR..."
if ! aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com; then
echo "ERROR: Initial ECR login failed!"
echo "AWS CLI version:"
aws --version
echo "Region: $REGION"
echo "Account ID: $ACCOUNT_ID"
exit 1
fi
echo "Initial ECR login successful."
cd ..
else
echo "PDF-to-PDF pre-build steps..."
echo "Installing Python dependencies..."
pip install -r requirements.txt
echo "Bootstrapping CDK environment with retry logic..."
for i in {1..3}; do
echo "CDK bootstrap attempt $i/3..."
if cdk bootstrap; then
echo "CDK bootstrap successful on attempt $i"
break
else
echo "CDK bootstrap failed on attempt $i"
if [ $i -eq 3 ]; then
echo "All CDK bootstrap attempts failed, continuing anyway..."
else
echo "Waiting 15 seconds before retry..."
sleep 15
fi
fi
done
fi
build:
commands:
- echo "Build phase started on $(date)"
- |
if [ "$DEPLOYMENT_TYPE" = "pdf2html" ]; then
echo "Deploying PDF-to-HTML Remediation Solution..."
cd pdf2html
# Create S3 bucket if it doesn't exist
if ! aws s3api head-bucket --bucket $BUCKET_NAME 2>/dev/null; then
echo "Creating S3 bucket $BUCKET_NAME..."
if [ "$REGION" = "us-east-1" ]; then
aws s3api create-bucket --bucket $BUCKET_NAME --region $REGION
else
aws s3api create-bucket --bucket $BUCKET_NAME --region $REGION --create-bucket-configuration LocationConstraint=$REGION
fi
aws s3api put-bucket-versioning --bucket $BUCKET_NAME --versioning-configuration Status=Enabled
aws s3api put-object --bucket $BUCKET_NAME --key uploads/
aws s3api put-object --bucket $BUCKET_NAME --key output/
aws s3api put-object --bucket $BUCKET_NAME --key remediated/
echo "S3 bucket created successfully."
else
echo "S3 bucket $BUCKET_NAME already exists."
fi
# Create ECR repository if it doesn't exist
REPO_NAME="pdf2html-lambda"
if ! aws ecr describe-repositories --repository-names $REPO_NAME --region $REGION 2>/dev/null; then
echo "Creating ECR repository $REPO_NAME..."
aws ecr create-repository --repository-name $REPO_NAME --region $REGION
echo "ECR repository created successfully."
else
echo "ECR repository $REPO_NAME already exists."
fi
# Build and push Docker image with proper error handling
echo "Building Docker image..."
REPO_URI="$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/pdf2html-lambda"
# Build Docker image with error checking
if ! docker build --platform linux/amd64 --no-cache -t $REPO_URI:latest .; then
echo "ERROR: Docker build failed!"
echo "Build context contents:"
ls -la
echo "Docker version:"
docker --version
exit 1
fi
echo "Docker build completed successfully."
# Refresh ECR login right before push to prevent token expiry
echo "Refreshing ECR login..."
if ! aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com; then
echo "ERROR: ECR login failed!"
exit 1
fi
echo "ECR login successful."
# Push Docker image with error checking and retry logic
echo "Pushing Docker image to ECR..."
if ! docker push $REPO_URI:latest; then
echo "ERROR: Docker push failed on first attempt!"
echo "Checking Docker images:"
docker images | grep pdf2html-lambda
echo "Retrying ECR login and push..."
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com
if ! docker push $REPO_URI:latest; then
echo "ERROR: Docker push failed after retry!"
echo "ECR repository status:"
aws ecr describe-repositories --repository-names pdf2html-lambda --region $REGION || echo "Repository not found"
exit 1
fi
fi
echo "Docker image pushed successfully."
# Verify image exists in ECR
echo "Verifying image in ECR..."
if ! aws ecr describe-images --repository-name pdf2html-lambda --region $REGION --image-ids imageTag=latest; then
echo "ERROR: Image not found in ECR after push!"
echo "Available images in repository:"
aws ecr list-images --repository-name pdf2html-lambda --region $REGION || echo "No images found"
exit 1
fi
echo "Image verified successfully in ECR."
# Bootstrap CDK and deploy
echo "Bootstrapping CDK..."
cd cdk
npm install
# Set CDK environment variables to ensure region consistency
export CDK_DEFAULT_ACCOUNT=$ACCOUNT_ID
export CDK_DEFAULT_REGION=$REGION
npx cdk bootstrap aws://$ACCOUNT_ID/$REGION
echo "Deploying CDK stack..."
npx cdk deploy --app "node bin/app.js" --parameters BdaProjectArn=$BDA_PROJECT_ARN --parameters BucketName=$BUCKET_NAME --require-approval never
elif [ "$DEPLOYMENT_TYPE" = "pdf2pdf" ]; then
echo "Deploying PDF-to-PDF Remediation Solution..."
echo "Deploying CDK stacks with retry logic..."
for i in {1..3}; do
echo "CDK deploy attempt $i/3..."
if cdk deploy --all --require-approval never; then
echo "CDK deploy successful on attempt $i"
break
else
echo "CDK deploy failed on attempt $i"
if [ $i -eq 3 ]; then
echo "All CDK deploy attempts failed"
exit 1
fi
echo "Waiting 30 seconds before retry..."
sleep 30
fi
done
else
echo "Invalid DEPLOYMENT_TYPE: $DEPLOYMENT_TYPE"
echo "Valid options are: pdf2pdf, pdf2html"
exit 1
fi
post_build:
commands:
- echo "Post-build phase completed on $(date)"
- |
if [ "$DEPLOYMENT_TYPE" = "pdf2html" ]; then
echo "PDF-to-HTML deployment completed successfully!"
echo "S3 Bucket: $BUCKET_NAME"
echo "BDA Project: $BDA_PROJECT_ARN"
else
echo "PDF-to-PDF deployment completed successfully!"
echo "Check the CloudWatch dashboard for monitoring."
fi