lamby 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fcbbfe7eddb8acad415555fea80e62f2bd9e7dd265e2c48ec5921e9f97f1e436
4
- data.tar.gz: 5c27a927c94e8e49117f384cf17e657be0f75c570fe4d162111ed9775cf66e16
3
+ metadata.gz: 158acb62cde3cf096b2fa436266c972673610f9ae3a50c453861c68c17fc5354
4
+ data.tar.gz: 3222b4c37bf6499526edfa9fb7b9bf6368c2792f2ffac2cc58ff5b60dbe9162a
5
5
  SHA512:
6
- metadata.gz: 2a251f3fc0441f08fc8dbcb12a52aef07b10c200fa27728406d53fdc2a15df8dfc5c711fd5c37c313e7ad2663b82c6d481cfe25883c344dff5713b63628b9072
7
- data.tar.gz: 40ceda09d0393eadc9e457d3c4f55c9c3812c366f9b63185846be31d848a288139a37f0adecd694f153a683842d60c2d47a8e9511c6cb1dce685469000d84308
6
+ metadata.gz: 06e5cd72373261f7d6197dd85fda6c620d61183a002c80397be5433c61178c7fe632ef1b9264935c799ed5725caa12cf65880343d03cc091ad1feb51322b1613
7
+ data.tar.gz: 48f2f83253a4a44b8334fa0117400ff9d99d7d976dfd1b3847f4e64cb98a4c985f6d0cb52577447fc6dd99c253214969717a9821805a23336dafbb97a018ca5c
@@ -1 +1 @@
1
- 3.6.6
1
+ 3.6.7
@@ -4,7 +4,13 @@ language: ruby
4
4
  cache: bundler
5
5
  rvm:
6
6
  - 2.5.5
7
- before_install:
7
+ install:
8
+ - pushd /opt/pyenv/plugins/python-build && git pull origin master && popd
9
+ before_script:
10
+ - ls -lAGp
11
+ - pyenv install --skip-existing $(cat .python-version)
12
+ - sudo apt-get install python3-pip python3-dev
13
+ - pip3 install pipenv
8
14
  - sudo gem install bundler -v 1.17.3
9
15
  - bin/setup
10
16
  script:
@@ -2,6 +2,16 @@
2
2
 
3
3
  See this http://keepachangelog.com link for information on how we want this documented formatted.
4
4
 
5
+ ## v0.6.0
6
+
7
+ #### Added
8
+
9
+ * APPLICATION LOAD BALANACER SUPPORT!!! The new default. Use `rack: :api` option to handler for API Gateway support.
10
+
11
+ #### Changed
12
+
13
+ * Rake task `lamby:install` now defaults to `application_load_balancer`
14
+
5
15
 
6
16
  ## v0.5.1
7
17
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lamby (0.5.1)
4
+ lamby (0.6.0)
5
5
  rack
6
6
  rails
7
7
 
@@ -133,9 +133,9 @@ GEM
133
133
  thread_safe (0.3.6)
134
134
  tzinfo (1.2.5)
135
135
  thread_safe (~> 0.1)
136
- websocket-driver (0.7.0)
136
+ websocket-driver (0.7.1)
137
137
  websocket-extensions (>= 0.1.0)
138
- websocket-extensions (0.1.3)
138
+ websocket-extensions (0.1.4)
139
139
 
140
140
  PLATFORMS
141
141
  ruby
@@ -18,10 +18,10 @@
18
18
  "default": {
19
19
  "arrow": {
20
20
  "hashes": [
21
- "sha256:002f2315cf4c8404de737c42860441732d339bbc57fee584e2027520e055ecc1",
22
- "sha256:82dd5e13b733787d4eb0fef42d1ee1a99136dc1d65178f70373b3678b3181bfc"
21
+ "sha256:03404b624e89ac5e4fc19c52045fa0f3203419fd4dd64f6e8958c522580a574a",
22
+ "sha256:41be7ea4c53c2cf57bf30f2d614f60c411160133f7a0a8c49111c30fb7e725b5"
23
23
  ],
24
- "version": "==0.13.2"
24
+ "version": "==0.14.2"
25
25
  },
26
26
  "aws-lambda-builders": {
27
27
  "hashes": [
@@ -33,12 +33,12 @@
33
33
  },
34
34
  "aws-sam-cli": {
35
35
  "hashes": [
36
- "sha256:2dd68800723c76f52980141ba704e105d77469b6ba465781fbc9120e8121e76c",
37
- "sha256:671fb8032625e560c271021e33b0f8592204025243e951c7bc215847aaa13c68",
38
- "sha256:7bede6dff854037d49ff223e921d4bd55ff92e2765198df9596e34e254d5a57b"
36
+ "sha256:1da003711aab7fa668f2fbf82d078e407b64533a8d356d4452d2e38bdab3a55e",
37
+ "sha256:95be2293373f323975198f7695eb712d687920d1e376918b54df24d9c4ef1580",
38
+ "sha256:b487e4e3c6947f4d7a1694a18ff35a080ad540caf118cbdfca86657729e3a9ea"
39
39
  ],
40
40
  "index": "pypi",
41
- "version": "==0.16.1"
41
+ "version": "==0.17.0"
42
42
  },
43
43
  "aws-sam-translator": {
44
44
  "hashes": [
@@ -48,11 +48,11 @@
48
48
  },
49
49
  "awscli": {
50
50
  "hashes": [
51
- "sha256:734637607adf6034b0021073dd8582871dba2c97df0df4f443552574390def8e",
52
- "sha256:a9b8a72f7a07b08be50eda14c4542fd14614aeecb73cf8477f8f92c403406c15"
51
+ "sha256:0331fe61eb2d64fc8045141baf3e919db7e2d39b188a171c52865df1f13f3312",
52
+ "sha256:ccd5b3021ed537a147709ed19807364acdd4ae23a033568966bdf62ecb4da810"
53
53
  ],
54
54
  "index": "pypi",
55
- "version": "==1.16.166"
55
+ "version": "==1.16.177"
56
56
  },
57
57
  "binaryornot": {
58
58
  "hashes": [
@@ -63,17 +63,17 @@
63
63
  },
64
64
  "boto3": {
65
65
  "hashes": [
66
- "sha256:59782c178af2d5acf66315fe96b3cd1dc075109c0296c384e18a6c4143c0745d",
67
- "sha256:758787b5ad7c5e2aa2979b0671129491fbab00a7b84a26532cd6b9d073ed862b"
66
+ "sha256:60bbb64ca5ff05ec3aecca582e33b516e774c739fa9e9591c1e3db512e81bc49",
67
+ "sha256:e59cf23e82156b8f4d8dcebd8fcae618b30ca10125f9e7222eb181a627913f0d"
68
68
  ],
69
- "version": "==1.9.156"
69
+ "version": "==1.9.167"
70
70
  },
71
71
  "botocore": {
72
72
  "hashes": [
73
- "sha256:00b72bc2104a2f56513bc40ce380d0605262decc9fe3b2ce840da48f257598d7",
74
- "sha256:a12a817bf1faf36837bc2d371aacfb5c7c324e0e9f0b3af94b9930cfcd8d62ea"
73
+ "sha256:bba0548c3638877aebe3356819b5235b9d39cd4d2ebac9946979a93e412bb777",
74
+ "sha256:d3424b05a13ac38efe225a2f75a72d3f949513980f737001ae9279edd81b1b31"
75
75
  ],
76
- "version": "==1.12.156"
76
+ "version": "==1.12.167"
77
77
  },
78
78
  "certifi": {
79
79
  "hashes": [
@@ -154,10 +154,10 @@
154
154
  },
155
155
  "idna": {
156
156
  "hashes": [
157
- "sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e",
158
- "sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16"
157
+ "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",
158
+ "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"
159
159
  ],
160
- "version": "==2.7"
160
+ "version": "==2.8"
161
161
  },
162
162
  "itsdangerous": {
163
163
  "hashes": [
@@ -274,24 +274,26 @@
274
274
  },
275
275
  "regex": {
276
276
  "hashes": [
277
- "sha256:0ac2ba14f1e069507eec395140c0701045340f386a7ee7adb95961b3a2d821ef",
278
- "sha256:3b90bfb577ba7ff2c25f74496096e8c2a92007b4c32108a7076d3b33a92ab357",
279
- "sha256:455f29e0ad4a8002aed6a6c00c8054441dde03e25b1d622568afddb251439f14",
280
- "sha256:6b957a06533fa7f070d339c1399fcc739d8b9b08f9d82c768e19bcf2b5cf5427",
281
- "sha256:6fe1dd1d522062912e18d08f1ddd508431fdad7af2ebd80e035d74a1953e9e6a",
282
- "sha256:7e3f1cbdd4d38dade6c880551fe7c2a2832635c743d9c8993785714615d7c08d",
283
- "sha256:81f13830915fb3fe88d49e896a99a25eeb62dc1f3808c77bf3588fe8cdfcadc5",
284
- "sha256:a5b17c52ce57657459162fb414f2ff8b85b7212921281ffe1ca63fb4105bee24",
285
- "sha256:cc4dcdb1ee6fcb7d6a0b6eff4d86cb203b5b082719c94c88b7d7175c13691f92"
277
+ "sha256:1c70ccb8bf4ded0cbe53092e9f56dcc9d6b0efcf6e80b6ef9b0ece8a557d6635",
278
+ "sha256:2948310c01535ccb29bb600dd033b07b91f36e471953889b7f3a1e66b39d0c19",
279
+ "sha256:2ab13db0411cb308aa590d33c909ea4efeced40188d8a4a7d3d5970657fe73bc",
280
+ "sha256:38e6486c7e14683cd1b17a4218760f0ea4c015633cf1b06f7c190fb882a51ba7",
281
+ "sha256:80dde4ff10b73b823da451687363cac93dd3549e059d2dc19b72a02d048ba5aa",
282
+ "sha256:84daedefaa56320765e9c4d43912226d324ef3cc929f4d75fa95f8c579a08211",
283
+ "sha256:b98e5876ca1e63b41c4aa38d7d5cc04a736415d4e240e9ae7ebc4f780083c7d5",
284
+ "sha256:ca4f47131af28ef168ff7c80d4b4cad019cb4cabb5fa26143f43aa3dbd60389c",
285
+ "sha256:cf7838110d3052d359da527372666429b9485ab739286aa1a11ed482f037a88c",
286
+ "sha256:dd4e8924915fa748e128864352875d3d0be5f4597ab1b1d475988b8e3da10dd7",
287
+ "sha256:f2c65530255e4010a5029eb11138f5ecd5aa70363f57a3444d83b3253b0891be"
286
288
  ],
287
- "version": "==2019.5.25"
289
+ "version": "==2019.6.8"
288
290
  },
289
291
  "requests": {
290
292
  "hashes": [
291
- "sha256:65b3a120e4329e33c9889db89c80976c5272f56ea92d3e74da8a463992e3ff54",
292
- "sha256:ea881206e59f41dbd0bd445437d792e43906703fff75ca8ff43ccdb11f33f263"
293
+ "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",
294
+ "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31"
293
295
  ],
294
- "version": "==2.20.1"
296
+ "version": "==2.22.0"
295
297
  },
296
298
  "rsa": {
297
299
  "hashes": [
@@ -302,10 +304,10 @@
302
304
  },
303
305
  "s3transfer": {
304
306
  "hashes": [
305
- "sha256:7b9ad3213bff7d357f888e0fab5101b56fa1a0548ee77d121c3a3dbfbef4cb2e",
306
- "sha256:f23d5cb7d862b104401d9021fc82e5fa0e0cf57b7660a1331425aab0c691d021"
307
+ "sha256:6efc926738a3cd576c2a79725fed9afde92378aa5c6a957e3af010cb019fac9d",
308
+ "sha256:b780f2411b824cb541dbcd2c713d0cb61c7d1bcadae204cdddda2b35cef493ba"
307
309
  ],
308
- "version": "==0.2.0"
310
+ "version": "==0.2.1"
309
311
  },
310
312
  "serverlessrepo": {
311
313
  "hashes": [
@@ -329,11 +331,11 @@
329
331
  },
330
332
  "urllib3": {
331
333
  "hashes": [
332
- "sha256:2393a695cd12afedd0dcb26fe5d50d0cf248e5a66f75dbd89a3d4eb333a61af4",
333
- "sha256:a637e5fae88995b256e3409dc4d52c2e2e0ba32c42a6365fee8bbd2238de3cfb"
334
+ "sha256:b246607a25ac80bedac05c6f282e3cdaf3afb65420fd024ac94435cabe6e18d1",
335
+ "sha256:dbe59173209418ae49d485b87d1681aefa36252ee85884c31346debd19463232"
334
336
  ],
335
337
  "markers": "python_version >= '3.4'",
336
- "version": "==1.24.3"
338
+ "version": "==1.25.3"
337
339
  },
338
340
  "websocket-client": {
339
341
  "hashes": [
data/README.md CHANGED
@@ -1,174 +1,28 @@
1
1
 
2
2
  # Lamby [![Build Status](https://travis-ci.org/customink/lamby.svg?branch=master)](https://travis-ci.org/customink/lamby)
3
3
 
4
- <img src="https://user-images.githubusercontent.com/2381/54278425-af365680-4568-11e9-972a-6b73e0a44bb5.jpg" alt="Lamby: Simple Rails & AWS Lambda Integration using Rack." align="right" /><h3>Simple Rails & AWS Lambda Integration using Rack</h3>
4
+ <h2>Simple Rails &amp; AWS Lambda Integration</h2>
5
5
 
6
- The goal of this project is to provide minimal code to convert API Gateway `event` and `context` objects into Rack events for your Rails application in a Lambda handler. Most everything else is [documentation](https://github.com/customink/lamby/issues?q=is%3Aissue+is%3Aopen+label%3Adocs+sort%3Acreated-asc).
6
+ <img src="https://user-images.githubusercontent.com/2381/59363668-89edeb80-8d03-11e9-9985-2ce14361b7e3.png" alt="Lamby: Simple Rails & AWS Lambda Integration" align="right" />
7
7
 
8
- ```ruby
9
- def handler(event:, context:)
10
- Lamby.handler $app, event, context
11
- end
12
- ```
13
-
14
-
15
- ## Getting Started
16
-
17
- A simple Hello Rails application example using Lamby.
18
-
19
- #### Installing AWS CLI & SAM
20
-
21
- This is fairly easy on Macs using Homebrew. For other platforms or first time installers, please follow the [full guides](https://github.com/customink/lamby/issues/18) and configure your AWS CLI before proceeding.
22
-
23
- ```shell
24
- $ brew install awscli
25
- $ brew tap aws/tap
26
- $ brew install aws-sam-cli
27
- ```
28
-
29
- #### New Rails Application
30
-
31
- Lamby works with existing Rails projects. In theory, any version. In our getting started example, let's create a new application. Here we skip any frameworks not needed, notable ActiveRecord.
32
-
33
- ```shell
34
- $ rbenv local 2.5.5
35
- $ gem install rails -v 6.0.0.rc1
36
- $ rails new my_awesome_lambda \
37
- --skip-action-mailer --skip-action-mailbox --skip-action-text \
38
- --skip-active-record --skip-active-storage --skip-puma \
39
- --skip-action-cable --skip-spring --skip-listen --skip-turbolinks \
40
- --skip-system-test --skip-bootsnap --skip-webpack-install
41
- $ cd my_awesome_lambda
42
- ```
8
+ <p>&nbsp;</p>
43
9
 
44
- Add a root to the `routes.rb` file which maps to an `index` action in the application controller and return a simple Hello Rails H1 tag.
10
+ The goal of this project is to provide minimal code to allow your Rails application to respond to incoming [AWS Lambda Function Handler in Ruby](https://docs.aws.amazon.com/lambda/latest/dg/ruby-handler.html) `event` and `context` objects in the Lambda handler. We support integration with API Gateway and are currently working on Application Load Balancer support.
45
11
 
46
12
  ```ruby
47
- Rails.application.routes.draw do
48
- root to: 'application#index'
49
- end
50
-
51
- class ApplicationController < ActionController::Base
52
- def index
53
- render html: '<h1>Hello Rails</h1>'.html_safe
54
- end
13
+ def handler(event:, context:)
14
+ Lamby.handler $app, event, context
55
15
  end
56
16
  ```
57
17
 
58
- #### Add The Gem
59
-
60
- Add the Lamby gem to your Rails project's `Gemfile`. Recommend only requiring Lamby in your `app.rb` so your local development or test environment logs still work.
61
-
62
- ```ruby
63
- gem 'lamby', require: false
64
- gem 'aws-sdk-ssm'
65
- ```
66
-
67
- #### Install Needed Files
68
-
69
- Lamby provides a simple Rake task to install the files needed for AWS Lambda to run your application.
70
-
71
- ```shell
72
- $ ./bin/rake -r lamby lamby:install:api_gateway
73
- ```
74
-
75
- This will install the following files.
76
-
77
- * `app.rb` - Entry point for the Lambda handler.
78
- * `template.yaml` - Your AWS Serverless Application Model ([SAM](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md)) template.
79
- * `bin/build` - Configurable bash script to build your project with SAM.
80
- * `bin/deploy` - Configurable bash script to deploy your app to with SAM.
81
-
82
- #### Create CloudFormation S3 Bucket
83
-
84
- The default name for the bucket in the `deploy` script would include your computer's username. You can change this in your script (WHICH WE RECOMMEND) or use the `CLOUDFORMATION_BUCKET` environment varable. Assuming you stick with the default:
85
-
86
- ```shell
87
- aws s3 mb "s3://lamby.cloudformation.$(whoami)"
88
- ```
89
-
90
- #### Configuration
91
-
92
- We recommend getting started using [Rails Credentials](https://guides.rubyonrails.org/security.html#environmental-security) by setting the `RAILS_MASTER_KEY` environment variable in your `app.rb` file. The value be read from AWS Systems Manager [Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html) which Lamby has a client wrapper for. To set the master key value, use the following AWS CLI command.
93
-
94
- ```shell
95
- aws ssm put-parameter \
96
- --name "/config/my_awesome_lambda/env/RAILS_MASTER_KEY" \
97
- --type "SecureString" \
98
- --value $(cat config/master.key)
99
- ```
100
-
101
- Update your `app.rb` file and add this line right after `require 'lamby'`.
102
-
103
- ```ruby
104
- ENV['RAILS_MASTER_KEY'] =
105
- Lamby::SsmParameterStore.get!('/config/my_awesome_lambda/env/RAILS_MASTER_KEY')
106
- ```
107
-
108
- Finally, updated your `template.yaml` CloudFormation/SAM file by adding this to the `Properties` section of your `RailsFunction`. This addition allows your Lambda's runtime policy to read configs from SSM Parameter store.
109
-
110
- ```yaml
111
- Policies:
112
- - Version: "2012-10-17"
113
- Statement:
114
- - Effect: Allow
115
- Action:
116
- - ssm:GetParameter
117
- - ssm:GetParameters
118
- - ssm:GetParametersByPath
119
- - ssm:GetParameterHistory
120
- Resource:
121
- - !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/config/my_awesome_lambda/*
122
- ```
123
-
124
- #### First Deploy!
125
-
126
- Now your Rails app is ready to be deployed to AWS Lambda via CloudFormation & SAM.
127
-
128
- ```shell
129
- $ ./bin/deploy
130
- ```
131
-
132
- To see your newly created Lambda's API Gateway URL, log into the AWS Console or run the following command. This will describe the CloudFormation stack we just deployed and the `Outputs` from that template.
133
-
134
- ```shell
135
- aws cloudformation describe-stacks \
136
- --stack-name "awesomebotlambda-production-us-east-1" | \
137
- grep OutputValue | \
138
- grep https
139
- ```
140
-
141
-
142
- ## Additional Documentation
143
-
144
- In order to provide minimal code and ultimate flexibility for any type of Rails application on Lambda, optional or advanced features require that you write additional CloudFormation code or perform AWS Console actions to get the desired results.
145
-
146
- To that end, we are using our GitHub issues along with the `[docs]` label as a project-focused Stack Overflow where we encourage you to participate in and ask questions. Here are a few high level docs now that may interests most users. Also, [browse all docs](https://github.com/customink/lamby/issues?q=is%3Aissue+is%3Aopen+label%3Adocs) or open a new `[question]` issue and we would be glad to help!
147
-
148
- #### Local AWS Dependencies
149
-
150
- * [Installing AWS CLI and AWS SAM](https://github.com/customink/lamby/issues/18)
151
- * [Bin Scripts - Setup, Build, Server, & Deploy](https://github.com/customink/lamby/issues/17)
152
-
153
- #### Other Guides Just For You!
154
-
155
- * [Environments & Configuration](https://github.com/customink/lamby/issues/28)
156
- * [Asset Hosts & Precompiling](https://github.com/customink/lamby/issues/29)
157
- * [Custom Domain Name, Edge/Regional, & CloudFront](https://github.com/customink/lamby/issues/10)
158
- * [API Gateway Permissions & CloudWatch Logs](https://github.com/customink/lamby/issues/6)
159
- * [How Does Lamby Work?](https://github.com/customink/lamby/issues/12)
160
- * [Performance & Real World Usage](https://github.com/customink/lamby/issues/16)
161
- * [Database Connections](https://github.com/customink/lamby/issues/13)
162
- * [Basic Ruby with Lambda & AWS SAM](https://github.com/customink/lamby/issues/14)
163
- * [Docker Perf on Mac & Root Proxy SAM Bugs](https://github.com/customink/lamby/issues/15)
18
+ ## Quick Start
164
19
 
20
+ https://lamby.custominktech.com/docs/quick_start
165
21
 
166
- ## About AWS SAM and CloudFormation
167
22
 
168
- AWS SAM is shorthand for the [Serverless Application Model](https://github.com/awslabs/serverless-application-model) and it is a superset of CloudFormation - a language that describes and provisions your infrastructure using code. As your application grows and requires additional AWS resources, learning how to express this in your `template.yaml` is critical. We recommend the following links when needing to learn both SAM and CloudFormation.
23
+ ## Full Documentation
169
24
 
170
- * [AWS Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md)
171
- * [AWS CloudFormation User Guide](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)
25
+ https://lamby.custominktech.com/docs/installing_aws_sam
172
26
 
173
27
 
174
28
  ## Contributing
data/bin/setup CHANGED
@@ -3,5 +3,4 @@
3
3
  set -e
4
4
 
5
5
  bundle install
6
-
7
6
  pipenv install
@@ -4,7 +4,8 @@ require 'base64'
4
4
  require 'active_support/all'
5
5
  require 'lamby/version'
6
6
  require 'lamby/sam_helpers'
7
- require 'lamby/rack'
7
+ require 'lamby/rack_alb'
8
+ require 'lamby/rack_api'
8
9
  require 'lamby/debug'
9
10
  require 'lamby/handler'
10
11
  require 'rails/railtie'
@@ -14,8 +15,8 @@ module Lamby
14
15
 
15
16
  extend self
16
17
 
17
- def handler(app, event, context)
18
- Handler.call(app, event, context)
18
+ def handler(app, event, context, options = {})
19
+ Handler.call(app, event, context, options)
19
20
  end
20
21
 
21
22
  autoload :SsmParameterStore, 'lamby/ssm_parameter_store'
@@ -3,17 +3,17 @@ module Lamby
3
3
 
4
4
  class << self
5
5
 
6
- def call(app, event, context)
7
- new(app, event, context).call.response
6
+ def call(app, event, context, options = {})
7
+ new(app, event, context, options).call.response
8
8
  end
9
9
 
10
10
  end
11
11
 
12
- def initialize(app, event, context)
12
+ def initialize(app, event, context, options = {})
13
13
  @app = app
14
14
  @event = event
15
15
  @context = context
16
- @rack = Lamby::Rack.new event, context
16
+ @options = options
17
17
  @called = false
18
18
  end
19
19
 
@@ -46,11 +46,20 @@ module Lamby
46
46
 
47
47
  private
48
48
 
49
+ def rack
50
+ @rack ||= case @options[:rack]
51
+ when :api
52
+ Lamby::RackApi.new @event, @context
53
+ else
54
+ Lamby::RackAlb.new @event, @context
55
+ end
56
+ end
57
+
49
58
  def call_app
50
59
  if Debug.on?(@event)
51
- Debug.call @event, @context, @rack.env
60
+ Debug.call @event, @context, rack.env
52
61
  else
53
- @app.call @rack.env
62
+ @app.call rack.env
54
63
  end
55
64
  end
56
65
 
@@ -0,0 +1,90 @@
1
+ module Lamby
2
+ class RackAlb
3
+
4
+ include SamHelpers
5
+
6
+ LAMBDA_EVENT = 'lambda.event'.freeze
7
+ LAMBDA_CONTEXT = 'lambda.context'.freeze
8
+ HTTP_X_REQUESTID = 'HTTP_X_REQUEST_ID'.freeze
9
+
10
+ attr_reader :event, :context
11
+
12
+ def initialize(event, context)
13
+ @event = event
14
+ @context = context
15
+ end
16
+
17
+ def env
18
+ @env ||= env_base.merge!(env_headers)
19
+ end
20
+
21
+ private
22
+
23
+ def env_base
24
+ { ::Rack::REQUEST_METHOD => event['httpMethod'],
25
+ ::Rack::SCRIPT_NAME => '',
26
+ ::Rack::PATH_INFO => event['path'] || '',
27
+ ::Rack::QUERY_STRING => query_string,
28
+ ::Rack::SERVER_NAME => headers['host'],
29
+ ::Rack::SERVER_PORT => headers['x-forwarded-port'],
30
+ ::Rack::SERVER_PROTOCOL => 'HTTP/1.1',
31
+ ::Rack::RACK_VERSION => ::Rack::VERSION,
32
+ ::Rack::RACK_URL_SCHEME => headers['x-forwarded-proto'],
33
+ ::Rack::RACK_INPUT => StringIO.new(body || ''),
34
+ ::Rack::RACK_ERRORS => $stderr,
35
+ ::Rack::RACK_MULTITHREAD => false,
36
+ ::Rack::RACK_MULTIPROCESS => false,
37
+ ::Rack::RACK_RUNONCE => false,
38
+ LAMBDA_EVENT => event,
39
+ LAMBDA_CONTEXT => context
40
+ }.tap do |env|
41
+ ct = content_type
42
+ cl = content_length
43
+ env['CONTENT_TYPE'] = ct if ct
44
+ env['CONTENT_LENGTH'] = cl if cl
45
+ end
46
+ end
47
+
48
+ def env_headers
49
+ headers.transform_keys do |key|
50
+ "HTTP_#{key.to_s.upcase.tr '-', '_'}"
51
+ end.tap do |hdrs|
52
+ hdrs[HTTP_X_REQUESTID] = request_id
53
+ end
54
+ end
55
+
56
+ def content_type
57
+ headers.delete('Content-Type') || headers.delete('content-type') || headers.delete('CONTENT_TYPE')
58
+ end
59
+
60
+ def content_length
61
+ bytesize = body.bytesize.to_s if body
62
+ headers.delete('Content-Length') || headers.delete('content-length') || headers.delete('CONTENT_LENGTH') || bytesize
63
+ end
64
+
65
+ def body
66
+ @body ||= if event['body'] && base64_encoded?
67
+ Base64.decode64 event['body']
68
+ else
69
+ event['body']
70
+ end
71
+ end
72
+
73
+ def headers
74
+ event['headers'] || {}
75
+ end
76
+
77
+ def query_string
78
+ @query_string ||= event['queryStringParameters'].try(:to_query)
79
+ end
80
+
81
+ def base64_encoded?
82
+ event['isBase64Encoded']
83
+ end
84
+
85
+ def request_id
86
+ context.aws_request_id
87
+ end
88
+
89
+ end
90
+ end
@@ -1,5 +1,5 @@
1
1
  module Lamby
2
- class Rack
2
+ class RackApi
3
3
 
4
4
  include SamHelpers
5
5
 
@@ -1,5 +1,6 @@
1
1
  installers = {
2
- 'API Gateway': :api_gateway
2
+ 'API Gateway' => :api_gateway,
3
+ 'Application Load Balancer' => :application_load_balancer
3
4
  }.freeze
4
5
 
5
6
  namespace :lamby do
@@ -31,7 +32,5 @@ namespace :lamby do
31
32
  end
32
33
 
33
34
  desc "Install Lamby files for #{installers.first.first}"
34
- task install: 'install:api_gateway'
35
+ task install: 'install:application_load_balancer'
35
36
  end
36
-
37
-
@@ -16,9 +16,9 @@ append_to_file app_file('.gitignore'), <<-GITIGNORE.strip_heredoc
16
16
  GITIGNORE
17
17
 
18
18
  say 'Creating ./bin files for build and deploy...'
19
- copy_file shr_file('build'), app_file('bin/build')
19
+ copy_file tpl_file('build'), app_file('bin/build')
20
20
  chmod app_file('bin/build'), 0755
21
- copy_file shr_file('deploy'), app_file('bin/deploy')
21
+ copy_file tpl_file('deploy'), app_file('bin/deploy')
22
22
  chmod app_file('bin/deploy'), 0755
23
23
  gsub_file app_file('bin/deploy'), /APPNAMEHERE/, appname.downcase
24
24
 
@@ -6,5 +6,5 @@ require_relative 'config/environment'
6
6
  $app = Rack::Builder.new { run Rails.application }.to_app
7
7
 
8
8
  def handler(event:, context:)
9
- Lamby.handler $app, event, context
9
+ Lamby.handler $app, event, context, rack: :api
10
10
  end
@@ -1,6 +1,6 @@
1
1
  AWSTemplateFormatVersion: '2010-09-09'
2
2
  Transform: AWS::Serverless-2016-10-31
3
- Description: APPNAMEHERE Lambda
3
+ Description: APPNAMEHERE Lambda (API)
4
4
 
5
5
  Parameters:
6
6
 
@@ -57,7 +57,7 @@ Resources:
57
57
  Environment:
58
58
  Variables:
59
59
  RAILS_ENV: !Ref RailsEnv
60
- FunctionName: !Join [ '', [ 'myapp-', !Ref RailsEnv, '-', !Ref 'AWS::Region' ] ]
60
+ FunctionName: !Join [ '', [ 'APPNAMEHERE-', !Ref RailsEnv, '-', !Ref 'AWS::Region' ] ]
61
61
  Events:
62
62
  Root:
63
63
  Type: Api
@@ -0,0 +1,25 @@
1
+ $LAMBY_INSTALLER_NAME = 'application_load_balancer'
2
+ load 'lamby/templates/shared.rb'
3
+
4
+ say '==> Running Lamby Application Load Balancer installer...'
5
+
6
+ say 'Copying files...'
7
+ copy_file tpl_file('app.rb'), app_file('app.rb')
8
+ copy_file tpl_file('template.yaml'), app_file('template.yaml')
9
+ gsub_file app_file('template.yaml'), /APPNAMEHERE/, appname
10
+
11
+ say 'Adding to .gitignore...'
12
+ FileUtils.touch app_file('.gitignore')
13
+ append_to_file app_file('.gitignore'), <<-GITIGNORE.strip_heredoc
14
+ # Lamby
15
+ /.aws-sam
16
+ GITIGNORE
17
+
18
+ say 'Creating ./bin files for build and deploy...'
19
+ copy_file tpl_file('build'), app_file('bin/build')
20
+ chmod app_file('bin/build'), 0755
21
+ copy_file tpl_file('deploy'), app_file('bin/deploy')
22
+ chmod app_file('bin/deploy'), 0755
23
+ gsub_file app_file('bin/deploy'), /APPNAMEHERE/, appname.downcase
24
+
25
+ say 'Welcome to AWS Lambda and Rails 🎉', :green
@@ -0,0 +1,10 @@
1
+ require_relative 'config/boot'
2
+ require 'lamby'
3
+ require_relative 'config/application'
4
+ require_relative 'config/environment'
5
+
6
+ $app = Rack::Builder.new { run Rails.application }.to_app
7
+
8
+ def handler(event:, context:)
9
+ Lamby.handler $app, event, context, rack: :alb
10
+ end
@@ -0,0 +1,23 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ # Clean any previous bundle/builds.
5
+ rm -rf ./.aws-sam/build
6
+
7
+ # Ensure native extensions built for platform.
8
+ sam build --use-container
9
+
10
+ # [HOOK] Environments & Configuration
11
+ # https://github.com/customink/lamby/issues/28
12
+
13
+ # [HOOK] Asset Hosts & Precompiling
14
+ # https://github.com/customink/lamby/issues/29
15
+
16
+ # Clean un-needed artifacts.
17
+ pushd ./.aws-sam/build/RailsFunction/
18
+ rm -rf .aws-sam \
19
+ .git \
20
+ log \
21
+ test \
22
+ tmp
23
+ popd
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ export RAILS_ENV=${RAILS_ENV:="production"}
5
+ export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:=us-east-1}
6
+ export CLOUDFORMATION_BUCKET=${CLOUDFORMATION_BUCKET:="lamby.cloudformation.$(whoami)"}
7
+
8
+ ./bin/build
9
+
10
+ export VPCID=${VPCID:=$(
11
+ aws ec2 describe-vpcs \
12
+ --output text \
13
+ --filters 'Name=state,Values=available' \
14
+ --query 'Vpcs[0].VpcId'
15
+ )}
16
+
17
+ export SUBNETS=${SUBNETS:=$(
18
+ aws ec2 describe-subnets \
19
+ --output text \
20
+ --filters "Name=state,Values=available,Name=vpc-id,Values=$VPCID" \
21
+ --query 'Subnets[*].SubnetId' | \
22
+ tr -s '[:blank:]' ','
23
+ )}
24
+
25
+ sam package \
26
+ --region ${AWS_DEFAULT_REGION} \
27
+ --template-file ./.aws-sam/build/template.yaml \
28
+ --output-template-file ./.aws-sam/build/packaged.yaml \
29
+ --s3-bucket $CLOUDFORMATION_BUCKET \
30
+ --s3-prefix "APPNAMEHERE-${RAILS_ENV}"
31
+
32
+ sam deploy \
33
+ --template-file ./.aws-sam/build/packaged.yaml \
34
+ --stack-name "APPNAMEHERE-${RAILS_ENV}-${AWS_DEFAULT_REGION}" \
35
+ --capabilities "CAPABILITY_IAM" \
36
+ --parameter-overrides \
37
+ RailsEnv=${RAILS_ENV} \
38
+ VpcId=${VPCID} \
39
+ Subnets=${SUBNETS}
@@ -0,0 +1,86 @@
1
+ AWSTemplateFormatVersion: '2010-09-09'
2
+ Transform: AWS::Serverless-2016-10-31
3
+ Description: APPNAMEHERE Lambda (ALB)
4
+
5
+ Parameters:
6
+
7
+ RailsEnv:
8
+ Type: String
9
+ Default: production
10
+ AllowedValues:
11
+ - staging
12
+ - production
13
+ VpcId:
14
+ Type: AWS::EC2::VPC::Id
15
+ Subnets:
16
+ Type: List<AWS::EC2::Subnet::Id>
17
+
18
+ Resources:
19
+
20
+ RailsLoadBalancer:
21
+ Type: AWS::ElasticLoadBalancingV2::LoadBalancer
22
+ Properties:
23
+ Name: APPNAMEHERE
24
+ Scheme: internet-facing
25
+ Subnets: !Ref Subnets
26
+ SecurityGroups:
27
+ - !Ref RailsSecurityGroup
28
+
29
+ RailsLoadBalancerTargetGroup:
30
+ Type: AWS::ElasticLoadBalancingV2::TargetGroup
31
+ DependsOn: RailsFunctionInvokePermission
32
+ Properties:
33
+ TargetType: lambda
34
+ Targets:
35
+ - Id: !GetAtt RailsFunction.Arn
36
+
37
+ RailsLoadBalancerHttpListener:
38
+ Type: 'AWS::ElasticLoadBalancingV2::Listener'
39
+ Properties:
40
+ DefaultActions:
41
+ - TargetGroupArn: !Ref RailsLoadBalancerTargetGroup
42
+ Type: forward
43
+ LoadBalancerArn: !Ref RailsLoadBalancer
44
+ Port: 80
45
+ Protocol: HTTP
46
+
47
+ RailsSecurityGroup:
48
+ Type: AWS::EC2::SecurityGroup
49
+ Properties:
50
+ GroupDescription: Allow HTTP
51
+ VpcId: !Ref VpcId
52
+ SecurityGroupIngress:
53
+ - IpProtocol: tcp
54
+ FromPort: 80
55
+ ToPort: 80
56
+ CidrIp: 0.0.0.0/0
57
+
58
+ RailsFunctionInvokePermission:
59
+ Type: AWS::Lambda::Permission
60
+ Properties:
61
+ FunctionName: !GetAtt RailsFunction.Arn
62
+ Action: 'lambda:InvokeFunction'
63
+ Principal: elasticloadbalancing.amazonaws.com
64
+
65
+ RailsFunction:
66
+ Type: AWS::Serverless::Function
67
+ Properties:
68
+ CodeUri: .
69
+ Handler: app.handler
70
+ Runtime: ruby2.5
71
+ MemorySize: 512
72
+ Timeout: 30
73
+ Environment:
74
+ Variables:
75
+ RAILS_ENV: !Ref RailsEnv
76
+ FunctionName: !Join [ '', [ 'APPNAMEHERE-', !Ref RailsEnv, '-', !Ref 'AWS::Region' ] ]
77
+ AutoPublishAlias: live
78
+
79
+ Outputs:
80
+
81
+ RailsLoadBalancerDNSNAme:
82
+ Value: !GetAtt RailsLoadBalancer.DNSName
83
+
84
+ RailsFunctionArn:
85
+ Description: Lambda ARN
86
+ Value: !GetAtt RailsFunction.Arn
@@ -10,10 +10,6 @@ def tpl_file(path)
10
10
  "#{__dir__}/#{inst_name}/#{path}"
11
11
  end
12
12
 
13
- def shr_file(path)
14
- "#{__dir__}/shared/#{path}"
15
- end
16
-
17
13
  def appname
18
14
  Rails.application.class.parent.name
19
15
  end
@@ -1,3 +1,3 @@
1
1
  module Lamby
2
- VERSION = '0.5.1'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lamby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-29 00:00:00.000000000 Z
11
+ date: 2019-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -167,7 +167,8 @@ files:
167
167
  - lib/lamby/debug.rb
168
168
  - lib/lamby/handler.rb
169
169
  - lib/lamby/logger.rb
170
- - lib/lamby/rack.rb
170
+ - lib/lamby/rack_alb.rb
171
+ - lib/lamby/rack_api.rb
171
172
  - lib/lamby/railtie.rb
172
173
  - lib/lamby/sam_helpers.rb
173
174
  - lib/lamby/ssm_parameter_store.rb
@@ -175,10 +176,15 @@ files:
175
176
  - lib/lamby/templates.rake
176
177
  - lib/lamby/templates/api_gateway.rb
177
178
  - lib/lamby/templates/api_gateway/app.rb
179
+ - lib/lamby/templates/api_gateway/build
180
+ - lib/lamby/templates/api_gateway/deploy
178
181
  - lib/lamby/templates/api_gateway/template.yaml
182
+ - lib/lamby/templates/application_load_balancer.rb
183
+ - lib/lamby/templates/application_load_balancer/app.rb
184
+ - lib/lamby/templates/application_load_balancer/build
185
+ - lib/lamby/templates/application_load_balancer/deploy
186
+ - lib/lamby/templates/application_load_balancer/template.yaml
179
187
  - lib/lamby/templates/shared.rb
180
- - lib/lamby/templates/shared/build
181
- - lib/lamby/templates/shared/deploy
182
188
  - lib/lamby/version.rb
183
189
  homepage: https://github.com/customink/lamby
184
190
  licenses: