-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathEC2Service.php
185 lines (158 loc) · 5.38 KB
/
EC2Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[php.example_code.ec2.service.Ec2Service]
namespace Ec2;
use Aws\Ec2\Ec2Client;
use Aws\Ec2\Exception\Ec2Exception;
class EC2Service
{
protected Ec2Client $ec2Client;
protected string $region;
public function getRegion(): string
{
return $this->region;
}
public function __construct($client = null, $region = 'us-west-2', $version = 'latest', $profile = 'default')
{
if (gettype($client) == Ec2Client::class) {
$this->ec2Client = $client;
return;
}
$this->ec2Client = new Ec2Client([
'region' => $region,
'version' => $version,
'profile' => $profile,
]);
$this->region = $region;
/* Inline declaration example
// snippet-start:[php.example_code.ec2.basics.createClient]
$ec2Client = new Ec2Client(['region' => 'us-west-2', 'version' => 'latest', 'profile' => 'default']);
// snippet-end:[php.example_code.ec2.basics.createClient]
*/
}
// snippet-start:[php.example_code.ec2.service.createVpcEndpoint]
/**
* @param string $serviceName
* @param string $vpcId
* @param array $routeTableIds
* @return array
*/
public function createVpcEndpoint(string $serviceName, string $vpcId, array $routeTableIds): array
{
try {
$result = $this->ec2Client->createVpcEndpoint([
'ServiceName' => $serviceName,
'VpcId' => $vpcId,
'RouteTableIds' => $routeTableIds,
]);
return $result["VpcEndpoint"];
} catch(Ec2Exception $caught){
echo "There was a problem creating the VPC Endpoint: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}
// snippet-end:[php.example_code.ec2.service.createVpcEndpoint]
// snippet-start:[php.example_code.ec2.service.createVpc]
/**
* @param string $cidr
* @return array
*/
public function createVpc(string $cidr): array
{
try {
$result = $this->ec2Client->createVpc([
"CidrBlock" => $cidr,
]);
return $result['Vpc'];
}catch(Ec2Exception $caught){
echo "There was a problem creating the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}
// snippet-end:[php.example_code.ec2.service.createVpc]
// snippet-start:[php.example_code.ec2.service.deleteVpc]
/**
* @param string $vpcId
* @return void
*/
public function deleteVpc(string $vpcId)
{
try {
$this->ec2Client->deleteVpc([
"VpcId" => $vpcId,
]);
}catch(Ec2Exception $caught){
echo "There was a problem deleting the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}
// snippet-end:[php.example_code.ec2.service.deleteVpc]
// snippet-start:[php.example_code.ec2.service.deleteVpcEndpoint]
/**
* @param string $vpcEndpointId
* @return void
*/
public function deleteVpcEndpoint(string $vpcEndpointId)
{
try {
$this->ec2Client->deleteVpcEndpoints([
"VpcEndpointIds" => [$vpcEndpointId],
]);
}catch (Ec2Exception $caught){
echo "There was a problem deleting the VPC Endpoint: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}
// snippet-end:[php.example_code.ec2.service.deleteVpcEndpoint]
// snippet-start:[php.example_code.ec2.service.describeRouteTables]
/**
* @param array $routeTableIds
* @param array $filters
* @return array
*/
public function describeRouteTables(array $routeTableIds = [], array $filters = []): array
{
$parameters = [];
if($routeTableIds){
$parameters['RouteTableIds'] = $routeTableIds;
}
if($filters){
$parameters['Filters'] = $filters;
}
try {
$paginator = $this->ec2Client->getPaginator("DescribeRouteTables", $parameters);
$contents = [];
foreach ($paginator as $result) {
foreach ($result['RouteTables'] as $object) {
$contents[] = $object['RouteTableId'];
}
}
}catch (Ec2Exception $caught){
echo "There was a problem paginating the results of DescribeRouteTables: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
return $contents;
}
// snippet-end:[php.example_code.ec2.service.describeRouteTables]
// snippet-start:[php.example_code.ec2.service.waitForVpcAvailable]
/**
* @param string $VpcId
* @return void
*/
public function waitForVpcAvailable(string $VpcId): void
{
try {
$waiter = $this->ec2Client->getWaiter("VpcAvailable", [
'VpcIds' => [$VpcId],
]);
$promise = $waiter->promise();
$promise->wait();
}catch(Ec2Exception $caught){
echo "There was a problem waiting for the VPC: {$caught->getAwsErrorMessage()}\n";
throw $caught;
}
}
// snippet-end:[php.example_code.ec2.service.waitForVpcAvailable]
}
// snippet-end:[php.example_code.ec2.service.Ec2Service]