programming amazon web services for beginners (1)
TRANSCRIPT
2
Amazon Web Service Developer Ecosystem
Java
• AWS Java libraries
• AWS toolkit for Eclipse
• …
Ruby
• Ruby gems (RightScale, amazon-ec2, AWS::S3)
• …
PHP
• AWS PHP libraries
• Zend Web development framework supports S3, EC2 & SQS
• …
.NET
• AWS SDK for .NET (libraries, Visual Studio templates)
• Windows on EC2
• …
FOCUS
… and more.
3
AWS Software Development with Java
AWS Java librariesJets3t (James Murty)
Open-source Java toolkit and application suite for Amazon S3 and CloudFront
http://bitbucket.org/jmurty/jets3t/wiki/Home
Typica (D. Kavanagh, Xerox Corporation)Open-Source Java libraries for a broad variety of Amazon Web Services
http://code.google.com/p/typica/
Amazon SDK for JavaOpen-source Java libraries for a broad variety of Amazon Web Services
http://aws.amazon.com/sdkforjava/
AWS toolkit for EclipseEclipse Plug-in
AWS perspective with views “EC2 Instances”, “EC2 AMIs”, et cetera
AWS Project Wizard
4
AWS Toolkit for Eclipse (1)
Sou
rce:
ww
w.e
clip
se.o
rg/d
ownl
oads
/, A
pril
2010
5
AWS Toolkit for Eclipse (2)
1) Go to “Help” > “Install New Software…”2) Enter http://aws.amazon.com/eclipse
Sou
rce:
aw
s.am
azon
.com
/ecl
ipse
/, A
pril
2010
6
AWS Toolkit for Eclipse (3)
Sou
rce:
aw
s.am
azon
.com
/ecl
ipse
/, A
pril
2010
7
AWS Toolkit for Eclipse (4)
Sou
rce:
aw
s.am
azon
.com
/ecl
ipse
/, A
pril
2010
8
CodingIntroduction
9
Amazon Web Service Credentials
10
Programming S3Introduction
11
Basic S3 Concepts
BucketsHTTP PUT
HTTP GET
ObjectsHTTP PUT
HTTP GET
12
S3 Buckets
DNS-compatible bucket namesBucket names must be unique in the S3 universe
Bucket names should not contain underscores (_)
Bucket names should be between 3 and 63 characters long
Bucket names should not end with a dash
Bucket names cannot contain two, adjacent periods
Bucket names cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid)
13
Operations on Buckets: PUT
PUT: Create a new bucketPUT / HTTP/1.1Host: eorg-exercise1.s3.amazonaws.comContent-Length: 0Date: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf…
s3.createBucket(“eorg-exercise1");
14
Operations on Buckets: PUT (2)
Set the Bucket RegionPUT / HTTP/1.1Host: eorg-exercise2.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf3Y…Content-Type: text/plainContent-Length: 124<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <LocationConstraint>EU</LocationConstraint></CreateBucketConfiguration >
CreateBucketRequest request = new CreateBucketRequest("eorg-exercise2");request.setRegion("EU");s3.createBucket(request);
15
Operations on Buckets: PUT (3)
Set Access Control
String bucketName = "eorg-exercise3";CannedAccessControlList acl = CannedAccessControlList.PublicRead;s3.createBucket(bucketName);s3.setBucketAcl(bucketName, acl);
PUT / HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comContent-Length: 0x-amz-acl: public-readDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRep…
16
Operations on Buckets: GET (1)
Return all (up to 1000) of the objects in a bucket
GET / HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRep…Content-Type: text/plain
String bucketName = "eorg-exercise3";ObjectListing list = s3.listObjects(new
ListObjectsRequest().withBucketName(bucketName));for (S3ObjectSummary s : list.getObjectSummaries()) {
System.out.println(" - " + s.getKey() + “ (size = " + s.getSize() + ")");
}
17
Operations on S3 Objects: PUT (1)
PUT /isbn-12345 HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 15B4D3461F177624206A:xQE0diMbLRep…Content-Type: text/plainContent-Length: 135Content-MD5: JBVusP8u0QVhBvsvxNDthQ==[…data…]
String bucketName = "eorg-exercise3";String key = "isbn-12345";s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile(“Program-AWS")));
18
Operations on S3 Objects: PUT (2)
Object Versioning
BucketVersioningConfiguration config = new BucketVersioningConfiguration();config.setStatus(BucketVersioningConfiguration.ENABLED);SetBucketVersioningConfigurationRequest configReq = new SetBucketVersioningConfigurationRequest(bucketName, config);s3.setBucketVersioningConfiguration(configReq);
PutObjectResult result = s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile("Program-
AWS-2")));System.out.println("Version ID: "+result.getVersionId());
19
Operations on S3 Objects: GET
GET /isbn-12345 HTTP/1.1Host: eorg-exercise3.s3.amazonaws.comDate: Wed, 27 Oct 2010 12:00:00 GMTAuthorization: AWS 02236Q3V0WHVSRW0EXG2:0RQf4/cR…
String bucketName = "eorg-exercise3";String key = "isbn-12345";S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());displayTextInputStream(object.getObjectContent());
20
Note: Access Control Lists
“Note: Bucket and object ACLs are completely independent; an object does not inherit the ACL from its bucket. For example, if you create a bucket and grant write access to another user, you will not be able to access the user's objects unless the user explicitly grants access. This also applies if you grant anonymous write access to a bucket. Only the user "anonymous" will be able to access objects the user created unless permission is explicitly granted to the bucket owner.
Important: We highly recommend that you do not grant the anonymous group write access to your buckets as you will have no control over the objects others can store and their associated charges.”
21
Note: Mediated Access with Signed URLs
Gatekeeper1.) Get signed URL
S32.) Retrieve S3 object
Cf. James Murty: “Programming Amazon Web Services”, fig. 4-2
22
Programming EC2Introduction
23
Basic EC2 Concepts
EC2 Flow
Amazon Machine Images (AMIs)
Regions & Availability Zones
Run EC2 Instances
Terminate EC2 Instances
24
EC2 Flow
Source: EC2 Developer Guide 2010-08-3
25
Amazon Machine Images (AMIs)
Amazon Machine Images (AMIs) are virtual machine images with a root device which is stored either in
Amazon S3, or
Amazon Elastic Block Store (EBS)
Pre-configured public AMIs are provided by the AWS community
26
Amazon Machine Images (2)
27
Regions
“Amazon EC2 provides multiple Regions so you can launch Amazon EC2 instances in locations that meet your requirements. Each Amazon EC2 Region is designed to be completely isolated from the other Amazon EC2 Regions. This achieves the greatest possible failure independence and stability, and it makes the locality of each EC2 resource unambiguous.”
28
Availability Zones
“[F]ailures can occur that affect the availability of instances that are in the same location. Although this is rare, if you host all your Amazon EC2 instances in a single location that is affected by such a failure, your instances will be unavailable.
For example, if you have instances distributed across three Availability Zones and one of the instances fails, you can design your application so the instances in the remaining Availability Zones handle any requests.”
29
Run EC2 Instances (1)
ec2 = new AmazonEC2Client(credentials);RunInstancesRequest req = new RunInstancesRequest();req.setImageId("ami-480df921");req.setInstanceType("t1.micro");
req.setMinCount(1);req.setMaxCount(1);
RunInstancesResult res = ec2.runInstances(req);System.out.println(res.toString());
30
Run EC2 Instances (2)
HTTP POST Request
https://ec2.amazonaws.com/?Action=RunInstances&ImageId=ami-480df921&MaxCount=1&MinCount=1&Placement.AvailabilityZone=eu-west-1b&SignatureMethod=HmacSHA256& AWSAccessKeyId=123…
31
Run EC2 Instances (3)
AWS Management Console
Eclipse Management Console
32
Run EC2 Instances (4)
Run EC2 Instances in the EU Region
ec2 = new AmazonEC2Client(credentials);ec2.setEndpoint("https://eu-west-1.ec2.amazonaws.com");...Placement p = new Placement();p.setAvailabilityZone("eu-west-1b");req.setPlacement(p);
33
Don’t forget to terminate…
TerminateInstancesRequest req = new TerminateInstancesRequest().withInstanceIds(id);ec2.terminateInstances(req);
34
References
S3 Developer Guide 2006-03-01
S3 API Reference 2006-03-01
EC2 Developer Guide 2010-08-31
EC2 API Reference 2010-08-31