Skip to content

Instantly share code, notes, and snippets.

@rakibulislam
Forked from ssaunier/cache.yml
Created November 24, 2020 18:21
Show Gist options
  • Save rakibulislam/7f1d6ecdfb933cb2b910c01a20c249a1 to your computer and use it in GitHub Desktop.
Save rakibulislam/7f1d6ecdfb933cb2b910c01a20c249a1 to your computer and use it in GitHub Desktop.
Speed up the AWS Elastic Beanstalk of your Rails deployment with bundle/assets caching
# .ebextensions/cache.config
files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/02a_set_cache.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
set -xe
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
BUCKET="elasticbeanstalk-PUT_YOUR_REGION-PUT_YOUR_ACCOUNT_ID"
mkdir -p /tmp/cache
if aws s3 ls s3://${BUCKET}/cache/gem.tar.gz ; then
aws s3 cp --quiet s3://${BUCKET}/cache/gem.tar.gz /tmp/cache/gem.tar.gz
fi
if aws s3 ls s3://${BUCKET}/cache/node_modules.tar.gz ; then
aws s3 cp --quiet s3://${BUCKET}/cache/node_modules.tar.gz /tmp/cache/node_modules.tar.gz
fi
if aws s3 ls s3://${BUCKET}/cache/assets.tar.gz ; then
aws s3 cp --quiet s3://${BUCKET}/cache/assets.tar.gz /tmp/cache/assets.tar.gz
fi
[ -f /tmp/cache/gem.tar.gz ] && tar -xf /tmp/cache/gem.tar.gz -C $EB_APP_STAGING_DIR && chown webapp:webapp -R "$EB_APP_STAGING_DIR/vendor/bundle"
[ -f /tmp/cache/node_modules.tar.gz ] && tar -xf /tmp/cache/node_modules.tar.gz -C $EB_APP_STAGING_DIR && chown webapp:webapp -R "$EB_APP_STAGING_DIR/node_modules"
[ -f /tmp/cache/assets.tar.gz ] && tar -xf /tmp/cache/assets.tar.gz -C $EB_APP_STAGING_DIR && chown webapp:webapp -R "$EB_APP_STAGING_DIR/tmp"
exit 0
"/opt/elasticbeanstalk/hooks/appdeploy/pre/11a_build_cache.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
set -xe
EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
cd $EB_APP_STAGING_DIR
mkdir -p /tmp/cache
tar -zcf /tmp/cache/gem.tar.gz vendor/bundle
tar -zcf /tmp/cache/node_modules.tar.gz node_modules
tar -zcf /tmp/cache/assets.tar.gz tmp/cache/assets
BUCKET="elasticbeanstalk-PUT_YOUR_REGION-PUT_YOUR_ACCOUNT_ID"
aws s3 cp /tmp/cache/gem.tar.gz s3://${BUCKET}/cache/gem.tar.gz
aws s3 cp /tmp/cache/node_modules.tar.gz s3://${BUCKET}/cache/node_modules.tar.gz
aws s3 cp /tmp/cache/assets.tar.gz s3://${BUCKET}/cache/assets.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment