Skip to content

Commit 1cf714d

Browse files
authored
Merge pull request #1472 from pccornel/pccornel-ruby-ec2
Cleaned up remaining Ruby code examples for Amazon EC2. Added READMEs
2 parents ffaf376 + b931fd5 commit 1cf714d

13 files changed

+976
-243
lines changed

ruby/example_code/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This folder contains code examples that demonstrate how to use the AWS SDK for R
2121

2222
- As an AWS best practice, grant this code least privilege, or only the
2323
permissions required to perform a task. For more information, see
24-
[Grant Least Privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
24+
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
2525
in the *AWS Identity and Access Management User Guide*.
2626
- This code has not been tested in all AWS Regions. Some AWS services are
2727
available only in specific AWS Regions. For more information, see the
@@ -37,7 +37,7 @@ Most of these code example files can be run with very little to no modification.
3737
ruby s3/create_bucket_snippet.rb
3838
```
3939

40-
Most of these files have been refactored into reusable functions that can be copied into your own code. You can then call those functions directly from your own code without modifying the copied function code itself. For example, you could copy the `list_bucket_objects?` function code from the `s3/auth_request_object_keys.rb` file into your own code. You could then adapt the code in the `run_me` function in that same file as a basis to write your own code to call the copied `list_bucket_objects?` function.
40+
Most of these files have been refactored into reusable functions that can be copied into your own code. You can then call those functions directly from your own code without modifying the copied function code itself. For example, you could copy the `bucket_created?` function code from the `s3/create_bucket_snippet.rb` file into your own code. You could then adapt the code in the `run_me` function in that same file as a basis to write your own code to call the copied `bucket_created?` function.
4141

4242
## Running the tests
4343

@@ -46,7 +46,7 @@ Most of these code example files have accompanying tests that are written to wor
4646
To use RSpec to run all tests within a file, specify the path to that file, for example:
4747

4848
```
49-
rspec s3/tests/test_auth_request_test.rb
49+
rspec s3/tests/test_create_bucket_snippet.rb
5050
```
5151

5252
To explore additional options for using RSpec to run tests, run the `rspec --help` command.
@@ -56,7 +56,7 @@ Most of these tests are designed to use stubs, to avoid generating unnecessary c
5656

5757
## Additional information
5858

59-
- [Amazon S3 Developer Guide](https://docs.aws.amazon.com/AmazonS3/latest/dev)
59+
- [AWS Documentation](https://docs.aws.amazon.com)
6060
- [AWS SDK for Ruby Documentation](https://docs.aws.amazon.com/sdk-for-ruby)
6161
- [AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/credref/latest/refdocs)
6262
- [RSpec Documentation](https://rspec.info/documentation)

ruby/example_code/ec2/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# AWS SDK for Ruby code examples for Amazon EC2
2+
3+
## Purpose
4+
5+
This folder contains code examples that demonstrate how to use the AWS SDK for Ruby to automate
6+
Amazon Elastic Compute Cloud (Amazon EC2).
7+
8+
## Prerequisites
9+
10+
- An AWS account. To create an account, see [How do I create and activate a new AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) on the AWS Premium Support website.
11+
- AWS credentials or an AWS Security Token Service (AWS STS) access token. For details, see
12+
[Configuring the AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html) in the
13+
*AWS SDK for Ruby Developer Guide*.
14+
- To run the code examples, Ruby version 1.9 or later. For Ruby download and installation instructions, see
15+
[Download Ruby](https://www.ruby-lang.org/en/downloads/) on the Ruby Progamming Language website.
16+
- To test the code examples, RSpec 3.9 or later. For RSpec download and installation instructions, see the [rspec/rspec](https://github.com/rspec/rspec) repository in GitHub.
17+
- The AWS SDK for Ruby. For AWS SDK for Ruby download and installation instructions, see
18+
[Install the AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-install.html) in the
19+
*AWS SDK for Ruby Developer Guide*.
20+
21+
## Cautions
22+
23+
- As an AWS best practice, grant this code least privilege, or only the
24+
permissions required to perform a task. For more information, see
25+
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
26+
in the *AWS Identity and Access Management User Guide*.
27+
- This code has not been tested in all AWS Regions. Some AWS services are
28+
available only in specific AWS Regions. For more information, see the
29+
[AWS Regional Services List](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)
30+
on the AWS website.
31+
- Running this code might result in charges to your AWS account.
32+
33+
## Running the code
34+
35+
Most of these code example files can be run with very little to no modification. For example, to use Ruby to run the `ec2-ruby-example-create-instance.rb` file, you can run the file along with the correct command-line arguments. Or you can replace the equivalent hard-coded values in the file with your own values, save the file, and then run the file without any command-line arguments. For example:
36+
37+
```
38+
# Option 1: Run the file with the correct command-line arguments.
39+
40+
ruby ec2-ruby-example-create-instance.rb ami-0947d2ba12EXAMPLE my-key-pair my-key my-value t2.micro us-east-1 my-user-data.txt
41+
42+
# Option 2: Replace the equivalent hard-coded values in the file with your own values, save the file, and then run the file without any command-line arguments.
43+
44+
ruby ec2-ruby-example-create-instance.rb
45+
```
46+
47+
To display the list of correct command-line arguments, run the file along with the `-h` or `--help` option, for example:
48+
49+
```
50+
# Running one of the following:
51+
52+
ruby ec2-ruby-example-create-instance.rb -h
53+
ruby ec2-ruby-example-create-instance.rb --help
54+
55+
# Displays:
56+
57+
Usage: ruby ec2-ruby-example-create-instance.rb IMAGE_ID KEY_PAIR_NAME TAG_KEY TAG_VALUE INSTANCE_TYPE REGION [USER_DATA_FILE]
58+
Example: ruby ec2-ruby-example-create-instance.rb ami-0947d2ba12EXAMPLE my-key-pair my-key my-value t2.micro us-east-1 my-user-data.txt
59+
```
60+
61+
Most of these files have been refactored into reusable functions that can be copied into your own code. You can then call those functions directly from your own code without modifying the copied function code itself. For example, you could copy the `instance_created?` function code from the `ec2-ruby-example-create-instance.rb` file into your own code. You could then adapt the code in the `run_me` function in that same file as a basis to write your own code to call the copied `instance_created?` function.
62+
63+
## Running the tests
64+
65+
Most of these code example files have accompanying tests that are written to work with RSpec. These tests are in the `tests` folder and contain the same file name as the corresponding code example file, for example `tests/test_ec2-ruby-example-create-instance.rb` contains tests for `ec2-ruby-example-create-instance.rb`.
66+
67+
To use RSpec to run all tests within a file, specify the path to that file, for example:
68+
69+
```
70+
rspec tests/test_ec2-ruby-example-create-instance.rb
71+
```
72+
73+
To explore additional options for using RSpec to run tests, run the `rspec --help` command.
74+
75+
Most of these tests are designed to use stubs, to avoid generating unnecessary costs in an AWS account. For more information, see [Stubbing Client Responses and Errors](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/stubbing.html) in the *AWS SDK for Ruby Developer Guide*.
76+
77+
78+
## Additional information
79+
80+
- [Amazon Elastic Compute Cloud Documentation](https://docs.aws.amazon.com/ec2)
81+
- [AWS SDK for Ruby Documentation](https://docs.aws.amazon.com/sdk-for-ruby)
82+
- [AWS Tools and SDKs Shared Configuration and Credentials Reference Guide](https://docs.aws.amazon.com/credref/latest/refdocs)
83+
- [RSpec Documentation](https://rspec.info/documentation)
84+
85+
---
86+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
87+
88+
SPDX-License-Identifier: Apache-2.0

ruby/example_code/ec2/ec2-ruby-example-manage-instances.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def instance_detailed_monitoring_enabled?(ec2_client, instance_id)
122122
puts "Detailed monitoring state: #{result.instance_monitorings[0].monitoring.state}"
123123
return true
124124
rescue Aws::EC2::Errors::InvalidState
125-
puts "The instance is not in a monitorable state. Continuing on..."
125+
puts "The instance is not in a monitorable state. Skipping this step."
126126
return false
127127
rescue StandardError => e
128128
puts "Error enabling detailed monitoring: #{e.message}"
@@ -191,25 +191,25 @@ def run_me
191191
puts 'Attempting to stop the instance. ' \
192192
'This might take a few minutes...'
193193
unless instance_stopped?(ec2_client, instance_id)
194-
puts 'Cannot stop the instance. Continuing anyway...'
194+
puts 'Cannot stop the instance. Skipping this step.'
195195
end
196196

197197
puts "\nAttempting to restart the instance. " \
198198
'This might take a few minutes...'
199199
unless instance_restarted?(ec2_client, instance_id)
200-
puts 'Cannot restart the instance. Continuing anyway...'
200+
puts 'Cannot restart the instance. Skipping this step.'
201201
end
202202

203203
puts "\nAttempting to reboot the instance. " \
204204
'This might take a few minutes...'
205205
unless instance_rebooted?(ec2_client, instance_id)
206-
puts 'Cannot reboot the instance. Continuing anyway...'
206+
puts 'Cannot reboot the instance. Skipping this step.'
207207
end
208208

209209
puts "\nAttempting to enable detailed monitoring for the instance..."
210210
unless instance_detailed_monitoring_enabled?(ec2_client, instance_id)
211211
puts 'Cannot enable detailed monitoring for the instance. ' \
212-
'Continuing anyway...'
212+
'Skipping this step.'
213213
end
214214

215215
puts "\nInformation about available instances:"

0 commit comments

Comments
 (0)