aws-ec2 0.8.4 → 0.9.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: 7ecc2b042e1fabbb3a4a343edb5766601a51e54259f5029c19d37f93ebed7443
4
- data.tar.gz: d0a388114c37362c2f8b1590f71d1e2d16d54dee0631bd36a42c9f5ed89fd877
3
+ metadata.gz: 195f128cedbd5bf833767064b92f8f0a81fa3cc24ae131d9a932a7b714e7e3b3
4
+ data.tar.gz: 408ad9efbdbcbc7008db3267cf7ed68617bb01c437ffd8d4b410dfe43dfd0516
5
5
  SHA512:
6
- metadata.gz: c77a9ba413bec00c46b387370df141d10e64286ac94689c194437ff0aeecd3e8ccfbcb50a548549090344df3628affe8210014a4c902e40466da4360cc66bb6a
7
- data.tar.gz: 63e9c1864910f2a8c7d57a7005e64297272e6c1764642c29ba55fde4722073d46fd854a5f6afe7f08763e47f9d454e66fafdd790ff4599660dc02a1723738a67
6
+ metadata.gz: 06af930c02065bcd7a88a3c14ba72c0716738faa663c8cd59cba9900719614d7bb328b84620d41dd4823aed56333dcccf19261c9ca11c988fdcf162991bfaebf
7
+ data.tar.gz: e13dd0da82d53d71ab2fe955e08ac7f1fe622981024361f1c926475159e7c4749d28a92e9ade7aa258ba92398f605a327da26b41674f29191b6857a2cf44e83b
@@ -3,6 +3,11 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.9.0]
7
+ - much improved error messaging
8
+ - rename docs folder
9
+ - update readme
10
+
6
11
  ## [0.8.4]
7
12
  - add doc/example
8
13
  - rename to commands to compile and upload
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aws-ec2 (0.8.3)
4
+ aws-ec2 (0.8.4)
5
5
  activesupport
6
6
  aws-sdk-ec2
7
7
  colorize
data/README.md CHANGED
@@ -12,20 +12,7 @@ Example:
12
12
  aws-ec2 create myserver --profile myserver
13
13
  ```
14
14
 
15
- In a nutshell, the profile parameters are passed to the ruby aws-sdk [AWS::EC2::Client#run_instances](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Client.html#run_instances-instance_method) method. This allows you to specify any parameter you wish that is available in the aws-sdk. To check out what a profile looks like check out [example default](example/profiles/default.yml)
16
-
17
- You can use ERB in the profile files. Some useful helper methods are documented here:
18
-
19
- Helper | Description
20
- ------------- | -------------
21
- user_data | Allows you to embed a generated user_data script. More details on the user-data are provided in the user data section below.
22
- config | Access to the variables set in config/[AWS_EC2_ENV].yml. Examples are `config/development.yml`, `config/staging.yml`, and `config/production.yml`.
23
- latest_ami | Returns an AMI id by searching the ami name pattern and sorting in reverse older. Example: `latest_ami("ruby-2.5.0_*")` would return the latest ruby AMIs are named with timestamps at the end like so: `ruby-2.5.0_2018-01-30-05-36-02` and `ruby-2.5.0_2018-01-29-05-36-02`.
24
- search_ami | Returns a collection of AMI image objects based on a search pattern. The query searches on the AMI name.
25
-
26
- For a full list of all the template helpers checkout: [aws_ec2/template_helper](lib/aws_ec2/template_helper).
27
-
28
- You can also define custom helpers in the `app/helpers` folder as ruby modules with the naming convention `*_helper.rb`. Example, you would define a `module FooHelper` in `app/helpers/foo_helper.rb`. Custom helpers are first-class citizens and have access to the same variables, methods and scope as built-in helpers.
15
+ In a nutshell, the profile parameters are passed to the ruby aws-sdk [AWS::EC2::Client#run_instances](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Client.html#run_instances-instance_method) method. This allows you to specify any parameter you wish that is available in the aws-sdk. To check out what a profile looks like check out [example default](docs/example/profiles/default.yml)
29
16
 
30
17
  ## Noop mode
31
18
 
@@ -40,13 +27,28 @@ cat tmp/user-data.txt # to view generated user-data script
40
27
 
41
28
  Directory | Description
42
29
  ------------- | -------------
43
- app/helpers | Custom helpers methods. Define them as modules and their methods available whenever ERB is available: `config`, `profiles`, `app/scripts`, `app/user-data` files, etc. Example, you would define a `module FooHelper` in `app/helpers/foo_helper.rb`.
44
- app/partials | Your partials that can be use to be included in other scripts. This is used in conjunction with the `partial` helper method.
30
+ app/helpers | Custom helpers methods. Define them as modules and their methods are made available whenever ERB is available: `profiles`, `app/scripts`, `app/user-data` files, etc. For example, you would define a `module FooHelper` in `app/helpers/foo_helper.rb`.
31
+ app/partials | Your partials that can to be included in other scripts. This is used in conjunction with the `partial` helper method.
45
32
  app/scripts | Where you define common scripts that can be used to configure the server. These scripts can be automatically uploaded to an s3 bucket for later downloading in your user-data script by setting the `scripts_s3_bucket` config option.
46
33
  app/user-data | Your user-data scripts that are used to bootstrap EC2 instance.
47
- config/[AWS_EC2_ENV].yml | The config file where you set configs that you want available in your templating logic. Examples are: `config/development.yml`, `config/staging.yml`, or `config/production.yml`. You access the config with the `<%= config["var"] %>` helper.
48
- profiles | Your profile files. These files mainly contain parameters that are passed to the aws-sd2 run_instances API.
49
- tmp | Where the generated scripts get compiled to. You can manually invoke the complilation via `aws-ec2 compile` if you wish to inspect what is generated, though this is automatically done as part of the `aws-ec2` create command.
34
+ config/[AWS_EC2_ENV].yml | The config file where you set configs that you want available in your templating logic. Examples are: `config/development.yml` and `config/production.yml`. You access the config variables with the `<%= config["var"] %>` helper.
35
+ profiles | Your profile files. These files mainly contain parameters that are passed to the aws-sd2 run_instances API method.
36
+ tmp | Where the generated scripts get compiled to. You can manually invoke the complilation via `aws-ec2 compile` to inspect what is generated. This is automatically done as part of the `aws-ec2` create command.
37
+
38
+ ## Helpers
39
+
40
+ You can use ERB in the profile files. Some useful helper methods are documented here:
41
+
42
+ Helper | Description
43
+ ------------- | -------------
44
+ user_data | Allows you to embed a generated user_data script. More details on the user-data are provided in the user data section below.
45
+ config | Access to the variables set in config/[AWS_EC2_ENV].yml. Examples are `config/development.yml` and `config/production.yml`.
46
+ latest_ami | Returns an AMI id by searching the AMI name pattern and sorting in reverse older. Example: `latest_ami("ruby-2.5.0_*")` would return the latest ruby AMIs are named with timestamps at the end like so: `ruby-2.5.0_2018-01-30-05-36-02` and `ruby-2.5.0_2018-01-29-05-36-02`.
47
+ search_ami | Returns a collection of AMI image objects based on a search pattern. The query searches on the AMI name.
48
+
49
+ For a full list of all the template helpers check out: [aws_ec2/template_helper](lib/aws_ec2/template_helper).
50
+
51
+ You can also define custom helpers in the `app/helpers` folder as ruby modules with the naming convention `*_helper.rb`. For example, you would define a `module FooHelper` in `app/helpers/foo_helper.rb`. Custom helpers are first-class citizens and have access to the same variables, methods, and scope as built-in helpers.
50
52
 
51
53
  ## User-Data
52
54
 
@@ -54,20 +56,20 @@ You can provide a user-data script to customize the server upon launch. The use
54
56
 
55
57
  * app/user-data/myserver.yml
56
58
 
57
- The user-data script is generated on the machine that is running the aws-ec2 command. If this is your local macosx machine, then the context of your local macosx machine is available. To see the generated user-data script, you can use the run the create command in noop mode and then inspect the generated script. Example:
59
+ The user-data script is generated on the machine that is running the aws-ec2 command. If this is your local macosx machine, then the context of your local macosx machine is available. To see the generated user-data script, you can run the create command in noop mode and then inspect the generated script. Example:
58
60
 
59
61
  ```sh
60
62
  aws-ec2 create myserver --noop
61
- cat /tmp/aws-ec2/user-data.txt
63
+ cat tmp/user-data.txt
62
64
  ```
63
65
 
64
- Another way of viewing the generated user-data scripts is the `aws-ec2 compile` command. It generates the files in the tmp folder. Example:
66
+ Another way to view the generated user-data scripts is the `aws-ec2 compile` command. It generates the files in the tmp folder. Example:
65
67
 
66
68
  ```
67
69
  aws-ec2 compile # generates files in tmp folder
68
70
  ```
69
71
 
70
- To use the user-data script when creating an EC2 instance, you use the `user_data` helper method in the profile. Here's a grep of an example profile that uses the helper to show you want it looks like:
72
+ To use the user-data script when creating an EC2 instance, use the `user_data` helper method in the profile file. Here's a grep of an example profile that uses the helper to show you want it looks like. Be sure to surround the ERB call with quotes because the user-data script context is base64 encoded.
71
73
 
72
74
  ```
73
75
  $ grep user_data profiles/default.yml
@@ -76,12 +78,12 @@ user_data: "<%= user_data("bootstrap") %>"
76
78
 
77
79
  ### Config
78
80
 
79
- You can set variables in a config file and they are available when ERB is available, which are your profiles, user_data, scripts, etc. Example `config/development.yml`:
81
+ You can set variables in a config file and they are available when ERB is available: profiles, user-data, scripts, etc. Example `config/development.yml`:
80
82
 
81
83
  ```yaml
82
84
  ---
83
85
  vpc_id: vpc-123
84
- db_subnet_group_name: default
86
+ subnets:
85
87
  - subnet-123
86
88
  - subnet-456
87
89
  - subnet-789
@@ -92,7 +94,7 @@ scripts_s3_bucket: mybucket # enables s3 uploading of generated app/scripts
92
94
  # compiled scripts in tmp after aws-ec2 create
93
95
  ```
94
96
 
95
- The variables are accessed via the `config` helper method. Here's a filtered example where it shows shows the relevant part of a profile: `profiles/default.yml`:
97
+ The variables are accessed via the `config` helper method. Here's a filtered example where it shows the relevant part of a profile: `profiles/default.yml`:
96
98
 
97
99
  ```yaml
98
100
  image_id: ami-4fffc834 # Amazon Lambda AMI
@@ -108,15 +110,11 @@ There are some config options that change the behavior of the ec2-aws:
108
110
 
109
111
  Option | Description
110
112
  --- | ---
111
- scripts_s3_bucket | Set this to the bucket name where you want the generated scripts in app/scripts and app/user-data to be automatically sync. The sync happens right before the the internal call to run_instances that launches the instance. If you need more custom logic, you can use the `before_run_instances` hook, covered in the Hooks section.
112
-
113
- ## Dotenv File Support
114
-
115
- You can set and configure environment variables in `.env*` files. Examples of this is in the [doc/example](doc/example) project.
113
+ scripts_s3_bucket | Set this to the bucket name where you want the generated scripts in app/scripts and app/user-data to be uploaded to. The upload sync happens right before the internal to run_instances call that launches the instance. If you need more custom logic, you can use the `before_run_instances` hook, covered in the Hooks section.
116
114
 
117
115
  ### Hooks
118
116
 
119
- There is only one hook: before_run_instances. You can configure this with `config/hooks.yml`: Example:
117
+ There is only one hook: `before_run_instances`. You can configure this with `config/hooks.yml`: Example:
120
118
 
121
119
  ```
122
120
  ---
@@ -125,11 +123,21 @@ before_run_instances: /path/to/my/script.sh
125
123
 
126
124
  This will run `/path/to/my/script.sh` as a shelled out command before the `run_instances` call.
127
125
 
126
+ ## Dotenv File Support
127
+
128
+ You can set and configure environment variables in `.env*` files. Examples of this are in the [example](docs/example) project.
129
+
128
130
  ## AMI Creation
129
131
 
130
- To create AMIs you can use the `aws-ec2 ami` command. This command launches an EC2 instance with the specified profile and create an AMI after the user-data script completes successfully. It does this by adding an AMI creation script at the end of the user-data script. It is recommended to use the `set -e` option so that any error halts the script and the AMI does not get created.
132
+ To create AMIs you can use the `aws-ec2 ami` command. This command launches an EC2 instance with the specified profile and creates an AMI after the user-data script successfully completes. It does this by appending an AMI creation script at the end of the user-data script. It is recommended to use the `set -e` option in your user-data script so that any error halts the script and the AMI does not get created.
133
+
134
+ After the AMI is successfully created, the instance will also terminate itself automatically so you do not have to worry about cleanup. This is also done with an appended script. For more help run `aws-ec2 ami help`.
135
+
136
+ For the instance to image and terminate itself, the EC2 IAM role for the instance requires IAM permissions for:
131
137
 
132
- After the AMI is created successfully, the instance will also terminate itself automatically so you do not have to worry about cleanup. For more help run `aws-ec2 ami help`.
138
+ * aws ec2 create-image
139
+ * aws ec2 cancel-spot-instance-requests # in case a spot instance was used
140
+ * aws ec2 terminate-instances
133
141
 
134
142
  ## Spot Instance Support
135
143
 
@@ -138,7 +146,7 @@ Spot instance is natively supported by the AWS run_instances command. So, simpl
138
146
  * [https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateSpotMarketOptionsRequest.html](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateSpotMarketOptionsRequest.html)
139
147
  * [https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Types/SpotMarketOptions.html](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/EC2/Types/SpotMarketOptions.html)
140
148
 
141
- An example of a spot instance profile is provided in [doc/example/profiles/spot.yml](doc/example/profiles/spot.yml).
149
+ An example of a spot instance profile is provided in [example/profiles/spot.yml](docs/example/profiles/spot.yml).
142
150
 
143
151
  ## More Help
144
152
 
@@ -149,7 +157,7 @@ aws-ec2 compile help
149
157
  aws-ec2 help # general help
150
158
  ```
151
159
 
152
- Examples are in the [doc/example](doc/example) folder. You will have to update settings like your subnet and security group ids.
160
+ Examples are in the [example](docs/example) folder. You will have to update settings like your subnet and security group ids.
153
161
 
154
162
  ## Installation
155
163
 
@@ -159,7 +167,7 @@ gem install aws-ec2
159
167
 
160
168
  ### Dependencies
161
169
 
162
- This tool mainly uses the ruby aws-sdk but it does use the aws cli to check your region: `aws configure get region`. So it is dependent on the the `aws cli`.
170
+ This tool mainly uses the ruby aws-sdk. Though it does use the aws cli to check your region: `aws configure get region`. It also the uses `aws s3 sync` to perform the scripts upload. So it is dependent on the the `aws cli`.
163
171
 
164
172
  ## Contributing
165
173
 
File without changes
File without changes
File without changes
File without changes
@@ -4,8 +4,10 @@ require 'active_support/core_ext/hash'
4
4
  module AwsEc2
5
5
  class Create < Base
6
6
  autoload :Params, "aws_ec2/create/params"
7
+ autoload :ErrorMessages, "aws_ec2/create/error_messages"
7
8
 
8
9
  include AwsServices
10
+ include ErrorMessages
9
11
 
10
12
  def run
11
13
  puts "Creating EC2 instance #{@options[:name]}..."
@@ -17,11 +19,17 @@ module AwsEc2
17
19
 
18
20
  Hook.run(:before_run_instances, @options)
19
21
  sync_scripts_to_s3
20
- resp = ec2.run_instances(params)
22
+ run_instances(params)
21
23
  puts "EC2 instance #{@options[:name]} created! 🎉"
22
24
  puts "Visit https://console.aws.amazon.com/ec2/home to check on the status"
23
25
  end
24
26
 
27
+ def run_instances(params)
28
+ resp = ec2.run_instances(params)
29
+ rescue Aws::EC2::Errors::ServiceError => e
30
+ handle_ec2_service_error!(e)
31
+ end
32
+
25
33
  # Configured by config/[AWS_EC2_ENV].yml.
26
34
  # Example: config/development.yml:
27
35
  #
@@ -0,0 +1,58 @@
1
+ require "active_support/core_ext/string"
2
+
3
+ class AwsEc2::Create
4
+ module ErrorMessages
5
+ def handle_ec2_service_error!(exception)
6
+ meth = map_exception_to_method(exception)
7
+ if respond_to?(meth)
8
+ message = send(meth) # custom specific error message
9
+ message = print_error_message(exception, message)
10
+ else
11
+ # generic error message
12
+ print_error_message(exception, <<-EOL)
13
+ There was an error with the parameters used for the run_instance method.
14
+ EOL
15
+ end
16
+ end
17
+
18
+ # Examples:
19
+ # Aws::EC2::Errors::InvalidGroupNotFound => invalid_group_not_found!
20
+ # Aws::EC2::Errors::InvalidParameterCombination => invalid_parameter_combination!
21
+ def map_exception_to_method(exception)
22
+ class_name = File.basename(exception.class.to_s).sub(/.*::/,'')
23
+ method_name = class_name.underscore
24
+ end
25
+
26
+ def print_error_message(exception, message)
27
+ puts "ERROR: Unable to launch the instance.".colorize(:red)
28
+ puts message
29
+ puts exception.message
30
+ puts "For the full internal backtrace re-run the command with DEBUG=1"
31
+ puts exception.backtrace if ENV['DEBUG']
32
+ exit 1
33
+ end
34
+
35
+ #######################################################
36
+ # specific messages with a little more info for more common error cases below:
37
+ def invalid_group_not_found
38
+ <<-EOL
39
+ The security group passed in does not exit.
40
+ Please double check that security group exists in the VPC.
41
+ EOL
42
+ end
43
+
44
+ def invalid_parameter_combination
45
+ <<-EOL
46
+ The parameters passed to the run_instances method were invalid.
47
+ Please double check that the parameters are all valid.
48
+ EOL
49
+ end
50
+
51
+ def invalid_subnet_id_not_found
52
+ <<-EOL
53
+ The provided subnets ids were were not found.
54
+ Please double check that the subnets exists.
55
+ EOL
56
+ end
57
+ end
58
+ end
@@ -6,7 +6,7 @@ Launches an EC2 instance to create an AMI. An AMI creation script is appended t
6
6
 
7
7
  It is useful to include to timestamp as a part of the AMI name with the date command.
8
8
 
9
- $ aws-ec2 ami $(date "+ruby-2.5.0_%Y-%m-%d-%H-%M") --profile ruby --noop
9
+ $ aws-ec2 ami ruby-2.5.0_$(date "+%Y-%m-%d-%H-%M") --profile ruby --noop
10
10
 
11
11
  The instance also automatically gets terminated and cleaned up by a termination script appended to user-data.
12
12
 
@@ -25,7 +25,7 @@ module AwsEc2
25
25
  def load_profile(file)
26
26
  return {} unless File.exist?(file)
27
27
 
28
- puts "Using profile: #{file}"
28
+ puts "Using profile: #{file}".colorize(:green)
29
29
  data = YAML.load(erb_result(file))
30
30
  data ? data : {} # in case the file is empty
31
31
  data.has_key?("run_instances") ? data["run_instances"] : data
@@ -13,7 +13,7 @@ class AwsEc2::Script
13
13
  end
14
14
 
15
15
  def compile_folder(folder)
16
- puts "Compiling app/#{folder}:"
16
+ puts "Compiling app/#{folder}:".colorize(:green)
17
17
  Dir.glob("#{AwsEc2.root}/app/#{folder}/**/*").each do |path|
18
18
  next if File.directory?(path)
19
19
  result = erb_result(path)
@@ -15,7 +15,7 @@ class AwsEc2::Script
15
15
  end
16
16
 
17
17
  def sync_scripts_to_s3
18
- puts "Uploading tmp/app to s3..."
18
+ puts "Uploading tmp/app to s3...".colorize(:green)
19
19
  s3_bucket = AwsEc2.config["scripts_s3_bucket"]
20
20
  s3_path = AwsEc2.config["scripts_s3_path"] || "ec2/app"
21
21
  sh "aws s3 sync tmp/app s3://#{s3_bucket}/#{s3_path}"
@@ -1,3 +1,3 @@
1
1
  module AwsEc2
2
- VERSION = "0.8.4"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-ec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-30 00:00:00.000000000 Z
11
+ date: 2018-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -197,14 +197,14 @@ files:
197
197
  - README.md
198
198
  - Rakefile
199
199
  - aws-ec2.gemspec
200
- - doc/example/.env
201
- - doc/example/.env.development
202
- - doc/example/.env.production
203
- - doc/example/app/scripts/hello.sh
204
- - doc/example/app/user-data/bootstrap.sh
205
- - doc/example/config/development.yml
206
- - doc/example/profiles/default.yml
207
- - doc/example/profiles/spot.yml
200
+ - docs/example/.env
201
+ - docs/example/.env.development
202
+ - docs/example/.env.production
203
+ - docs/example/app/scripts/hello.sh
204
+ - docs/example/app/user-data/bootstrap.sh
205
+ - docs/example/config/development.yml
206
+ - docs/example/profiles/default.yml
207
+ - docs/example/profiles/spot.yml
208
208
  - exe/aws-ec2
209
209
  - lib/aws-ec2.rb
210
210
  - lib/aws_ec2/ami.rb
@@ -215,6 +215,7 @@ files:
215
215
  - lib/aws_ec2/config.rb
216
216
  - lib/aws_ec2/core.rb
217
217
  - lib/aws_ec2/create.rb
218
+ - lib/aws_ec2/create/error_messages.rb
218
219
  - lib/aws_ec2/create/params.rb
219
220
  - lib/aws_ec2/dotenv.rb
220
221
  - lib/aws_ec2/help.rb