
Note
AWS CloudFormation does not support calling its API with an IAM role.You must call the AWS
CloudFormation API as a regular IAM user.
Walkthrough: Using IAM Roles to Retrieve an
Amazon S3 Object from an EC2 Instance
In this walkthrough, we'll begin with a program that retrieves an object from Amazon S3 using regular
account credentials.Then, we'll modify it to use IAM roles for EC2 instances.
Sample Program with Credentials
Here is our starting program, which retrieves an object from an Amazon S3 bucket.The following code
explicitly specifies credentials in the call to the Amazon S3 client constructor.
require 'rubygems'
require 'aws-sdk'
s3 = AWS::S3.new(
:access_key_id => 'AKIAIOSFODNN7EXAMPLE',
:secret_access_key => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
bucket_name = 'text-content'
obj_name = 'text-object.txt'
document = s3.buckets[bucket_name].objects[obj_name]
File.open(obj_name, "w") do |f|
f.write(document.read)
end
puts "'#{obj_name}' copied from S3."
To test the program locally
1. Substitute your AWS credentials for the values of :access_key_id and :secret_access_key.
2. Substitute the names of an Amazon S3 bucket and text object associated with your AWS account
for the values of bucket_name and obj_name, respectively.
For instructions about how to create an Amazon S3 bucket and upload an object, see the Amazon
Simple Storage Service Getting Started Guide.
3. Run the program with the AWS SDK for Ruby and a Ruby interpreter. For information about setting
up the SDK, see Getting Started (p. 4).
For example, if you've saved the code in a file called get-object.rb, run it by using cd on the
command-line (terminal) to change to the directory in which you saved the file, and then type ruby
get-object.rb.
Version v1.0.0
11
AWS SDK for Ruby Developer Guide
Walkthrough: Using IAM Roles to Retrieve an Amazon
S3 Object from an EC2 Instance
Comentários a estes Manuais