lamby 2.2.1 → 2.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -0
  3. data/Gemfile.lock +2 -2
  4. data/lib/lamby/debug.rb +1 -1
  5. data/lib/lamby/handler.rb +5 -0
  6. data/lib/lamby/ssm_parameter_store.rb +1 -1
  7. data/lib/lamby/templates/alb.rb +10 -2
  8. data/lib/lamby/templates/alb/Dockerfile +3 -0
  9. data/lib/lamby/templates/alb/Dockerfile-build +15 -0
  10. data/lib/lamby/templates/alb/_build +40 -0
  11. data/lib/lamby/templates/alb/_deploy +52 -0
  12. data/lib/lamby/templates/alb/app.rb +2 -0
  13. data/lib/lamby/templates/alb/deploy +5 -35
  14. data/lib/lamby/templates/alb/docker-compose.yml +17 -0
  15. data/lib/lamby/templates/alb/template.yaml +11 -11
  16. data/lib/lamby/templates/http.rb +10 -2
  17. data/lib/lamby/templates/http/Dockerfile +3 -0
  18. data/lib/lamby/templates/http/Dockerfile-build +15 -0
  19. data/lib/lamby/templates/http/_build +40 -0
  20. data/lib/lamby/templates/http/_deploy +35 -0
  21. data/lib/lamby/templates/http/app.rb +2 -0
  22. data/lib/lamby/templates/http/deploy +5 -18
  23. data/lib/lamby/templates/http/docker-compose.yml +17 -0
  24. data/lib/lamby/templates/http/template.yaml +7 -7
  25. data/lib/lamby/templates/rest.rb +10 -2
  26. data/lib/lamby/templates/rest/Dockerfile +3 -0
  27. data/lib/lamby/templates/rest/Dockerfile-build +15 -0
  28. data/lib/lamby/templates/rest/_build +40 -0
  29. data/lib/lamby/templates/rest/_deploy +35 -0
  30. data/lib/lamby/templates/rest/app.rb +2 -0
  31. data/lib/lamby/templates/rest/deploy +5 -18
  32. data/lib/lamby/templates/rest/docker-compose.yml +17 -0
  33. data/lib/lamby/templates/rest/template.yaml +9 -9
  34. data/lib/lamby/version.rb +1 -1
  35. metadata +17 -5
  36. data/lib/lamby/templates/alb/build +0 -23
  37. data/lib/lamby/templates/http/build +0 -23
  38. data/lib/lamby/templates/rest/build +0 -23
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76c0182c6acf69bd8342ddd73b09e793684f3b12da095f212637ced6cabed916
4
- data.tar.gz: 9718fcac74658d3979572e65dfe55a26c123bc606afd868bab808622bdef13a2
3
+ metadata.gz: 1e7a5dca080f6ad01223f4fb9de1284240d8c3512b0a7790d97bcd4461103ca8
4
+ data.tar.gz: 2341bada28a7832cd0035ab03bfbe18b69451864d8e1917c93a64710513708ec
5
5
  SHA512:
6
- metadata.gz: 5c8240b4a45558396837cfd78b12e7528ab7441e4a4d9b69b8cab0ed90b8c13074cc8ba504827ef4f08aa8847283334b89b37e775d7c820edd636c7807a0c171
7
- data.tar.gz: 4be0113b2ff936cd3e9e90cb641d685e45ad93c7a02c4c5cc6a5f24adc8ee8685599c0df4db1be9770b57f4d3d450a574b376fe1815e044b2086babd4b797851
6
+ metadata.gz: 8d9396e54730b3bf3c414d141845a71897b92ffa26a336fd7ca68cc0a8c4c1f6b56e22aadbb008d8838670dbb34878ad7a73087aa528c5edca0947e484774e72
7
+ data.tar.gz: 19bf131edee90db7a82b3d6b98cb302639c3cb8d150f595e5c2e2e3258b8bd4ae96b2a2855dccfb8c188634576e10a4bbfae0fffe7e3c2bcae12c12684ded2b9
@@ -3,6 +3,34 @@
3
3
 
4
4
  See this http://keepachangelog.com link for information on how we want this documented formatted.
5
5
 
6
+ ## v2.5.3
7
+
8
+ #### Fixed
9
+
10
+ * Base64 encode response body if the rack response is gzip or brotli compressed.
11
+
12
+ ## v2.5.2
13
+
14
+ * SSM file always overwrites. Fixes #65.
15
+
16
+ ## v2.5.1
17
+
18
+ #### Fixed
19
+
20
+ * Quoting in describe-subnets #62 Thanks @atwoodjw
21
+
22
+ ## v2.5.0
23
+
24
+ #### Changed
25
+
26
+ * Install files to favor containers.
27
+
28
+ ## v2.2.2
29
+
30
+ #### Changed
31
+
32
+ * More ActiveSupport removal. Better ENV.to_h.
33
+
6
34
  ## v2.2.1
7
35
 
8
36
  #### Changed
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lamby (2.2.1)
4
+ lamby (2.5.3)
5
5
  rack
6
6
 
7
7
  GEM
@@ -166,4 +166,4 @@ DEPENDENCIES
166
166
  rake
167
167
 
168
168
  BUNDLED WITH
169
- 2.1.4
169
+ 2.2.7
@@ -43,7 +43,7 @@ module Lamby
43
43
  end
44
44
 
45
45
  def development?
46
- ENV
46
+ ENV.to_h
47
47
  .slice('RACK_ENV', 'RAILS_ENV')
48
48
  .values
49
49
  .any? { |v| v.to_s.casecmp('development').zero? }
@@ -47,6 +47,7 @@ module Lamby
47
47
  def base64_encodeable?
48
48
  @headers && (
49
49
  @headers['Content-Transfer-Encoding'] == 'binary' ||
50
+ content_encoding_compressed? ||
50
51
  @headers['X-Lamby-Base64'] == '1'
51
52
  )
52
53
  end
@@ -80,5 +81,9 @@ module Lamby
80
81
  end
81
82
  end
82
83
 
84
+ def content_encoding_compressed?
85
+ content_encoding_header = @headers['Content-Encoding'] || ''
86
+ content_encoding_header.split(', ').any? { |h| ['br', 'gzip'].include?(h) }
87
+ end
83
88
  end
84
89
  end
@@ -40,7 +40,7 @@ module Lamby
40
40
  end
41
41
 
42
42
  def to_dotenv
43
- File.open(dotenv_file, 'w') { |f| f.write(dotenv_contents) }
43
+ File.open(dotenv_file, 'a') { |f| f.write(dotenv_contents) }
44
44
  end
45
45
 
46
46
  def get!
@@ -7,19 +7,27 @@ say 'Copying files...'
7
7
  copy_file tpl_file('app.rb'), app_file('app.rb')
8
8
  copy_file tpl_file('template.yaml'), app_file('template.yaml')
9
9
  gsub_file app_file('template.yaml'), /APPNAMEHERE/, appname
10
+ copy_file tpl_file('Dockerfile'), app_file('Dockerfile')
11
+ copy_file tpl_file('Dockerfile-build'), app_file('Dockerfile-build')
12
+ copy_file tpl_file('docker-compose.yml'), app_file('docker-compose.yml')
10
13
 
11
14
  say 'Adding to .gitignore...'
12
15
  FileUtils.touch app_file('.gitignore')
13
16
  append_to_file app_file('.gitignore'), <<~GITIGNORE
14
17
  # Lamby
15
18
  /.aws-sam
19
+ /.lamby
16
20
  GITIGNORE
17
21
 
18
22
  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
23
+ copy_file tpl_file('_build'), app_file('bin/_build')
24
+ gsub_file app_file('bin/_build'), /APPNAMEHERE/, appname.downcase
25
+ chmod app_file('bin/_build'), 0755
21
26
  copy_file tpl_file('deploy'), app_file('bin/deploy')
22
27
  chmod app_file('bin/deploy'), 0755
28
+ copy_file tpl_file('_deploy'), app_file('bin/_deploy')
29
+ gsub_file app_file('bin/_deploy'), /APPNAMEHERE/, appname.downcase
30
+ chmod app_file('bin/_deploy'), 0755
23
31
  gsub_file app_file('bin/deploy'), /APPNAMEHERE/, appname.downcase
24
32
 
25
33
  say 'Welcome to AWS Lambda and Rails 🎉', :green
@@ -0,0 +1,3 @@
1
+ FROM public.ecr.aws/lambda/ruby:2.7
2
+ COPY . .
3
+ CMD ["app.handler"]
@@ -0,0 +1,15 @@
1
+ FROM amazon/aws-sam-cli-build-image-ruby2.7
2
+
3
+ # Ensure minimum required SAM version.
4
+ # RUN pip install awscli && \
5
+ # pip uninstall --yes aws-sam-cli && \
6
+ # pip install aws-sam-cli==v1.13.2
7
+
8
+ # Node for JavaScript.
9
+ # RUN curl -sL https://rpm.nodesource.com/setup_12.x | bash - && \
10
+ # yum install -y nodejs && \
11
+ # curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo && \
12
+ # rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg && \
13
+ # yum install -y yarn
14
+
15
+ WORKDIR /var/task
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ echo '== Simulate SAM Build Directory =='
5
+ RESOURCE="RailsLambda"
6
+ SAM_TEMP=$(mktemp -d -t sam.XXXXXX)
7
+ rm -rf ./.lamby ./.aws-sam
8
+ cp -r . "$SAM_TEMP"
9
+ mkdir -p ./.lamby
10
+ cp -r "$SAM_TEMP" "./.lamby/$RESOURCE"
11
+ pushd "./.lamby/$RESOURCE"
12
+
13
+ echo '== Cleaning Dev Dependencies =='
14
+ rm -rf ./.bundle \
15
+ ./vendor/bundle
16
+
17
+ echo '== Bundle For Deployment =='
18
+ bundle config --local deployment true
19
+ bundle config --local without 'development test'
20
+ bundle config --local path './vendor/bundle'
21
+ bundle install --quiet --jobs 4
22
+
23
+ echo "== Environments & Configuration =="
24
+ # ./bin/rails \
25
+ # -rlamby \
26
+ # lamby:ssm:dotenv \
27
+ # LAMBY_SSM_PARAMS_PATH="/APPNAMEHERE/${RAILS_ENV}/env"
28
+
29
+ echo "== Asset Hosts & Precompiling =="
30
+ # NODE_ENV='production' \
31
+ # RAILS_GROUPS=assets \
32
+ # ./bin/rails assets:precompile
33
+
34
+ echo "== Cleanup Unused Files & Directories =="
35
+ rm -rf \
36
+ log \
37
+ node_modules \
38
+ test \
39
+ tmp \
40
+ vendor/bundle/ruby/2.7.0/cache
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env 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
+ # https://github.com/aws/aws-sam-cli/issues/2447
9
+ export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
10
+ export IMAGE_REPOSITORY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/myorg/APPNAMEHERE"
11
+
12
+ ./bin/_build
13
+
14
+ export VPCID=${VPCID:=$(
15
+ aws ec2 describe-vpcs \
16
+ --output text \
17
+ --filters 'Name=state,Values=available' \
18
+ --query 'Vpcs[0].VpcId'
19
+ )}
20
+
21
+ export SUBNETS=${SUBNETS:=$(
22
+ aws ec2 describe-subnets \
23
+ --output text \
24
+ --filters 'Name=state,Values=available' "Name=vpc-id,Values=$VPCID" \
25
+ --query 'Subnets[*].SubnetId' | \
26
+ tr -s '[:blank:]' ','
27
+ )}
28
+
29
+ sam build \
30
+ --parameter-overrides \
31
+ RailsEnv="${RAILS_ENV}"
32
+
33
+ echo "== SAM package..."
34
+ sam package \
35
+ --region "$AWS_DEFAULT_REGION" \
36
+ --template-file ./.aws-sam/build/template.yaml \
37
+ --output-template-file ./.aws-sam/build/packaged.yaml \
38
+ --image-repository "$IMAGE_REPOSITORY" \
39
+ --s3-bucket "${CLOUDFORMATION_BUCKET}" \
40
+ --s3-prefix "APPNAMEHERE-${RAILS_ENV}"
41
+
42
+ echo "== SAM deploy..."
43
+ sam deploy \
44
+ --region "$AWS_DEFAULT_REGION" \
45
+ --template-file ./.aws-sam/build/packaged.yaml \
46
+ --stack-name "APPNAMEHERE-${RAILS_ENV}" \
47
+ --image-repository "$IMAGE_REPOSITORY" \
48
+ --capabilities "CAPABILITY_IAM" \
49
+ --parameter-overrides \
50
+ RailsEnv="${RAILS_ENV}" \
51
+ VpcId=${VPCID} \
52
+ Subnets=${SUBNETS}
@@ -1,4 +1,6 @@
1
+ ENV['RAILS_SERVE_STATIC_FILES'] = '1'
1
2
  require_relative 'config/boot'
3
+ require 'dotenv' ; Dotenv.load ".env.#{ENV['RAILS_ENV']}"
2
4
  require 'lamby'
3
5
  require_relative 'config/application'
4
6
  require_relative 'config/environment'
@@ -1,39 +1,9 @@
1
1
  #!/usr/bin/env bash
2
2
  set -e
3
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)"}
4
+ export RAILS_ENV=${RAILS_ENV:=production}
7
5
 
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}
6
+ docker-compose run \
7
+ -e CLOUDFORMATION_BUCKET \
8
+ cicd \
9
+ ./bin/_deploy
@@ -0,0 +1,17 @@
1
+ version: '3.7'
2
+ services:
3
+ cicd:
4
+ build:
5
+ context: .
6
+ dockerfile: Dockerfile-build
7
+ environment:
8
+ - RAILS_ENV=${RAILS_ENV-development}
9
+ - AWS_PROFILE=${AWS_PROFILE-default}
10
+ - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION-us-east-1}
11
+ - SSH_AUTH_SOCK=${SSH_AUTH_SOCK}
12
+ volumes:
13
+ - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}
14
+ - ~/.aws:/root/.aws:delegated
15
+ - ~/.ssh/known_hosts:/root/.ssh/known_hosts
16
+ - .:/var/task:delegated
17
+ - /var/run/docker.sock:/var/run/docker.sock
@@ -28,14 +28,14 @@ Resources:
28
28
 
29
29
  RailsLoadBalancerTargetGroup:
30
30
  Type: AWS::ElasticLoadBalancingV2::TargetGroup
31
- DependsOn: RailsFunctionInvokePermission
31
+ DependsOn: RailsLambdaInvokePermission
32
32
  Properties:
33
33
  TargetType: lambda
34
34
  TargetGroupAttributes:
35
35
  - Key: lambda.multi_value_headers.enabled
36
36
  Value: true
37
37
  Targets:
38
- - Id: !GetAtt RailsFunction.Arn
38
+ - Id: !GetAtt RailsLambda.Arn
39
39
 
40
40
  RailsLoadBalancerHttpListener:
41
41
  Type: 'AWS::ElasticLoadBalancingV2::Listener'
@@ -58,26 +58,26 @@ Resources:
58
58
  ToPort: 80
59
59
  CidrIp: 0.0.0.0/0
60
60
 
61
- RailsFunctionInvokePermission:
61
+ RailsLambdaInvokePermission:
62
62
  Type: AWS::Lambda::Permission
63
63
  Properties:
64
- FunctionName: !GetAtt RailsFunction.Arn
64
+ FunctionName: !GetAtt RailsLambda.Arn
65
65
  Action: 'lambda:InvokeFunction'
66
66
  Principal: elasticloadbalancing.amazonaws.com
67
67
 
68
- RailsFunction:
68
+ RailsLambda:
69
69
  Type: AWS::Serverless::Function
70
+ Metadata:
71
+ DockerContext: ./.lamby/RailsLambda
72
+ Dockerfile: Dockerfile
70
73
  Properties:
71
- CodeUri: .
72
- Handler: app.handler
73
- Runtime: ruby2.7
74
74
  MemorySize: 512
75
75
  Timeout: 30
76
76
  Environment:
77
77
  Variables:
78
78
  RAILS_ENV: !Ref RailsEnv
79
79
  FunctionName: !Join [ '', [ 'APPNAMEHERE-', !Ref RailsEnv, '-', !Ref 'AWS::Region' ] ]
80
- AutoPublishAlias: live
80
+ PackageType: Image
81
81
 
82
82
  Outputs:
83
83
 
@@ -85,6 +85,6 @@ Outputs:
85
85
  Description: Application Load Balancer DNS Name
86
86
  Value: !GetAtt RailsLoadBalancer.DNSName
87
87
 
88
- RailsFunctionArn:
88
+ RailsLambdaArn:
89
89
  Description: Lambda ARN
90
- Value: !GetAtt RailsFunction.Arn
90
+ Value: !GetAtt RailsLambda.Arn
@@ -7,19 +7,27 @@ say 'Copying files...'
7
7
  copy_file tpl_file('app.rb'), app_file('app.rb')
8
8
  copy_file tpl_file('template.yaml'), app_file('template.yaml')
9
9
  gsub_file app_file('template.yaml'), /APPNAMEHERE/, appname
10
+ copy_file tpl_file('Dockerfile'), app_file('Dockerfile')
11
+ copy_file tpl_file('Dockerfile-build'), app_file('Dockerfile-build')
12
+ copy_file tpl_file('docker-compose.yml'), app_file('docker-compose.yml')
10
13
 
11
14
  say 'Adding to .gitignore...'
12
15
  FileUtils.touch app_file('.gitignore')
13
16
  append_to_file app_file('.gitignore'), <<~GITIGNORE
14
17
  # Lamby
15
18
  /.aws-sam
19
+ /.lamby
16
20
  GITIGNORE
17
21
 
18
22
  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
23
+ copy_file tpl_file('_build'), app_file('bin/_build')
24
+ gsub_file app_file('bin/_build'), /APPNAMEHERE/, appname.downcase
25
+ chmod app_file('bin/_build'), 0755
21
26
  copy_file tpl_file('deploy'), app_file('bin/deploy')
22
27
  chmod app_file('bin/deploy'), 0755
28
+ copy_file tpl_file('_deploy'), app_file('bin/_deploy')
29
+ gsub_file app_file('bin/_deploy'), /APPNAMEHERE/, appname.downcase
30
+ chmod app_file('bin/_deploy'), 0755
23
31
  gsub_file app_file('bin/deploy'), /APPNAMEHERE/, appname.downcase
24
32
 
25
33
  say 'Welcome to AWS Lambda and Rails 🎉', :green
@@ -0,0 +1,3 @@
1
+ FROM public.ecr.aws/lambda/ruby:2.7
2
+ COPY . .
3
+ CMD ["app.handler"]
@@ -0,0 +1,15 @@
1
+ FROM amazon/aws-sam-cli-build-image-ruby2.7
2
+
3
+ # Ensure minimum required SAM version.
4
+ # RUN pip install awscli && \
5
+ # pip uninstall --yes aws-sam-cli && \
6
+ # pip install aws-sam-cli==v1.13.2
7
+
8
+ # Node for JavaScript.
9
+ # RUN curl -sL https://rpm.nodesource.com/setup_12.x | bash - && \
10
+ # yum install -y nodejs && \
11
+ # curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo && \
12
+ # rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg && \
13
+ # yum install -y yarn
14
+
15
+ WORKDIR /var/task
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ echo '== Simulate SAM Build Directory =='
5
+ RESOURCE="RailsLambda"
6
+ SAM_TEMP=$(mktemp -d -t sam.XXXXXX)
7
+ rm -rf ./.lamby ./.aws-sam
8
+ cp -r . "$SAM_TEMP"
9
+ mkdir -p ./.lamby
10
+ cp -r "$SAM_TEMP" "./.lamby/$RESOURCE"
11
+ pushd "./.lamby/$RESOURCE"
12
+
13
+ echo '== Cleaning Dev Dependencies =='
14
+ rm -rf ./.bundle \
15
+ ./vendor/bundle
16
+
17
+ echo '== Bundle For Deployment =='
18
+ bundle config --local deployment true
19
+ bundle config --local without 'development test'
20
+ bundle config --local path './vendor/bundle'
21
+ bundle install --quiet --jobs 4
22
+
23
+ echo "== Environments & Configuration =="
24
+ # ./bin/rails \
25
+ # -rlamby \
26
+ # lamby:ssm:dotenv \
27
+ # LAMBY_SSM_PARAMS_PATH="/APPNAMEHERE/${RAILS_ENV}/env"
28
+
29
+ echo "== Asset Hosts & Precompiling =="
30
+ # NODE_ENV='production' \
31
+ # RAILS_GROUPS=assets \
32
+ # ./bin/rails assets:precompile
33
+
34
+ echo "== Cleanup Unused Files & Directories =="
35
+ rm -rf \
36
+ log \
37
+ node_modules \
38
+ test \
39
+ tmp \
40
+ vendor/bundle/ruby/2.7.0/cache
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env 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
+ # https://github.com/aws/aws-sam-cli/issues/2447
9
+ export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
10
+ export IMAGE_REPOSITORY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/myorg/APPNAMEHERE"
11
+
12
+ ./bin/_build
13
+
14
+ sam build \
15
+ --parameter-overrides \
16
+ RailsEnv="${RAILS_ENV}"
17
+
18
+ echo "== SAM package..."
19
+ sam package \
20
+ --region "$AWS_DEFAULT_REGION" \
21
+ --template-file ./.aws-sam/build/template.yaml \
22
+ --output-template-file ./.aws-sam/build/packaged.yaml \
23
+ --image-repository "$IMAGE_REPOSITORY" \
24
+ --s3-bucket "${CLOUDFORMATION_BUCKET}" \
25
+ --s3-prefix "APPNAMEHERE-${RAILS_ENV}"
26
+
27
+ echo "== SAM deploy..."
28
+ sam deploy \
29
+ --region "$AWS_DEFAULT_REGION" \
30
+ --template-file ./.aws-sam/build/packaged.yaml \
31
+ --stack-name "APPNAMEHERE-${RAILS_ENV}" \
32
+ --image-repository "$IMAGE_REPOSITORY" \
33
+ --capabilities "CAPABILITY_IAM" \
34
+ --parameter-overrides \
35
+ RailsEnv="${RAILS_ENV}"
@@ -1,4 +1,6 @@
1
+ ENV['RAILS_SERVE_STATIC_FILES'] = '1'
1
2
  require_relative 'config/boot'
3
+ require 'dotenv' ; Dotenv.load ".env.#{ENV['RAILS_ENV']}"
2
4
  require 'lamby'
3
5
  require_relative 'config/application'
4
6
  require_relative 'config/environment'
@@ -1,22 +1,9 @@
1
1
  #!/usr/bin/env bash
2
2
  set -e
3
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)"}
4
+ export RAILS_ENV=${RAILS_ENV:=production}
7
5
 
8
- ./bin/build
9
-
10
- sam package \
11
- --region ${AWS_DEFAULT_REGION} \
12
- --template-file ./.aws-sam/build/template.yaml \
13
- --output-template-file ./.aws-sam/build/packaged.yaml \
14
- --s3-bucket $CLOUDFORMATION_BUCKET \
15
- --s3-prefix "APPNAMEHERE-${RAILS_ENV}"
16
-
17
- sam deploy \
18
- --template-file ./.aws-sam/build/packaged.yaml \
19
- --stack-name "APPNAMEHERE-${RAILS_ENV}-${AWS_DEFAULT_REGION}" \
20
- --capabilities "CAPABILITY_IAM" \
21
- --parameter-overrides \
22
- RailsEnv=${RAILS_ENV}
6
+ docker-compose run \
7
+ -e CLOUDFORMATION_BUCKET \
8
+ cicd \
9
+ ./bin/_deploy
@@ -0,0 +1,17 @@
1
+ version: '3.7'
2
+ services:
3
+ cicd:
4
+ build:
5
+ context: .
6
+ dockerfile: Dockerfile-build
7
+ environment:
8
+ - RAILS_ENV=${RAILS_ENV-development}
9
+ - AWS_PROFILE=${AWS_PROFILE-default}
10
+ - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION-us-east-1}
11
+ - SSH_AUTH_SOCK=${SSH_AUTH_SOCK}
12
+ volumes:
13
+ - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}
14
+ - ~/.aws:/root/.aws:delegated
15
+ - ~/.ssh/known_hosts:/root/.ssh/known_hosts
16
+ - .:/var/task:delegated
17
+ - /var/run/docker.sock:/var/run/docker.sock
@@ -13,12 +13,12 @@ Parameters:
13
13
 
14
14
  Resources:
15
15
 
16
- RailsFunction:
16
+ RailsLambda:
17
17
  Type: AWS::Serverless::Function
18
+ Metadata:
19
+ DockerContext: ./.lamby/RailsLambda
20
+ Dockerfile: Dockerfile
18
21
  Properties:
19
- CodeUri: .
20
- Handler: app.handler
21
- Runtime: ruby2.7
22
22
  MemorySize: 512
23
23
  Timeout: 30
24
24
  Environment:
@@ -30,7 +30,7 @@ Resources:
30
30
  Type: HttpApi
31
31
  Properties:
32
32
  ApiId: !Ref RailsApi
33
- AutoPublishAlias: live
33
+ PackageType: Image
34
34
 
35
35
  RailsApi:
36
36
  Type: AWS::Serverless::HttpApi
@@ -43,6 +43,6 @@ Outputs:
43
43
  Description: API Gateway Endpoint
44
44
  Value: !Sub "https://${RailsApi}.execute-api.${AWS::Region}.amazonaws.com/${RailsEnv}/"
45
45
 
46
- RailsFunctionArn:
46
+ RailsLambdaArn:
47
47
  Description: Lambda ARN
48
- Value: !GetAtt RailsFunction.Arn
48
+ Value: !GetAtt RailsLambda.Arn
@@ -7,19 +7,27 @@ say 'Copying files...'
7
7
  copy_file tpl_file('app.rb'), app_file('app.rb')
8
8
  copy_file tpl_file('template.yaml'), app_file('template.yaml')
9
9
  gsub_file app_file('template.yaml'), /APPNAMEHERE/, appname
10
+ copy_file tpl_file('Dockerfile'), app_file('Dockerfile')
11
+ copy_file tpl_file('Dockerfile-build'), app_file('Dockerfile-build')
12
+ copy_file tpl_file('docker-compose.yml'), app_file('docker-compose.yml')
10
13
 
11
14
  say 'Adding to .gitignore...'
12
15
  FileUtils.touch app_file('.gitignore')
13
16
  append_to_file app_file('.gitignore'), <<~GITIGNORE
14
17
  # Lamby
15
18
  /.aws-sam
19
+ /.lamby
16
20
  GITIGNORE
17
21
 
18
22
  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
23
+ copy_file tpl_file('_build'), app_file('bin/_build')
24
+ gsub_file app_file('bin/_build'), /APPNAMEHERE/, appname.downcase
25
+ chmod app_file('bin/_build'), 0755
21
26
  copy_file tpl_file('deploy'), app_file('bin/deploy')
22
27
  chmod app_file('bin/deploy'), 0755
28
+ copy_file tpl_file('_deploy'), app_file('bin/_deploy')
29
+ gsub_file app_file('bin/_deploy'), /APPNAMEHERE/, appname.downcase
30
+ chmod app_file('bin/_deploy'), 0755
23
31
  gsub_file app_file('bin/deploy'), /APPNAMEHERE/, appname.downcase
24
32
 
25
33
  say 'Welcome to AWS Lambda and Rails 🎉', :green
@@ -0,0 +1,3 @@
1
+ FROM public.ecr.aws/lambda/ruby:2.7
2
+ COPY . .
3
+ CMD ["app.handler"]
@@ -0,0 +1,15 @@
1
+ FROM amazon/aws-sam-cli-build-image-ruby2.7
2
+
3
+ # Ensure minimum required SAM version.
4
+ # RUN pip install awscli && \
5
+ # pip uninstall --yes aws-sam-cli && \
6
+ # pip install aws-sam-cli==v1.13.2
7
+
8
+ # Node for JavaScript.
9
+ # RUN curl -sL https://rpm.nodesource.com/setup_12.x | bash - && \
10
+ # yum install -y nodejs && \
11
+ # curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo && \
12
+ # rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg && \
13
+ # yum install -y yarn
14
+
15
+ WORKDIR /var/task
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ echo '== Simulate SAM Build Directory =='
5
+ RESOURCE="RailsLambda"
6
+ SAM_TEMP=$(mktemp -d -t sam.XXXXXX)
7
+ rm -rf ./.lamby ./.aws-sam
8
+ cp -r . "$SAM_TEMP"
9
+ mkdir -p ./.lamby
10
+ cp -r "$SAM_TEMP" "./.lamby/$RESOURCE"
11
+ pushd "./.lamby/$RESOURCE"
12
+
13
+ echo '== Cleaning Dev Dependencies =='
14
+ rm -rf ./.bundle \
15
+ ./vendor/bundle
16
+
17
+ echo '== Bundle For Deployment =='
18
+ bundle config --local deployment true
19
+ bundle config --local without 'development test'
20
+ bundle config --local path './vendor/bundle'
21
+ bundle install --quiet --jobs 4
22
+
23
+ echo "== Environments & Configuration =="
24
+ # ./bin/rails \
25
+ # -rlamby \
26
+ # lamby:ssm:dotenv \
27
+ # LAMBY_SSM_PARAMS_PATH="/APPNAMEHERE/${RAILS_ENV}/env"
28
+
29
+ echo "== Asset Hosts & Precompiling =="
30
+ # NODE_ENV='production' \
31
+ # RAILS_GROUPS=assets \
32
+ # ./bin/rails assets:precompile
33
+
34
+ echo "== Cleanup Unused Files & Directories =="
35
+ rm -rf \
36
+ log \
37
+ node_modules \
38
+ test \
39
+ tmp \
40
+ vendor/bundle/ruby/2.7.0/cache
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env 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
+ # https://github.com/aws/aws-sam-cli/issues/2447
9
+ export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
10
+ export IMAGE_REPOSITORY="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/myorg/APPNAMEHERE"
11
+
12
+ ./bin/_build
13
+
14
+ sam build \
15
+ --parameter-overrides \
16
+ RailsEnv="${RAILS_ENV}"
17
+
18
+ echo "== SAM package..."
19
+ sam package \
20
+ --region "$AWS_DEFAULT_REGION" \
21
+ --template-file ./.aws-sam/build/template.yaml \
22
+ --output-template-file ./.aws-sam/build/packaged.yaml \
23
+ --image-repository "$IMAGE_REPOSITORY" \
24
+ --s3-bucket "${CLOUDFORMATION_BUCKET}" \
25
+ --s3-prefix "APPNAMEHERE-${RAILS_ENV}"
26
+
27
+ echo "== SAM deploy..."
28
+ sam deploy \
29
+ --region "$AWS_DEFAULT_REGION" \
30
+ --template-file ./.aws-sam/build/packaged.yaml \
31
+ --stack-name "APPNAMEHERE-${RAILS_ENV}" \
32
+ --image-repository "$IMAGE_REPOSITORY" \
33
+ --capabilities "CAPABILITY_IAM" \
34
+ --parameter-overrides \
35
+ RailsEnv="${RAILS_ENV}"
@@ -1,4 +1,6 @@
1
+ ENV['RAILS_SERVE_STATIC_FILES'] = '1'
1
2
  require_relative 'config/boot'
3
+ require 'dotenv' ; Dotenv.load ".env.#{ENV['RAILS_ENV']}"
2
4
  require 'lamby'
3
5
  require_relative 'config/application'
4
6
  require_relative 'config/environment'
@@ -1,22 +1,9 @@
1
1
  #!/usr/bin/env bash
2
2
  set -e
3
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)"}
4
+ export RAILS_ENV=${RAILS_ENV:=production}
7
5
 
8
- ./bin/build
9
-
10
- sam package \
11
- --region ${AWS_DEFAULT_REGION} \
12
- --template-file ./.aws-sam/build/template.yaml \
13
- --output-template-file ./.aws-sam/build/packaged.yaml \
14
- --s3-bucket $CLOUDFORMATION_BUCKET \
15
- --s3-prefix "APPNAMEHERE-${RAILS_ENV}"
16
-
17
- sam deploy \
18
- --template-file ./.aws-sam/build/packaged.yaml \
19
- --stack-name "APPNAMEHERE-${RAILS_ENV}-${AWS_DEFAULT_REGION}" \
20
- --capabilities "CAPABILITY_IAM" \
21
- --parameter-overrides \
22
- RailsEnv=${RAILS_ENV}
6
+ docker-compose run \
7
+ -e CLOUDFORMATION_BUCKET \
8
+ cicd \
9
+ ./bin/_deploy
@@ -0,0 +1,17 @@
1
+ version: '3.7'
2
+ services:
3
+ cicd:
4
+ build:
5
+ context: .
6
+ dockerfile: Dockerfile-build
7
+ environment:
8
+ - RAILS_ENV=${RAILS_ENV-development}
9
+ - AWS_PROFILE=${AWS_PROFILE-default}
10
+ - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION-us-east-1}
11
+ - SSH_AUTH_SOCK=${SSH_AUTH_SOCK}
12
+ volumes:
13
+ - ${SSH_AUTH_SOCK}:${SSH_AUTH_SOCK}
14
+ - ~/.aws:/root/.aws:delegated
15
+ - ~/.ssh/known_hosts:/root/.ssh/known_hosts
16
+ - .:/var/task:delegated
17
+ - /var/run/docker.sock:/var/run/docker.sock
@@ -28,7 +28,7 @@ Resources:
28
28
  x-amazon-apigateway-any-method:
29
29
  x-amazon-apigateway-integration:
30
30
  uri:
31
- Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RailsFunction.Arn}:live/invocations
31
+ Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RailsLambda.Arn}:live/invocations
32
32
  httpMethod: POST
33
33
  type: aws_proxy
34
34
  /{resource+}:
@@ -40,18 +40,18 @@ Resources:
40
40
  type: string
41
41
  x-amazon-apigateway-integration:
42
42
  uri:
43
- Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RailsFunction.Arn}:live/invocations
43
+ Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RailsLambda.Arn}:live/invocations
44
44
  httpMethod: POST
45
45
  type: aws_proxy
46
46
  x-amazon-apigateway-binary-media-types:
47
47
  - '*/*'
48
48
 
49
- RailsFunction:
49
+ RailsLambda:
50
50
  Type: AWS::Serverless::Function
51
+ Metadata:
52
+ DockerContext: ./.lamby/RailsLambda
53
+ Dockerfile: Dockerfile
51
54
  Properties:
52
- CodeUri: .
53
- Handler: app.handler
54
- Runtime: ruby2.7
55
55
  MemorySize: 512
56
56
  Timeout: 30
57
57
  Environment:
@@ -71,7 +71,7 @@ Resources:
71
71
  Path: /{resource+}
72
72
  Method: ANY
73
73
  RestApiId: !Ref RailsApi
74
- AutoPublishAlias: live
74
+ PackageType: Image
75
75
 
76
76
  Outputs:
77
77
 
@@ -79,6 +79,6 @@ Outputs:
79
79
  Description: API Gateway Endpoint
80
80
  Value: !Sub "https://${RailsApi}.execute-api.${AWS::Region}.amazonaws.com/${RailsEnv}/"
81
81
 
82
- RailsFunctionArn:
82
+ RailsLambdaArn:
83
83
  Description: Lambda ARN
84
- Value: !GetAtt RailsFunction.Arn
84
+ Value: !GetAtt RailsLambda.Arn
@@ -1,3 +1,3 @@
1
1
  module Lamby
2
- VERSION = '2.2.1'
2
+ VERSION = '2.5.3'
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: 2.2.1
4
+ version: 2.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2021-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -163,19 +163,31 @@ files:
163
163
  - lib/lamby/tasks.rake
164
164
  - lib/lamby/templates.rake
165
165
  - lib/lamby/templates/alb.rb
166
+ - lib/lamby/templates/alb/Dockerfile
167
+ - lib/lamby/templates/alb/Dockerfile-build
168
+ - lib/lamby/templates/alb/_build
169
+ - lib/lamby/templates/alb/_deploy
166
170
  - lib/lamby/templates/alb/app.rb
167
- - lib/lamby/templates/alb/build
168
171
  - lib/lamby/templates/alb/deploy
172
+ - lib/lamby/templates/alb/docker-compose.yml
169
173
  - lib/lamby/templates/alb/template.yaml
170
174
  - lib/lamby/templates/http.rb
175
+ - lib/lamby/templates/http/Dockerfile
176
+ - lib/lamby/templates/http/Dockerfile-build
177
+ - lib/lamby/templates/http/_build
178
+ - lib/lamby/templates/http/_deploy
171
179
  - lib/lamby/templates/http/app.rb
172
- - lib/lamby/templates/http/build
173
180
  - lib/lamby/templates/http/deploy
181
+ - lib/lamby/templates/http/docker-compose.yml
174
182
  - lib/lamby/templates/http/template.yaml
175
183
  - lib/lamby/templates/rest.rb
184
+ - lib/lamby/templates/rest/Dockerfile
185
+ - lib/lamby/templates/rest/Dockerfile-build
186
+ - lib/lamby/templates/rest/_build
187
+ - lib/lamby/templates/rest/_deploy
176
188
  - lib/lamby/templates/rest/app.rb
177
- - lib/lamby/templates/rest/build
178
189
  - lib/lamby/templates/rest/deploy
190
+ - lib/lamby/templates/rest/docker-compose.yml
179
191
  - lib/lamby/templates/rest/template.yaml
180
192
  - lib/lamby/templates/shared.rb
181
193
  - lib/lamby/version.rb
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env 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://lamby.custominktech.com/docs/environment_and_configuration
12
-
13
- # [HOOK] Asset Hosts & Precompiling
14
- # https://lamby.custominktech.com/docs/asset_host_and_precompiling
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
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env 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://lamby.custominktech.com/docs/environment_and_configuration
12
-
13
- # [HOOK] Asset Hosts & Precompiling
14
- # https://lamby.custominktech.com/docs/asset_host_and_precompiling
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
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env 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://lamby.custominktech.com/docs/environment_and_configuration
12
-
13
- # [HOOK] Asset Hosts & Precompiling
14
- # https://lamby.custominktech.com/docs/asset_host_and_precompiling
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