Rails Support: Afterburner Mode

Jets supports deploying Rails applications sometimes without any changes to your code. Note, this is an experimental feature.

How It Works

Jets Afterburner mode works by starting a rack subprocess in the Lambda Execution Context and then a normal Jets application proxies to that server. This is accomplished via Jets Mega Mode. All of this happens transparently, you just deploy from your rails application folder.

Usage

If your Rails application uses environment variables such as DATABASE_URL or DATABASE_PASSWORD, Jets needs to know about them prior to deployment in order to make them available to the generated Lambda functions. You should set the variables in .env files which should be placed in your Rails project’s .jets/project/ directory. Be sure to read the .env file documentation so that you know how to name your .env files.

Setting the Project Name

By default, Jets will infer the project name from the folder you are in. You can override this with a special .jets/project/project_name file. The first line in .jets/project/project_name will be used as the project name if the file exists.

Advanced customizations

If you want to make Lambda-specific application-wide configurations, you can add config files in the .jets/project directory. For example, you can set the Lamba function memory size for the production environment inside of .jets/project/config/environments/production.rb:

Jets.application.configuredoconfig.function.memory_size=1024end

Read the documentation for Function Properties to learn about other properties which can be set.

Notes and Considerations

Do not add jets to your Rails project’s Gemfile. Adding it to the Gemfile might result in bundler being unable to resolve dependencies. Jets is used as a stand-alone tool in Afterburner mode.

Afterburner mode provides a way to run Rails on Serverless but is not a panacea for all Rails applications. Each and every Rails application is different and likely makes assumptions that it’s running on a traditional server not serverless.

For example, the app might upload files or images to the filesystem. This doesn’t work on AWS Lambda because the app doesn’t have access to a persistent filesystem. The application would have to be reworked to store files on a distributed store like S3 instead.

Also, AWS currently limits the total Lambda code size + Gem Layer to 250MB. AWS Docs Lambda Limits. The baseline Rails gems add up to about 146MB, so you have about 104MB of space left for additional gems.

For more complex Rails apps, you might want to consider looking into Jets Mega Mode. Mega Mode allows you to selectively run parts of your app in Rails and parts in Jets.

Most apps just make more sense to run as a Jets app. Jets was built and specifically designed for the serverless world.

Rails with Jets vs Straight Jets

Generally, it is recommended you run Jets application directly instead of Afterburner mode. Though mostly hidden from a user perspective, there is overhead associated and extra obfuscation with Jets Afterburner. This Mega Mode Post may help.

When you run a Jets application natively, you also get access to the full power of Jets. Some examples are Jobs, Events, and IAM Policies.

JavaScript Runtime Error

If the Rails rack process fails to start up and you see this error in the CloudWatch logs:

/opt/ruby/gems/2.5.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

This is because Rails includes the uglifier and coffee-rails gems, which depend on a javascript runtime. The error means a javascript runtime like node was not found. In recent versions of the AWS Lambda Ruby Runtime, it looks like node is no longer installed. To fix this, uncomment therubyracer in your Gemfile. Uglifier will detect that therubyracer javascript runtime is available and use that instead.

Another approach that seems to work is to move the uglifier and coffee-rails gems in the Gemfile development group. Jets does not bundle gems in the development group as a part of deployment to Lambda.