User guide
.withMaxCount(1)
.withKeyName("my-key-pair")
.withSecurityGroups("my-security-group");
withImageId
The ID of the AMI. For a list of public AMIs provided by Amazon, see Amazon Machine Images.
withInstanceType
An instance type that is compatible with the specified AMI. For more information, see Instance
Types in the Amazon EC2 User Guide for Linux Instances.
withMinCount
The minimum number of EC2 instances to launch. If this is more instances than Amazon EC2
can launch in the target Availability Zone, Amazon EC2 launches no instances.
withMaxCount
The maximum number of EC2 instances to launch. If this is more instances than Amazon EC2
can launch in the target Availability Zone, Amazon EC2 launches the largest possible number
of instances above MinCount.You can launch between 1 and the maximum number of instances
you're allowed for the instance type. For more information, see How many instances can I run
in Amazon EC2 in the Amazon EC2 General FAQ.
withKeyName
The name of the EC2 key pair. If you launch an instance without specifying a key pair, you can't
connect to it. For more information, see Create a Key Pair (p. 29).
withSecurityGroups
One or more security groups. For more information, see Create an Amazon EC2 Security
Group (p. 27).
2. Launch the instances by passing the request object to the runInstances method.The method returns
a RunInstancesResult object, as follows:
RunInstancesResult runInstancesResult =
amazonEC2Client.runInstances(runInstancesRequest);
After your instance is running, you can connect to it using your key pair. For more information, see Connect
to Your Linux Instance. in the Amazon EC2 User Guide for Linux Instances.
Using IAM Roles to Grant Access to AWS
Resources on Amazon EC2
All requests to Amazon Web Services (AWS) must be cryptographically signed using credentials issued
by AWS.You can use IAM roles to conveniently grant secure access to AWS resources from your EC2
instances.
This topic provides information about how to use IAM roles with Java SDK applications running on EC2.
For more information about IAM instances, see IAM Roles for Amazon EC2 in the Amazon Elastic Compute
Cloud User Guide.
The Default Provider Chain and EC2 Instance Profiles
If your application creates an AWS client using the default constructor, then the client will search for
credentials using the default credentials provider chain, in the following order:
Version v1.0.0
30
AWS SDK for Java Developer Guide
Using IAM Roles for EC2 Instances