1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AMIからオンデマンドインスタンスを作成する。[SDK for Java]

Last updated at Posted at 2014-11-05

はじめに

EC2には下記3つのインスタンスが存在します。
1.オンデマンドインスタンス
2.スポットインスタンス
3.リザーブドインスタンス
ここではオンデマンドインスタンスを作成する手順について説明します。

SDK for Java


import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;

public class Ec2CreateInstanceFromAmi {

	public static void main(String[] args) {

		AWSCredentialsProvider provider = new ProfileCredentialsProvider("uzr");

		AmazonEC2 ec2_tokyo = Region.getRegion(Regions.AP_NORTHEAST_1)
				.createClient(AmazonEC2Client.class, provider,
						new ClientConfiguration());

		RunInstancesRequest instanceRequest = new RunInstancesRequest()
				.withImageId("ami-61a29d60")
				.withInstanceType(InstanceType.T2Micro)
				.withMinCount(1)
				.withMaxCount(1)
				.withSecurityGroupIds("launch-wizard-1")
				.withKeyName("uzr_key");

		RunInstancesResult result = ec2_tokyo.runInstances(instanceRequest);
		
		System.out.println(result.getReservation());
	}
}

実行結果


{ReservationId: r-596cc640,OwnerId: 117364780807,Groups: [],GroupNames: [],Instances: [{InstanceId: i-5dcf8faf,ImageId: ami-61a29d60,State: {Code: 0,Name: pending},PrivateDnsName: ip-172-31-20-119.ap-northeast-1.compute.internal,PublicDnsName: ,StateTransitionReason: ,KeyName: uzr_key,AmiLaunchIndex: 0,ProductCodes: [],InstanceType: t2.micro,LaunchTime: Wed Nov 05 13:13:39 JST 2014,Placement: {AvailabilityZone: ap-northeast-1c,GroupName: ,Tenancy: default},Monitoring: {State: disabled},SubnetId: subnet-40d1c006,VpcId: vpc-c353a0a6,PrivateIpAddress: 172.31.20.119,StateReason: {Code: pending,Message: pending},Architecture: x86_64,RootDeviceType: ebs,RootDeviceName: /dev/xvda,BlockDeviceMappings: [],VirtualizationType: hvm,ClientToken: dad5e3c1-9d17-4fce-b83c-e08d2bf2002b,Tags: [],SecurityGroups: [{GroupName: launch-wizard-1,GroupId: sg-71c21714}],SourceDestCheck: true,Hypervisor: xen,NetworkInterfaces: [{NetworkInterfaceId: eni-7f7ecd26,SubnetId: subnet-40d1c006,VpcId: vpc-c353a0a6,Description: ,OwnerId: 117364780807,Status: in-use,MacAddress: 0a:2c:f8:ff:e3:cd,PrivateIpAddress: 172.31.20.119,PrivateDnsName: ip-172-31-20-119.ap-northeast-1.compute.internal,SourceDestCheck: true,Groups: [{GroupName: launch-wizard-1,GroupId: sg-71c21714}],Attachment: {AttachmentId: eni-attach-f04a6af6,DeviceIndex: 0,Status: attaching,AttachTime: Wed Nov 05 13:13:39 JST 2014,DeleteOnTermination: true},PrivateIpAddresses: [{PrivateIpAddress: 172.31.20.119,PrivateDnsName: ip-172-31-20-119.ap-northeast-1.compute.internal,Primary: true,}]}],EbsOptimized: false,}]}

補足

プロパティ 説明
imageid AMIのIDです。
instanceType インスタンスのタイプです。無料で利用できるt2.microを指定しています。
count インスンタンスの数です
security group id ここではSSHとHTTPを許可するセキュリティグループを指定しています。
keyName keypairの名称です。前に作ったインスタンスと同じkeyでログインしたかったので既存のものを指定しています。

参考

インスタンスタイプについて
スポットインスタンスの作り方

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?