aws_cloudformation_helper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +60 -0
- data/.rubocop.yml +15 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +8 -0
- data/aws_cloudformation_helper.gemspec +40 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/aws_cloudformation_helper.rb +35 -0
- data/lib/aws_cloudformation_helper/event.rb +102 -0
- data/lib/aws_cloudformation_helper/logger.rb +56 -0
- data/lib/aws_cloudformation_helper/response.rb +99 -0
- data/lib/aws_cloudformation_helper/version.rb +11 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e2557da5d307726c403d35dfda7173c9c17d3400c513c017bf6d055f2262a4f0
|
4
|
+
data.tar.gz: 1bc54b9ea4df94a1a199528fa58e49c7aa4069e01c304823d361e4326c6e986c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 53fc752f5b5af740aec1269900d9919cadc980552f9cdc514552ace3dc4f70656736606afbcd38e5ed5101df51b6d809957f790486ffb314869ca40580d496fd
|
7
|
+
data.tar.gz: 8f47f9b95aca6765eeab448158b45e4f699a53e8eaa0aa18226775a7edaa82e6af4988eb5935c512a1090d1461773e65d2a4698c71bc5cf9ef31e075c4bf4c19
|
data/.gitignore
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
*.zip
|
4
|
+
/.config
|
5
|
+
/coverage/
|
6
|
+
/InstalledFiles
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/spec/examples.txt
|
10
|
+
/test/tmp/
|
11
|
+
/test/version_tmp/
|
12
|
+
/tmp/
|
13
|
+
.DS_Store
|
14
|
+
Gemfile.lock
|
15
|
+
.rspec_status
|
16
|
+
|
17
|
+
# Used by dotenv library to load environment variables.
|
18
|
+
# .env
|
19
|
+
|
20
|
+
# Ignore Byebug command history file.
|
21
|
+
.byebug_history
|
22
|
+
|
23
|
+
## Specific to RubyMotion:
|
24
|
+
.dat*
|
25
|
+
.repl_history
|
26
|
+
build/
|
27
|
+
*.bridgesupport
|
28
|
+
build-iPhoneOS/
|
29
|
+
build-iPhoneSimulator/
|
30
|
+
|
31
|
+
## Specific to RubyMotion (use of CocoaPods):
|
32
|
+
#
|
33
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
34
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
35
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
36
|
+
#
|
37
|
+
# vendor/Pods/
|
38
|
+
|
39
|
+
## Documentation cache and generated files:
|
40
|
+
/.yardoc/
|
41
|
+
/_yardoc/
|
42
|
+
/doc/
|
43
|
+
/rdoc/
|
44
|
+
|
45
|
+
## Environment normalization:
|
46
|
+
/.bundle/
|
47
|
+
/vendor/bundle
|
48
|
+
/lib/bundler/man/
|
49
|
+
|
50
|
+
# for a library or gem, you might want to ignore these files since the code is
|
51
|
+
# intended to run in multiple environments; otherwise, check them in:
|
52
|
+
# Gemfile.lock
|
53
|
+
# .ruby-version
|
54
|
+
# .ruby-gemset
|
55
|
+
|
56
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
57
|
+
.rvmrc
|
58
|
+
|
59
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
60
|
+
# .rubocop-https?--*
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Jeffrey Coe
|
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,95 @@
|
|
1
|
+
# aws_cloudformation_helper
|
2
|
+
[![Build Status](https://travis-ci.org/jeffreycoe/aws_cloudformation_helper.svg?branch=master)](https://travis-ci.org/jeffreycoe/aws_cloudformation_helper)
|
3
|
+
[![GitHub](https://img.shields.io/github/license/jeffreycoe/aws_cloudformation_helper)](https://github.com/jeffreycoe/aws_cloudformation_helper/blob/master/LICENSE.txt)
|
4
|
+
|
5
|
+
This Ruby gem is to assist with the development of custom resources for CloudFormation using the Ruby runtime.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'aws_cloudformation_helper'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install aws_cloudformation_helper
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Install this gem into your Lambda environment using bundle install or by bundling this gem in a Lambda layer.
|
26
|
+
|
27
|
+
Once the Ruby gem is installed, use the code template below for the main Lambda function to begin:
|
28
|
+
```ruby
|
29
|
+
require 'aws_cloudformation_helper'
|
30
|
+
|
31
|
+
def create
|
32
|
+
# Add code to handle CloudFormation Create event
|
33
|
+
end
|
34
|
+
|
35
|
+
def delete
|
36
|
+
# Add code to handle CloudFormation Delete event
|
37
|
+
end
|
38
|
+
|
39
|
+
def update
|
40
|
+
# Add code to handle CloudFormation Update event
|
41
|
+
end
|
42
|
+
|
43
|
+
def lambda_handler(event:, context:)
|
44
|
+
# Initializes CloudFormation Helper library
|
45
|
+
@cfn_helper = AWS::CloudFormation::Helper.new(self, event, context)
|
46
|
+
|
47
|
+
# Add additional initialization code here
|
48
|
+
|
49
|
+
# Executes the event
|
50
|
+
@cfn_helper.event.execute
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
The `create`, `delete`, and `update` methods will send a success response to CloudFormation when the method finishes executing, and a failure response will be sent when exceptions are raised.
|
55
|
+
|
56
|
+
### Logging
|
57
|
+
By default, the logging level is set to :info. To enable another logging level, add a line to the initialization section of the lambda_handler function. The logger is the standard Ruby logger provided by the Ruby stdlib.
|
58
|
+
|
59
|
+
The stdout and stderr variables can be reassigned to store log entries in another stream or file. By default, the default logger logs to STDOUT and the error logger (only for logger.error messages) logs to STDERR.
|
60
|
+
|
61
|
+
See more info here: https://ruby-doc.org/stdlib-2.7.0/libdoc/logger/rdoc/Logger.html
|
62
|
+
|
63
|
+
Below is a sample code block on enabling debug logging and redirecting stdout/stderr:
|
64
|
+
```ruby
|
65
|
+
def lambda_handler(event:, context:)
|
66
|
+
# Initializes CloudFormation Helper library
|
67
|
+
@cfn_helper = AWS::CloudFormation::Helper.new(self, event, context)
|
68
|
+
|
69
|
+
# Add additional initialization code here
|
70
|
+
@cfn_helper.logger.log_level = :debug
|
71
|
+
@cfn_helper.logger.stdout = $stdout
|
72
|
+
@cfn_helper.logger.stderr = $stderr
|
73
|
+
|
74
|
+
# Executes the event
|
75
|
+
@cfn_helper.event.execute
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### Event and Context
|
80
|
+
|
81
|
+
The event data and context object are available from the `@cfn_helper` variable in the main class. Access this data by calling `@cfn_helper.event` and `@cfn_helper.context`.
|
82
|
+
|
83
|
+
## Development
|
84
|
+
|
85
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
86
|
+
|
87
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jeffreycoe/aws_cloudformation_helper.
|
92
|
+
|
93
|
+
## License
|
94
|
+
|
95
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'aws_cloudformation_helper/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'aws_cloudformation_helper'
|
9
|
+
spec.version = AWS::CloudFormation::Helper::Version::GEM_VERSION
|
10
|
+
spec.authors = ['Jeffrey Coe']
|
11
|
+
|
12
|
+
spec.summary = 'Assists with the development of custom resources for AWS CloudFormation using the Ruby runtime'
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = 'https://github.com/jeffreycoe/aws_cloudformation_helper'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the allowed_push_host
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
|
20
|
+
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
23
|
+
spec.metadata['source_code_uri'] = 'https://github.com/jeffreycoe/aws_cloudformation_helper'
|
24
|
+
spec.metadata['changelog_uri'] = 'https://github.com/jeffreycoe/aws_cloudformation_helper/CHANGELOG.md'
|
25
|
+
end
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = 'exe'
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ['lib']
|
35
|
+
|
36
|
+
spec.add_development_dependency 'bundler'
|
37
|
+
spec.add_development_dependency 'rake'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
39
|
+
spec.add_development_dependency 'rubocop', '~> 0.79'
|
40
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'aws/cloudformation/helper'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws_cloudformation_helper/event'
|
4
|
+
require 'aws_cloudformation_helper/logger'
|
5
|
+
require 'aws_cloudformation_helper/response'
|
6
|
+
require 'aws_cloudformation_helper/version'
|
7
|
+
|
8
|
+
module AWS
|
9
|
+
module CloudFormation
|
10
|
+
# Main module
|
11
|
+
class Helper
|
12
|
+
attr_reader :event
|
13
|
+
attr_reader :context
|
14
|
+
|
15
|
+
def initialize(lambda_class, event, context)
|
16
|
+
# Ruby context object: https://docs.aws.amazon.com/lambda/latest/dg/ruby-context.html
|
17
|
+
@context = context
|
18
|
+
|
19
|
+
# Initialize event object
|
20
|
+
@event = Event.new(event, lambda_class.method(:create),
|
21
|
+
lambda_class.method(:delete), lambda_class.method(:update))
|
22
|
+
Event.instance = @event
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.logger
|
26
|
+
@logger ||= Logger.new
|
27
|
+
@logger
|
28
|
+
end
|
29
|
+
|
30
|
+
def logger
|
31
|
+
self.class.logger
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AWS
|
4
|
+
module CloudFormation
|
5
|
+
class Helper
|
6
|
+
# Creates an Event object based on the event data from CloudFormation
|
7
|
+
class Event
|
8
|
+
class << self
|
9
|
+
attr_accessor :instance
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :logical_resource_id
|
13
|
+
attr_reader :physical_resource_id
|
14
|
+
attr_reader :resource_properties
|
15
|
+
attr_reader :resource_type
|
16
|
+
attr_reader :response_url
|
17
|
+
attr_reader :request_id
|
18
|
+
attr_reader :request_type
|
19
|
+
attr_reader :stack_id
|
20
|
+
|
21
|
+
def initialize(event, create_method, delete_method, update_method)
|
22
|
+
parse_event_data(event)
|
23
|
+
|
24
|
+
@cfn_response = AWS::CloudFormation::Helper::Response.new
|
25
|
+
@create_method = create_method
|
26
|
+
@delete_method = delete_method
|
27
|
+
@update_method = update_method
|
28
|
+
end
|
29
|
+
|
30
|
+
def execute
|
31
|
+
Helper.logger.debug("Request Type: #{@request_type}")
|
32
|
+
|
33
|
+
case @request_type.downcase
|
34
|
+
when 'create'
|
35
|
+
execute_create
|
36
|
+
when 'delete'
|
37
|
+
execute_delete
|
38
|
+
when 'update'
|
39
|
+
execute_update
|
40
|
+
else
|
41
|
+
err_msg = "Invalid request type specified. Request Type: #{@request_type}"
|
42
|
+
Helper.logger.error(err_msg)
|
43
|
+
@cfn_response.failure(err_msg)
|
44
|
+
raise err_msg
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def execute_create
|
49
|
+
Helper.logger.info('Executing method for create event')
|
50
|
+
|
51
|
+
@create_method.call
|
52
|
+
@cfn_response.success
|
53
|
+
rescue StandardError => e
|
54
|
+
@cfn_response.failure(e)
|
55
|
+
raise e
|
56
|
+
end
|
57
|
+
|
58
|
+
def execute_delete
|
59
|
+
Helper.logger.info('Executing method for delete event')
|
60
|
+
|
61
|
+
@delete_method.call
|
62
|
+
@cfn_response.success
|
63
|
+
rescue StandardError => e
|
64
|
+
@cfn_response.failure(e)
|
65
|
+
raise e
|
66
|
+
end
|
67
|
+
|
68
|
+
def execute_update
|
69
|
+
Helper.logger.info('Executing method for update event')
|
70
|
+
|
71
|
+
@update_method.call
|
72
|
+
@cfn_response.success
|
73
|
+
rescue StandardError => e
|
74
|
+
@cfn_response.failure(e)
|
75
|
+
raise e
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def parse_event_data(event)
|
81
|
+
raise 'The event object from Lambda is nil.' if event.nil?
|
82
|
+
|
83
|
+
@logical_resource_id = event['LogicalResourceId'].to_s
|
84
|
+
@physical_resource_id = event['PhysicalResourceId'] if event.keys.include?('PhysicalResourceId')
|
85
|
+
@resource_properties = event['ResourceProperties']
|
86
|
+
@resource_type = event['ResourceType'].to_s
|
87
|
+
@response_url = event['ResponseURL'].to_s
|
88
|
+
@request_id = event['RequestId'].to_s
|
89
|
+
@request_type = event['RequestType'].to_s
|
90
|
+
@stack_id = event['StackId'].to_s
|
91
|
+
end
|
92
|
+
|
93
|
+
def valid_json?(json)
|
94
|
+
::JSON.parse(json)
|
95
|
+
true
|
96
|
+
rescue ::JSON::ParserError
|
97
|
+
false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module AWS
|
6
|
+
module CloudFormation
|
7
|
+
class Helper
|
8
|
+
# Main class to handle logging
|
9
|
+
class Logger
|
10
|
+
# https://docs.aws.amazon.com/lambda/latest/dg/ruby-logging.html
|
11
|
+
DEFAULT_LOG_LEVEL = :info
|
12
|
+
|
13
|
+
attr_accessor :log_level
|
14
|
+
attr_accessor :stdout
|
15
|
+
attr_accessor :stderr
|
16
|
+
|
17
|
+
def initialize(log_level = nil, stdout = STDOUT, stderr = STDERR)
|
18
|
+
@log_level = DEFAULT_LOG_LEVEL
|
19
|
+
@log_level = log_level unless log_level.nil?
|
20
|
+
@stdout = stdout
|
21
|
+
@stderr = stderr
|
22
|
+
end
|
23
|
+
|
24
|
+
def error(msg)
|
25
|
+
error_logger.error(msg)
|
26
|
+
end
|
27
|
+
|
28
|
+
def info(msg)
|
29
|
+
logger.info(msg)
|
30
|
+
end
|
31
|
+
|
32
|
+
def warn(msg)
|
33
|
+
logger.warn(msg)
|
34
|
+
end
|
35
|
+
|
36
|
+
def debug(msg)
|
37
|
+
logger.debug(msg)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def logger
|
43
|
+
@logger ||= ::Logger.new(@stdout)
|
44
|
+
@logger.level = @log_level
|
45
|
+
@logger
|
46
|
+
end
|
47
|
+
|
48
|
+
def error_logger
|
49
|
+
@logger ||= ::Logger.new(@stderr)
|
50
|
+
@logger.level = @log_level
|
51
|
+
@logger
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module AWS
|
8
|
+
module CloudFormation
|
9
|
+
class Helper
|
10
|
+
# Handles sending a response to CloudFormation
|
11
|
+
class Response
|
12
|
+
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-responses.html
|
13
|
+
FAILURE_STATUS = 'FAILED'
|
14
|
+
HTTP_MAX_RETRIES = 3
|
15
|
+
SUCCESS_STATUS = 'SUCCESS'
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@http_retries = 1
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure(reason = '')
|
22
|
+
Helper.logger.info("Sending #{FAILURE_STATUS} response to CloudFormation")
|
23
|
+
status_code = send_response('PUT', Event.instance.response_url, provider_response(FAILURE_STATUS, reason))
|
24
|
+
err_msg = "Failed to send failure message to CloudFormation pre-signed S3 URL. RC: #{status_code}"
|
25
|
+
raise err_msg if status_code > 400
|
26
|
+
end
|
27
|
+
|
28
|
+
def send_response(method, response_url, body = nil)
|
29
|
+
uri = ::URI.parse(response_url)
|
30
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
31
|
+
http.use_ssl = (uri.scheme == 'https')
|
32
|
+
request = http_request(method, uri, body)
|
33
|
+
|
34
|
+
Helper.logger.debug("Sending #{method} request to URL #{response_url}...")
|
35
|
+
response = http.request(request)
|
36
|
+
|
37
|
+
Helper.logger.debug("HTTP Response: #{response.inspect}")
|
38
|
+
response.code.to_i
|
39
|
+
rescue StandardError => e
|
40
|
+
err_msg = 'Failed to send response to CloudFormation pre-signed S3 URL. '\
|
41
|
+
"(Attempt #{@http_retries} of #{HTTP_MAX_RETRIES}) Error Details: #{e}"
|
42
|
+
Helper.logger.error(err_msg)
|
43
|
+
retry if (@http_retries += 1) <= HTTP_MAX_RETRIES
|
44
|
+
@http_retries = 1
|
45
|
+
err_msg = 'Reached max retry attempts and failed to send message to CloudFormation pre-signed S3 URL.'
|
46
|
+
Helper.logger.error(err_msg)
|
47
|
+
raise e
|
48
|
+
end
|
49
|
+
|
50
|
+
def success(reason = '')
|
51
|
+
Helper.logger.info("Sending #{SUCCESS_STATUS} response to CloudFormation")
|
52
|
+
status_code = send_response('PUT', Event.instance.response_url, provider_response(SUCCESS_STATUS, reason))
|
53
|
+
err_msg = "Failed to send success message to CloudFormation pre-signed S3 URL. RC: #{status_code}"
|
54
|
+
raise err_msg if status_code > 400
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def http_request(method, uri, body)
|
60
|
+
case method.upcase
|
61
|
+
when 'PUT'
|
62
|
+
request = Net::HTTP::Put.new(uri)
|
63
|
+
request.body = body.to_json
|
64
|
+
request['Content-Type'] = 'application/json'
|
65
|
+
else
|
66
|
+
Helper.logger.error("Invalid HTTP method specified. Method: #{method}")
|
67
|
+
end
|
68
|
+
|
69
|
+
request
|
70
|
+
end
|
71
|
+
|
72
|
+
def physical_resource_id
|
73
|
+
id = generate_physical_id
|
74
|
+
id = Event.instance.physical_resource_id unless Event.instance.physical_resource_id.to_s.empty?
|
75
|
+
id
|
76
|
+
end
|
77
|
+
|
78
|
+
def generate_physical_id
|
79
|
+
random_string = 8.times.map { [*'0'..'9', *'a'..'z'].sample }.join
|
80
|
+
"#{Event.instance.stack_id.split('/')[1]}_#{Event.instance.logical_resource_id}_#{random_string.downcase}"
|
81
|
+
end
|
82
|
+
|
83
|
+
def provider_response(status, reason)
|
84
|
+
{
|
85
|
+
Status: status,
|
86
|
+
Reason: reason.to_s,
|
87
|
+
PhysicalResourceId: physical_resource_id,
|
88
|
+
StackId: Event.instance.stack_id,
|
89
|
+
RequestId: Event.instance.request_id,
|
90
|
+
LogicalResourceId: Event.instance.logical_resource_id,
|
91
|
+
Data: {
|
92
|
+
Result: 'OK'
|
93
|
+
}
|
94
|
+
}
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aws_cloudformation_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeffrey Coe
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-27 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '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: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.79'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.79'
|
69
|
+
description: Assists with the development of custom resources for AWS CloudFormation
|
70
|
+
using the Ruby runtime
|
71
|
+
email:
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rubocop.yml"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- aws_cloudformation_helper.gemspec
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/aws_cloudformation_helper.rb
|
88
|
+
- lib/aws_cloudformation_helper/event.rb
|
89
|
+
- lib/aws_cloudformation_helper/logger.rb
|
90
|
+
- lib/aws_cloudformation_helper/response.rb
|
91
|
+
- lib/aws_cloudformation_helper/version.rb
|
92
|
+
homepage: https://github.com/jeffreycoe/aws_cloudformation_helper
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata:
|
96
|
+
homepage_uri: https://github.com/jeffreycoe/aws_cloudformation_helper
|
97
|
+
source_code_uri: https://github.com/jeffreycoe/aws_cloudformation_helper
|
98
|
+
changelog_uri: https://github.com/jeffreycoe/aws_cloudformation_helper/CHANGELOG.md
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubygems_version: 3.0.6
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Assists with the development of custom resources for AWS CloudFormation using
|
118
|
+
the Ruby runtime
|
119
|
+
test_files: []
|