|
| 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 |
0 commit comments