cfnresponse 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2649632ac1f6c97689ee00059a98c31cacc36d219a3fb2f37f5bfb0891a58aa2
4
+ data.tar.gz: 15dafe63948b5ea494712d98450785090cbfcc27cca17d1c094765e6308dbed6
5
+ SHA512:
6
+ metadata.gz: 77b890be2a4c5bb503ad065f2f4d48bee2e2851c1d1ba284246b898d9c23db33e4da79118095021800786a52cd8b5c03bb610265e170cba78427dc004c62da1c
7
+ data.tar.gz: 7672d10f7aa461b8cd4c21ebe18d7103d7891db2cfa39824f6481da4332992784327dda5a38fe3993daf5bfcf9184c3735c8da36a40949af36a72df6bf9cf1dd
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cfnresponse.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Tung Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Cfnresponse
2
+
3
+ Cfnresponse helps with writing [Custom CloudFormation resources](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html). The main method is `send_response`, which builds the response that is sent back to CloudFormation service from the Lambda function.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cfnresponse'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cfnresponse
20
+
21
+ ## Example Usage
22
+
23
+ ```ruby
24
+ require "cfnresponse"
25
+ include Cfnresponse
26
+
27
+ def lambda_handler(event:, context:)
28
+ print("Received event: " + json_pretty(event))
29
+
30
+ case event['RequestType']
31
+ when "Create"
32
+ # create logic
33
+ send_response(event, context, SUCCESS)
34
+ when "Update"
35
+ # update logic
36
+ send_response(event, context, SUCCESS)
37
+ when "Delete"
38
+ # delete logic
39
+ send_response(event, context, SUCCESS)
40
+ end
41
+
42
+ sleep 10 # a little time for logs to be sent to CloudWatch
43
+
44
+ # We rescue all exceptions and send a message to CloudFormation so we don't have to
45
+ # wait for over an hour for the stack operation to timeout and rollback.
46
+ rescue Exception => e
47
+ puts e.message
48
+ puts e.backtrace
49
+ sleep 10 # a little time for logs to be sent to CloudWatch
50
+ send_response(event, context, "FAILED")
51
+ end
52
+ ```
53
+
54
+ * [Cfnresponse code](lib/cfnresponse.rb)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cfnresponse"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cfnresponse/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cfnresponse"
7
+ spec.version = Cfnresponse::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tongueroo@gmail.com"]
10
+
11
+ spec.summary = %q{CloudFormation send_response helpers to help with custom resources}
12
+ spec.homepage = "https://github.com/tongueroo/cfnresponse"
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.0"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,59 @@
1
+ require "cfnresponse/version"
2
+ require "json"
3
+ require "uri"
4
+
5
+ module Cfnresponse
6
+ class Error < StandardError; end
7
+
8
+ # Debugging puts kept to help debug custom resources
9
+ def send_response(event, context, response_status, response_data={}, physical_id="PhysicalId", reason=nil)
10
+ reason ||= "See the details in CloudWatch Log Stream: #{context.log_stream_name.inspect}"
11
+
12
+ response_body = JSON.dump(
13
+ Status: response_status,
14
+ Reason: reason,
15
+ PhysicalResourceId: physical_id,
16
+ StackId: event['StackId'],
17
+ RequestId: event['RequestId'],
18
+ LogicalResourceId: event['LogicalResourceId'],
19
+ Data: response_data
20
+ )
21
+
22
+ puts "Response body:\n"
23
+ puts json_pretty(JSON.load(response_body))
24
+
25
+ url = event['ResponseURL']
26
+ uri = URI(url)
27
+ http = Net::HTTP.new(uri.host, uri.port)
28
+ http.open_timeout = http.read_timeout = 30
29
+ http.use_ssl = true if uri.scheme == 'https'
30
+
31
+ # must used url to include the AWSAccessKeyId and Signature
32
+ req = Net::HTTP::Put.new(url) # url includes query string and uri.path does not, must used url t
33
+ req.body = response_body
34
+ req.content_length = response_body.bytesize
35
+
36
+ # set headers
37
+ req['content-type'] = ''
38
+ req['content-length'] = response_body.bytesize
39
+
40
+ if ENV['CFNRESPONSE_TEST']
41
+ puts "uri #{uri.inspect}"
42
+ return # early return to not send the request
43
+ end
44
+
45
+ res = http.request(req)
46
+ puts "status code: #{res.code}"
47
+ puts "headers: #{res.each_header.to_h.inspect}"
48
+ puts "body: #{res.body}"
49
+ end
50
+
51
+ # Useful for pretty json output on cloud9 and also CloudWatch logs
52
+ def json_pretty(data)
53
+ if ENV['C9_USER']
54
+ JSON.pretty_generate(data)
55
+ else
56
+ JSON.dump(data) # one a single line for CloudWatch to reformat
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Cfnresponse
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cfnresponse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - tongueroo@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - cfnresponse.gemspec
72
+ - lib/cfnresponse.rb
73
+ - lib/cfnresponse/version.rb
74
+ homepage: https://github.com/tongueroo/cfnresponse
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.0.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: CloudFormation send_response helpers to help with custom resources
97
+ test_files: []