Skip to content

Instantly share code, notes, and snippets.

@tarrynn
Last active April 21, 2020 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarrynn/cc2d9b296a0fbaef04edd26edb968a8a to your computer and use it in GitHub Desktop.
Save tarrynn/cc2d9b296a0fbaef04edd26edb968a8a to your computer and use it in GitHub Desktop.
lambda setup
README
## Setup runtime for this lambda function
1. `docker build -t lambda-ruby2.5-postgresql10 .` to create an environment in which we can build gems with native extensions that will run properly on lambda
2. `docker run --rm -it -v $PWD:/var/task -w /var/task lambda-ruby2.5-postgresql10` to run the docker container with a bash terminal
3. Run the following inside the bash terminal opened above
`bundle config --local build.pg --with-pg-config=/usr/pgsql-10/bin/pg_config` inside the bash terminal opened above
`bundle config --local silence_root_warning true`
`bundle install --path vendor/bundle --clean`
`mkdir -p /var/task/lib`
`cp -a /usr/pgsql-10/lib/*.so.* /var/task/lib/`
## Deploy to lambda (after the above)
1. `zip -q -r package.zip * && aws lambda update-function-code --function-name $PROJECT_NAME --zip-file 'fileb://package.zip'`
Dockerfile
FROM lambci/lambda:build-ruby2.5
RUN yum install -y \
https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-6-x86_64/postgresql10-libs-10.10-1PGDG.rhel6.x86_64.rpm
RUN yum install -y \
https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-6-x86_64/postgresql10-10.10-1PGDG.rhel6.x86_64.rpm
RUN yum install -y postgresql10-devel
RUN gem update bundler
CMD "/bin/bash"
Gemfile
source "https://rubygems.org"
gem "pg"
Gemfile.lock
GEM
remote: https://rubygems.org/
specs:
pg (1.1.4)
PLATFORMS
ruby
DEPENDENCIES
pg
BUNDLED WITH
2.0.1
handler.rb
require 'pg'
DB_HOST = ENV['DB_HOST']
DB_PORT = ENV['DB_PORT']
DB_NAME = ENV['DB_NAME']
DB_USER = ENV['DB_USER']
DB_PWD = ENV['DB_PWD']
def main(event:, context:)
conn = PG::Connection.new( :host => DB_HOST, :dbname => DB_NAME, :port => DB_PORT, :user => DB_USER, :password => DB_PWD )
20.times do |n|
conn.exec( "refresh materialized view stuff;" )
sleep 2.7
end
{
sql: 'refresh materialized view stuff'
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment