Menu
AWS Cloud9
User Guide

Ruby Sample for AWS Cloud9

This sample enables you to run some Ruby scripts in an AWS Cloud9 development environment.

Note

This sample assumes you already have the AWS Cloud9 IDE for your AWS Cloud9 development environment open in your web browser, and that you're using an AWS Cloud9 EC2 development environment that is connected to an Amazon EC2 instance running Amazon Linux. In you're using a different operating system or using an AWS Cloud9 SSH development environment, you might need to adapt this sample's instructions to correctly install and configure this sample's required tools. To create an environment, see Creating an Environment.

When you're using this sample, be sure you're signed in to AWS with the AWS account ID and name and password of the user you created or identified in Team Setup.

Creating this sample may result in charges to your AWS account. These include possible charges for services such as Amazon EC2 and Amazon S3. For more information, see Amazon EC2 Pricing and Amazon S3 Pricing.

Step 1: Install Required Tools

In this step, you install Ruby, which is required to run this sample.

  1. In a terminal session in the AWS Cloud9 IDE, confirm whether Ruby is already installed by running the ruby --version command. (To start a new terminal session, on the menu bar, choose Window, New Terminal.) If successful, the output contains the Ruby version number. If Ruby is installed, skip ahead to Step 2: Add Code.

  2. Run the yum update command to help ensure the latest security updates and bug fixes are installed.

    sudo yum -y update
  3. Install Ruby by running the install command.

    sudo yum -y install ruby

    For more information, see Installing Ruby on the Ruby website.

Step 2: Add Code

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name hello.rb. (To create a file, on the menu bar, choose File, New File. To save the file, choose File, Save.)

puts "Hello, World!" puts "The sum of 2 and 3 is 5." argv0 = ARGV[0] argv1 = ARGV[1] sum = argv0.to_i + argv1.to_i puts "The sum of #{argv0} and #{argv1} is #{sum}."

Step 3: Run the Code

  1. In the AWS Cloud9 IDE, on the menu bar, choose Run, Run Configurations, New Run Configuration.

  2. On the [New] - Idle tab, choose Runner: Auto, and then choose Ruby.

  3. For Command, type hello.rb 5 9. In the code, 5 represents ARGV[0], and 9 represents ARGV[1].

  4. Choose the Run button, and compare your output.

    Hello, World! The sum of 2 and 3 is 5. The sum of 5 and 9 is 14.

            Output of running the Ruby code in the AWS Cloud9 IDE

Step 4: Install and Configure the AWS SDK for Ruby

You can enhance this sample to use the AWS SDK for Ruby to create an Amazon S3 bucket, list your available buckets, and then delete the bucket you just created.

In this step, you install and configure the AWS SDK for Ruby, which provides a convenient way to interact with AWS services such as Amazon S3, from your Ruby code. Before you can install the AWS SDK for Ruby, you must install RubyGems. After you install the AWS SDK for Ruby, you must set up credentials management in your environment. The AWS SDK for Ruby needs these credentials to interact with AWS services.

To install RubyGems

  1. In the AWS Cloud9 IDE, confirm whether RubyGems is already installed by running the gem --version command. If successful, the output contains the RubyGems version number. Otherwise, an error message should be output. If RubyGems is installed, skip ahead to "Step 4.2: Install the AWS SDK for Ruby."

  2. To install RubyGems, run the install command.

    sudo yum -y install gem

    For more information, see Download RubyGems on the RubyGems website.

To install the AWS SDK for Ruby

After you install RubyGems, run the gem install command.

sudo gem install aws-sdk

For more information, see Installing the AWS SDK for Ruby in the AWS SDK for Ruby Developer Guide.

To set up credentials management in your environment

Each time you use the AWS SDK for Ruby to call an AWS service, you must provide a set of credentials with the call. These credentials determine whether the AWS SDK for Ruby has the appropriate permissions to make that call. If the credentials don't cover the appropriate permissions, the call will fail.

In this step, you will store your credentials within the environment. To do this, follow the instructions in Call AWS Services from an Environment, and then return to this topic.

For additional information, see Configuring the AWS SDK for Ruby in the AWS SDK for Ruby Developer Guide.

Step 5: Add AWS SDK Code

In this step, you will add some more code, this time to interact with Amazon S3 to create a bucket, list your available buckets, and then delete the bucket you just created. You will run this code later.

In the AWS Cloud9 IDE, create a file with this content, and save the file with the name s3.rb.

require 'aws-sdk' bucket_name = ARGV[0] s3 = Aws::S3::Client.new(region: 'YOUR_REGION') # Lists all of your available buckets in this AWS Region. def list_my_buckets(s3) resp = s3.list_buckets() puts "My buckets now are:\n\n" resp.buckets.each do |bucket| puts bucket.name end end list_my_buckets(s3) # Create a new bucket. begin puts "\nCreating a new bucket named '#{bucket_name}'...\n\n" s3.create_bucket(bucket: bucket_name) rescue Aws::S3::Errors::BucketAlreadyExists puts "Cannot create the bucket. " + "A bucket with the name '#{bucket_name}' already exists. Exiting." exit(false) end list_my_buckets(s3) # Delete the bucket you just created. puts "\nDeleting the bucket named '#{bucket_name}'...\n\n" s3.delete_bucket(bucket: bucket_name) list_my_buckets(s3)

In the preceding code, replace YOUR_REGION with the ID of an AWS Region. For example, for the US East (Ohio) Region, use us-east-2. For more IDs, see Amazon Simple Storage Service (Amazon S3) in the Amazon Web Services General Reference.

Step 6: Run the AWS SDK Code

  1. In the AWS Cloud9 IDE, on the menu bar, choose Run, Run Configurations, New Run Configuration.

  2. In the [New] - Idle tab, choose Runner: Auto, and then choose Ruby.

  3. For Command, type s3.rb YOUR-BUCKET-NAME , where YOUR-BUCKET-NAME is the name of the bucket you want to create and then delete.

    Note

    Amazon S3 bucket names must be unique across AWS—not just your AWS account.

    
                  Running the AWS SDK for Ruby code in the AWS Cloud9 IDE
  4. Choose the Run button, and compare your output.

    My buckets now are: Creating a new bucket named 'my-test-bucket'... My buckets now are: my-test-bucket Deleting the bucket named 'my-test-bucket'... My buckets now are:

Step 7: Clean Up

To prevent ongoing charges to your AWS account after you're done using this sample, you should delete the environment. For instructions, see Deleting an Environment.