Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bin Scripts - Setup, Build, Server, & Deploy #17

Open
metaskills opened this issue Mar 23, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@metaskills
Copy link
Member

commented Mar 23, 2019

Below is a list of bin scripts we recommend creating for your AWS SAM & Rails projects. Each section contains a short description on what the script does. Remember, think of these scripts as starting points! You should add to or adjust each to meet your needs while following Strap and Scripts to Rule Them All conventions.

All of these are simple Bash scripts so please make sure to chmod +x each file after you create them in your Rails bin directory.

bin/build

This makes use of the sam build command along with the --use-container option to ensure gems with native extensions are built for the AWS' Linux platform. Each section:

  1. Sets up a pristine build environment.
  2. Use SAM's build. Builds gems via Docker for Linux platform.
  3. [HOOK] Section for Environments & Configuration.
  4. [HOOK] Section to Asset Hosts & Precompiling.
  5. Clean build directory's vendor gems & other un-needed files. Limits.
#!/bin/bash
set -e

# Clean any previous bundle/builds.
rm -rf ./.bundle \
  ./vendor/bundle \
  ./.aws-sam/build

# Ensure native extensions built for platform.
sam build --use-container

# [HOOK] Environments & Configuration
# https://github.com/customink/lamby/issues/28

# [HOOK] Asset Hosts & Precompiling
# https://github.com/customink/lamby/issues/29

# Clean un-needed artifacts.
pushd ./.aws-sam/build/RailsFunction/
rm -rf .aws-sam \
  .git \
  log \
  test \
  tmp
popd

⚠️ Does your Gemfile have private gems on GitHub?

If so, the SAM containerized build will have auth issues accessing them. One solution we like requires adding this line prior to the sam build command.

bundle config --local GITHUB__COM "$(git config github.token):x-oauth-basic"

It sets a Bundler config so the container can use your personal GitHub access token. Make sure to setup that up. Lastly, add this line to your Gemfile and switch all private gems to use this github source via HTTPS vs the GIT protocol.

git_source(:github) { |repo| "https://github.com/#{repo}.git" }

⚠️ Git Bundled Gems & Large Packages Sizes?

Using gems from Git sources can easily increase your Lambda's package size. We recommend cleaning gem's .git directories from within the ./.aws-sam/build/RailsFunction/vendor/bundle/ruby/2.5.0 direcotry in the last part of the build script. For example:

find bundler/gems \
  -type d \
  -name ".git" \
  -mindepth 2 \
  -maxdepth 2 \
  -exec rm -rf {} \;

bin/deploy

Uses both the sam package and sam deploy commands to ship your Lambda stack to AWS.

#!/bin/bash
set -e

export RAILS_ENV=${RAILS_ENV:="development"}
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:=us-east-1}
export CLOUDFORMATION_BUCKET=${CLOUDFORMATION_BUCKET:="mycloudformationbucket.example.com"}

./bin/build

sam package \
  --region ${AWS_DEFAULT_REGION} \
  --template-file ./.aws-sam/build/template.yaml \
  --output-template-file ./.aws-sam/build/packaged.yaml \
  --s3-bucket $CLOUDFORMATION_BUCKET \
  --s3-prefix "myapp-${RAILS_ENV}"

sam deploy \
    --template-file ./.aws-sam/build/packaged.yaml \
    --stack-name "hello-rails-${RAILS_ENV}-${AWS_DEFAULT_REGION}" \
    --capabilities "CAPABILITY_IAM" \
    --parameter-overrides \
      RailsEnv=${RAILS_ENV}

⚠️ CloudFormation Bucket

Your Lambda code needs to be packaged to an S3 bucket. It is a good idea to make one and use --s3-prefix to organize the contents. If you have the AWS CLI installed, creating a bucket is a simple command line call like so.

$ aws s3 mb mycloudformationbucket.example.com

⚠️ Deploying to Multiple Environments?

Feel free to make more per-environment deploy scripts. For example bin/deploy-production may look like this.

#!/bin/bash
set -e

export RAILS_ENV="production"

./bin/deploy

bin/server-sam

Uses the sam local start-api command to serve your Rails application within the Lambda environment via Docker.

#!/bin/bash
set -e

sam local start-api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.