It’s nice to host your own blog in AWS using S3 and CloudFront but deployment can quickly become a hassle. I intend to set up a fully automated pipeline but for the time being I just developed the following script to deploy the site.
The script does the following:
- Build Jekyll site
- Delete the contents of the target bucket
- Upload the site contents to the target bucket
- Invalidate CloudFormation
Also note that it assumes:
- The bucket is set up yo enable static wrbsite hosting
- There is a CloudFront distribtuion setup to use this bucket as source
- There is a DNS entry pointing to the CloudFront distribtuion.
Assuming those have already been created and set up, the deployment function works like below:
[bool] $runBuild = $true
Write-Host "Building blog"
bundle exec jekyll build
Write-Host "Clearing bucket"
aws s3 rm s3://$s3BucketName --recursive
Write-Host "Uploading site to staging bucket"
aws s3 cp ./_site s3://$s3BucketName --acl public-read --recursive
Write-Host "Invalidate CloudFront distribution"
aws cloudfront create-invalidation --distribution-id $cloudFrontDistributionId --paths "/*"
and the consuming function looks like this:
To deploy to staging and confirm the changes work fine, run:
$STAGING_BUCKET_NAME = "staging.my.domain.com"
$STAGING_DISTRIBUTION_ID = "STAGING_DIST_ID"
Deploy-Blog $STAGING_BUCKET_NAME $STAGING_DISTRIBUTION_ID
This script build the Jekyll site and deploys to staging. Then to promote to production, run:
$PROD_BUCKET_NAME = "my.domain.com"
$PROD_DISTRIBUTION_ID = "PROD_DIST_ID"
Deploy-Blog $PROD_BUCKET_NAME $PROD_DISTRIBUTION_ID $false
Note the build parameter is set to false as we don’t need to build the site again. We just want to deploy the previous output we deployed to staging stragith to production.