Skip to content

Commit 81bee2d

Browse files
committed
Write EC2 setup script.
1 parent 39268df commit 81bee2d

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

s3/ec2_setup.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,125 @@
99

1010
use Aws\Ec2\Ec2Client;
1111

12+
$config['region'] = \Aws\Common\Enum\Region::AP_NORTHEAST_1;
1213
$ec2 = Ec2Client::factory($config);
1314

15+
try {
16+
$options = array(
17+
'ImageId' => 'ami-b1fe9bb0',
18+
'MinCount' => 1,
19+
'MaxCount' => 1,
20+
'KeyName' => 'awsguidebook',
21+
'SecurityGroups' => array(
22+
'webserver' // sg-24040f46
23+
),
24+
'InstanceType' => 't1.micro',
25+
);
26+
$result = $ec2->RunInstances($options);
27+
} catch (Exception $ex) {
28+
exit("Instance Launch failed.: " . $ex->getMessage() . "\n");
29+
}
30+
31+
// インスタンス情報を取得
32+
$instance = $result['Instances'][0];
33+
$instanceId = $instance['InstanceId'];
34+
$availabilityZone = $instance['Placement']['AvailabilityZone'];
35+
36+
echo "Instance ID: $instanceId\n";
37+
echo "Availability Zone: $availabilityZone\n";
38+
39+
// wait for instance ready.
40+
do {
41+
$result = $ec2->describeInstances(array(
42+
'InstanceIds' => array(
43+
$instanceId
44+
)
45+
));
46+
47+
$status = $result['Reservations'][0]['Instances'][0]['State']['Name'];
48+
$runnning = ($status == 'running');
49+
50+
if (!$runnning) {
51+
echo "instance is currently in $status, waiting 10 seconds.\n";
52+
sleep(10);
53+
}
54+
55+
} while(!$runnning);
56+
57+
try {
58+
// ElasticIPを割り当て
59+
$result = $ec2->allocateAddress();
60+
$publicIp = $result['PublicIp'];
61+
echo "allocate IP Address: $publicIp\n";
62+
} catch (Exception $ex) {
63+
exit("Allocate IP Address failed. " . $ex->getMessage() . "\n");
64+
}
65+
66+
try {
67+
// ElasticIPをインスタンスにアタッチ
68+
$ec2->associateAddress(array(
69+
'InstanceId' => $instanceId,
70+
'PublicIp' => $publicIp,
71+
));
72+
echo "Associated IP address : ${publicIp} to ${instanceId}\n";
73+
} catch (Exception $ex) {
74+
exit("IP Address Association failed. " . $ex->getMessage() . "\n");
75+
}
76+
77+
try {
78+
// 先ほど得られたアベイラビリティゾーンに1GiBのボリュームを2つ作成
79+
$res1 = $ec2->createVolume(array(
80+
'Size' => 1,
81+
'AvailabilityZone' => $availabilityZone,
82+
));
83+
$res2 = $ec2->createVolume(array(
84+
'Size' => 1,
85+
'AvailabilityZone' => $availabilityZone,
86+
));
87+
$volumeId1 = $res1['VolumeId'];
88+
$volumeId2 = $res2['VolumeId'];
89+
echo "EBS Volume: $volumeId1, $volumeId2 created.\n";
90+
} catch (Exception $ex) {
91+
exit("EBS Volume create failed. " . $ex->getMessage() . "\n");
92+
}
93+
94+
95+
// wait for volumes ready.
96+
do {
97+
$result = $ec2->describeVolumes(array(
98+
'VolumeIds' => array(
99+
$volumeId1, $volumeId2
100+
)
101+
));
102+
103+
$status1 = $result['Volumes'][0]['State'];
104+
$status2 = $result['Volumes'][1]['State'];
105+
$volumeReady = ($status1 == 'available' && $status2 == 'available');
106+
107+
if (!$volumeReady) {
108+
echo "volumes are currently in ($status1, $status2), waiting 10 seconds.\n";
109+
sleep(10);
110+
}
111+
112+
} while(!$volumeReady);
113+
114+
115+
try {
116+
// ボリュームをインスタンスにアタッチ
117+
$res1 = $ec2->attachVolume(array(
118+
'VolumeId' => $volumeId1,
119+
'InstanceId' => $instanceId,
120+
'Device' => '/dev/sdf',
121+
));
122+
$res2 = $ec2->attachVolume(array(
123+
'VolumeId' => $volumeId2,
124+
'InstanceId' => $instanceId,
125+
'Device' => '/dev/sdg',
126+
));
127+
128+
echo "Volume $volumeId1, $volumeId2 has been attached successfully.\n";
129+
} catch (Exception $ex) {
130+
exit("EBS Attache failed. " . $ex->getMessage() . "\n");
131+
}
132+
133+
echo "Finished!!\n";

0 commit comments

Comments
 (0)