Use Amazon Cloud9 with the Amazon SDK for Ruby
Amazon Cloud9 is a web-based integrated development environment (IDE) that contains a collection of tools that you use to code, build, run, test, debug, and release software in the cloud. You can use Amazon Cloud9 with the Amazon SDK for Ruby to write and run your Ruby code by using a browser. Amazon Cloud9 includes tools such as a code editor and terminal. Because the Amazon Cloud9 IDE is cloud based, you can work on your projects from your office, home, or anywhere by using an internet-connected machine. For general information about Amazon Cloud9, see the Amazon Cloud9 User Guide.
Follow these instructions to set up Amazon Cloud9 with the Amazon SDK for Ruby:
Step 1: Set up your Amazon Web Services account to use Amazon Cloud9
To use Amazon Cloud9, sign in to the Amazon Cloud9 console from the Amazon Web Services Management Console.
Note
If you are using Amazon IAM Identity Center to authenticate, you might need to add the required
permission of iam:ListInstanceProfilesForRole
to the user-attached policy
in the IAM console.
To set up an IAM entity in your Amazon account to access Amazon Cloud9 and sign in to the Amazon Cloud9 console, see Team Setup for Amazon Cloud9 in the Amazon Cloud9 User Guide.
Step 2: Set up your Amazon Cloud9 development environment
After you sign in to the Amazon Cloud9 console, use the console to create an Amazon Cloud9 development environment. After you create the environment, Amazon Cloud9 opens the IDE for that environment.
For details, see Creating an Environment in Amazon Cloud9 in the Amazon Cloud9 User Guide.
Note
As you create your environment in the console for the first time, we recommend that you choose the option to Create a new instance for environment (EC2). This option tells Amazon Cloud9 to create an environment, launch an Amazon EC2 instance, and then connect the new instance to the new environment. This is the fastest way to begin using Amazon Cloud9.
If the terminal isn’t already open in the IDE, open it. On the menu bar in the IDE, choose Window, New Terminal. You can use the terminal window to install tools and build your applications.
Step 3: Set up the Amazon SDK for Ruby
After Amazon Cloud9 opens the IDE for your development environment, use the terminal window to set up the Amazon SDK for Ruby in your environment.
You can install the Amazon SDK for Ruby as you would any Ruby gem. The gems are available at RubyGemsaws-sdk
gem is large and may take over an hour.
We recommend only installing the gems for the Amazon Web Services you use. These are named like
aws-sdk-
and the complete
list is found in the Supported
Services table of the Amazon SDK for Ruby README file. For example, the gem for interfacing
with the Amazon S3 service is directly available at service_abbreviation
aws-sdk-s3
Ruby version manager
Instead of using system Ruby, we recommend using a Ruby version manager such as the following:
For example, if you are using an Amazon Linux 2 operating system, the following commands can be used to update RVM, list the available Ruby versions, then choose the version you want to use for development with the Amazon SDK for Ruby. The minimum required Ruby version is 2.3.
$
rvm get head
$
rvm list known
$
rvm install
ruby-3.1.3
$
rvm --default use
3.1.3
Bundler
If you use Bundler
-
Install Bundler and create the
Gemfile
:$
gem install bundler
$
bundle init
-
Open the created
Gemfile
and add agem
line for each Amazon service gem your code will use. To follow along with the Amazon S3 example, add the following line to the bottom of the file:gem "aws-sdk-s3"
-
Save the Gemfile.
-
Install the dependencies specified in your
Gemfile
:$
bundle install
Step 4: Download example code
Use the terminal window to download example code for the Amazon SDK for Ruby into the Amazon Cloud9 development environment.
To download a copy of all the code examples used in the official Amazon SDK documentation into your environment’s root directory, run the following command:
$
git clone https://github.com/awsdocs/aws-doc-sdk-examples.git
The code examples for the Amazon SDK for Ruby are located at
ENVIRONMENT_NAME/aws-doc-sdk-examples/ruby
directory, where
ENVIRONMENT_NAME
is the name of your development
environment.
To follow along using an Amazon S3 example, we recommend starting with code example
ENVIRONMENT_NAME/aws-doc-sdk-examples/ruby/example_code/s3/bucket_list.rb
.
Use the terminal window to navigate to the s3
directory and list the files.
$
cd aws-doc-sdk-examples/ruby/example_code/s3
$
ls
To open the file in Amazon Cloud9, you can click on the bucket_list.rb
directly in the terminal window.
For more support in understanding code examples, see Amazon SDK for Ruby Code Examples.
Step 5: Run example code
To run code in your Amazon Cloud9 development environment, choose the Run
button in the top menu bar. Amazon Cloud9 will automatically detect the .rb
file extension and use the Ruby runner to run the code. For more information about running
code in Amazon Cloud9, see Run
Your Code in the Amazon Cloud9 User Guide.
In the following screenshot, note these basic areas:
-
1: Run. The Run button is located on the top menu bar. This opens a new tab for your results.
Note
You can also manually create new run configurations. On the menu bar, choose Run, Run Configurations, New Run Configuration.
-
2: Command. Amazon Cloud9 populates the Command text box with the path and file name to the file you run. If your code expects any command line parameters to be passed in, these can be added to the command line in the same way that you would when running the code through a terminal window.
-
3: Runner. Amazon Cloud9 detects that your file extension is
.rb
and selects the Ruby Runner to run your code.
![Choosing Run from the top menu bar of the Amazon Cloud9 IDE opens a new tab with the Command box populated and the Ruby runner already selected.](images/cloud9_runconfig.png)
Any output generated from the running code displays in the tab.