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

Ruby Layers not properly packaged #177

jkahn117 opened this issue Jun 10, 2020 · 3 comments

Ruby Layers not properly packaged #177

jkahn117 opened this issue Jun 10, 2020 · 3 comments


Copy link

jkahn117 commented Jun 10, 2020


When using SAM to build a Ruby layer, packaging of Gems is incorrect. Gems are bundled in ruby /lib/vendor/bundle/ruby/2.7.0 using the default bundler workflow, these Gems are not picked up by the runtime.

Per documentation, GEM_PATH (ruby/gems/2.7.0) should be used. Shared / custom code is packaged properly to RUBYLIB (ruby/lib).

Steps to reproduce the issue:

  1. Create new SAM application.
  2. Add Layer to template:
    Type: AWS::Serverless::LayerVersion
      ContentUri: SAMBuild
        - ruby2.7
      BuildMethod: ruby2.7
  1. Add directory SAMBuild containing a Gemfile with valid contents.
  2. Run sam build command.
  3. Review results and/or test with a sample function.

Observed result:

Result of build (.aws-sam/build) contains SAMBuildLayer/ruby/lib/vendor/bundle/ruby/2.7.0/gems with bundled Gems. When attempting to run sample function, Gem is not found.

Expected result:

Gem should be packaged in ruby/gems/2.7.0 and available to function when executed. I was able to build using a Makefile:

  Type: AWS::Serverless::LayerVersion
    ContentUri: MakeBuild
      - ruby2.7
    BuildMethod: makefile

And Makefile in MakeBuild directory:

  bundle install --path=$(ARTIFACTS_DIR)
  rm -rf $(ARTIFACTS_DIR)/ruby/2.7.0/cache && rm -rf $(ARTIFACTS_DIR)/ruby/2.7.0/bin
  mkdir $(ARTIFACTS_DIR)/ruby/gems
  mv $(ARTIFACTS_DIR)/ruby/2.7.0 $(ARTIFACTS_DIR)/ruby/gems

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

SAM CLI, version 0.52.0

Copy link

jorian commented Dec 14, 2020

Having this issue. Are you still using the Makefile workaround, @jkahn117 ?

Edit: found your blog, really helpful 👍

Copy link

davegallant commented Jun 7, 2021

I ran into the same issue today.

The only way I was able to get the above workaround to work on SAM 1.24.0 was to adjust the above Makefile with:


mkfile_path := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))

	bundle install --path=$(mkfile_path)
	rm -rf $(mkfile_path)/ruby/$(RUBY_VERSION)/cache && rm -rf $(mkfile_path)/ruby/$(RUBY_VERSION)/bin
	mkdir -p $(mkfile_path)/ruby/gems
	mv $(mkfile_path)/ruby/$(RUBY_VERSION) $(mkfile_path)/ruby/gems

Otherwise, it would package up the ContentUri and nothing would be there.

Copy link

ryanjfrizzell commented Jun 11, 2021

+1 here for those who need some postgres (pg gem) love...based on @davegallant and @jkahn117 's work above

SHELL := /bin/bash
PGVERSION := 11.10
PGSOURCE := postgresql-$(PGVERSION)
.PHONY: build-RubyLayer


	yum install -y amazon-linux-extras tree
	yum makecache
	yum install -y postgresql-devel postgresql-libs
	curl -O
	tar -xvzf $(PGSOURCE).tar.gz
	(cd $(PGSOURCE) && ./configure --prefix=$(BP) --without-readline && $(MAKE) -C . MAKELEVEL=0 all && $(MAKE) install)
	bundle config --local --with-pg-config=$(BP)/bin/pg_config
	bundle config --local silence_root_warning true
	bundle config --local path $(ARTIFACTS_DIR)
	bundle install
	mkdir $(ARTIFACTS_DIR)/ruby/gems
	mv $(ARTIFACTS_DIR)/ruby/2.7.0 $(ARTIFACTS_DIR)/ruby/gems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

6 participants