Use AWS Cloud9 with the AWS SDK for Ruby
AWS 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 AWS Cloud9 with the AWS SDK for Ruby to write and run your Ruby code by using a browser. AWS Cloud9 includes tools such as a code editor and terminal. Because the AWS 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 AWS Cloud9, see the AWS Cloud9 User Guide.
Follow these instructions to set up AWS Cloud9 with the AWS SDK for Ruby:
Step 1: Set up your AWS account to use AWS Cloud9
To use AWS Cloud9, sign in to the AWS Cloud9 console from the AWS Management Console.
Note
If you are using AWS 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 AWS account to access AWS Cloud9 and sign in to the AWS Cloud9 console, see Team Setup for AWS Cloud9 in the AWS Cloud9 User Guide.
Step 2: Set up your AWS Cloud9 development environment
After you sign in to the AWS Cloud9 console, use the console to create an AWS Cloud9 development environment. After you create the environment, AWS Cloud9 opens the IDE for that environment.
For details, see Creating an Environment in AWS Cloud9 in the AWS 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 AWS 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 AWS 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 AWS SDK for Ruby
After AWS Cloud9 opens the IDE for your development environment, use the terminal window to set up the AWS SDK for Ruby in your environment.
You can install the AWS 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 AWS services you use. These are named like
aws-sdk-
and the complete
list is found in the Supported
Servicesservice_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 AWS 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 AWS 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 AWS SDK for Ruby into the AWS Cloud9 development environment.
To download a copy of all the code examples used in the official AWS 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 AWS 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 AWS Cloud9, you can click on the bucket_list.rb
directly in the terminal window.
Step 5: Run example code
To run code in your AWS Cloud9 development environment, choose the Run
button in the top menu bar. AWS Cloud9 will automatically detect the .rb
file extension and use the Ruby runner to run the code. For more information about running
code in AWS Cloud9, see Run
Your Code in the AWS 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. AWS 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. AWS Cloud9 detects that your file extension is
.rb
and selects the Ruby Runner to run your code.
Any output generated from the running code displays in the tab.
To test out many other Amazon S3 operations, check out the AWS Code Examples Repository