Amazon Amplify Script
I’ve used a couple different solutions for my main/“landing” URL for squad51.us, originally I had a linktr.ee Link Tree. For a couple months I had a roll-your-own link tree on a Github Page, and for the last year I’ve just been hosting it at home on my Synology with Web Station.
I’ve been concerned about traffic hitting that thing as well as having to leave it open as a DMZ host, and recently I’ve had a power outage or two at the house that left my server down, and that’s not ideal for my main website.
So I decided to set up a site in an S3 bucket and point my domain over there, but now Amazon recommends that sites be deployed with Amazon Amplify. This will handle CDN and certificates as well as hosting all the back-end stuff, which is a little overkill for me as this site is wholly static, but it works just as well. I don’t sync the site to an s3 bucket, I zip the site after building it in hugo and upload it to Amazon Amplify directly.
#!/usr/bin/env fish
set APP_ID "<app id>"
set BRANCH_NAME "<amplify branch name>"
set BUILD_DIR "public"
set ARCHIVE_NAME "site-deploy.zip"
set AWS_PROFILE "site-deploy"
echo "=> Building Hugo Site to $BUILD_DIR ..."
rm -rf "$BUILD_DIR"
mkdir "$BUILD_DIR"
hugo build --minify -d "$BUILD_DIR"
or begin
echo "ERROR: Hugo build failed"
exit 1
end
if not test -d "$BUILD_DIR"
echo "ERROR: Hugo build directory '$BUILD_DIR' not found"
exit 1
end
echo "=> Creating Site Archive"
rm -rf "$ARCHIVE_NAME"
pushd "$BUILD_DIR"
zip -r "../$ARCHIVE_NAME" .
or begin
echo "ERROR: Failed to create ZIP archive"
popd >/dev/null
exit 1
end
popd
echo "==> Creating AWS Amplify deployment..."
set CREATE_DEPLOYMENT_JSON (
aws amplify create-deployment --profile $AWS_PROFILE \
--app-id "$APP_ID" \
--branch-name "$BRANCH_NAME"
)
or begin
echo "ERROR: Failed to create deployment"
exit 1
end
set UPLOAD_URL (
echo "$CREATE_DEPLOYMENT_JSON" \
| python3 -c 'import sys, json; print(json.load(sys.stdin)["zipUploadUrl"])'
)
set JOB_ID (
echo "$CREATE_DEPLOYMENT_JSON" \
| python3 -c 'import sys, json; print(json.load(sys.stdin)["jobId"])'
)
echo "Deployment job ID: $JOB_ID"
curl \
--fail \
--silent \
--show-error \
-T "$ARCHIVE_NAME" \
"$UPLOAD_URL"
or begin
echo "ERROR: Upload failed"
exit 1
end
echo "==> Starting deployment..."
aws amplify start-deployment --profile $AWS_PROFILE \
--app-id "$APP_ID" \
--branch-name "$BRANCH_NAME" \
--job-id "$JOB_ID" \
>/dev/null
or begin
echo "ERROR: Failed to start deployment"
exit 1
end
echo "==> Waiting for deployment..."
while true
set STATUS (
aws amplify get-job --profile $AWS_PROFILE \
--app-id "$APP_ID" \
--branch-name "$BRANCH_NAME" \
--job-id "$JOB_ID" \
--query 'job.summary.status' \
--output text
)
echo "Current status: $STATUS"
switch "$STATUS"
case SUCCEED
echo "Deployment succeeded"
break
case FAILED CANCELLED
echo "Deployment failed"
exit 1
end
sleep 5
end