rails-lambda / lamby-cookiecutter

🐑🛤 AWS SAM Cookiecutter to Quick Start Rails & Lambda
https://lamby.cloud/docs/quick_start
37 stars 16 forks source link

Allow a rails api only build #4

Closed cimtico closed 3 years ago

cimtico commented 3 years ago

How can I build an api-only app instead of a full rails app? I noticed a _rails-new bin file, but it seems it isn't used during the sam init process, so adding the --api flag in there in my fork didn't do much.

I also tried adding the lamby gem to a new rails api setup, but got errors in aws about the gems not being found after deployment. So I'm not sure what is the best way to proceed

metaskills commented 3 years ago

How can I build an api-only app instead of a full rails app?

You sure can! I would recommend you go two different routes.

  1. Go thru the bootstrap process in full. Then make changes to your app to be API only. What are those changes? Typically a few lines to the application.rb, Gemfile, and the root controller.
  2. You can make a change as you said to the bin/_rails-new file by adding --api. Then do a bin/build and commit that to your own fork/branch. I did just that and made a pull request with the change so technically you can use this branch too. Hope that outlines the process or the manual files you have to change if #1 is what you choose.

but got errors in aws about the gems not being found after deployment. So I'm not sure what is the best way to proceed

I think above will help but if needed you must share the errors. Cant help unless I know what they are. Cheers!

cimtico commented 3 years ago

Thanks for your reply. I just ran through a new install following the steps in the readme (trying to just get something working and try to do precisely what you mentioned). However, with the "clean" install I get these errors:

2020-10-22T17:38:57.599-06:00 Init error when loading handler app.handler
  2020-10-22T17:38:57.599-06:00Copy{ "errorMessage": "Could not find public_suffix-4.0.6 in any of the sources", "errorType": "Init", "stackTrace": [ "/var/runtime/gems/bundler-2.1.4/lib/bundler/spec_set.rb:86:in block in materialize'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/spec_set.rb:80:inmap!'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/spec_set.rb:80:in materialize'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/definition.rb:170:inspecs'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/definition.rb:237:in specs_for'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/definition.rb:226:inrequested_specs'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/runtime.rb:101:in block in definition_method'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/runtime.rb:20:insetup'", "/var/runtime/gems/bundler-2.1.4/lib/bundler.rb:149:in setup'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/setup.rb:20:inblock in <top (required)>'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/ui/shell.rb:136:in with_level'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/ui/shell.rb:88:insilence'", "/var/runtime/gems/bundler-2.1.4/lib/bundler/setup.rb:20:in <top (required)>'", "/var/lang/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:inrequire'", "/var/lang/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in require'", "/var/task/config/boot.rb:3:in<top (required)>'", "/var/task/app.rb:2:in require_relative'", "/var/task/app.rb:2:in<top (required)>'", "/var/lang/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in require'", "/var/lang/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:inrequire'" ] }

It seems to me it that for some reason it is trying to load the "dev" dependencies instead of the prod-only ones. Have you encountered anything like this before? I feel it might just be a bad config somewhere.

cimtico commented 3 years ago

Interesting. It seems I get those errors when I use a container I built with the "newer" sam that is installed through "brew". Using your docker-compose and docker setup seems to get past those errors. However, I get an error about Missing `secret_key_base` for 'production' environment,... eventhough the master.key file is there, and the key is registered in SSM.

metaskills commented 3 years ago

Well, when you deploy to production the config/master.key should not be in the deployed package (https://github.com/customink/lamby-cookiecutter/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/bin/build-rails#L46) it should be written to the .env.production file in the package (see line 19-24) further up from that same link. You could download the package from S3, change the suffix to .gz and check to make sure. Likewise to confirm that bin/deploy is doing as expected you can run ./bin/run bash and that the aws ssm get-parameter ... on Line 19 works as expected. Another method is to check the build artifact directory in .aws-sam director.