You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.8 KiB
74 lines
1.8 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\CloudFormation;
|
|
|
|
use Aws\CloudFormation\CloudFormationClient;
|
|
|
|
class Client
|
|
{
|
|
protected $cfn;
|
|
|
|
public function __construct(?string $region = null, ?string $profile = null)
|
|
{
|
|
$this->cfn = new CloudFormationClient([
|
|
'version' => 'latest',
|
|
'region' => $region ?? env('AWS_REGION'),
|
|
'profile' => $profile ?? env('AWS_PROFILE')
|
|
]);
|
|
}
|
|
|
|
public function getStack(string $stack): ?Stack
|
|
{
|
|
try
|
|
{
|
|
$stack_description = $this->cfn->describeStacks([
|
|
'StackName' => $stack
|
|
]);
|
|
}
|
|
catch (\Aws\Exception\AwsException $ex)
|
|
{
|
|
if ($ex->getAwsErrorMessage() == 'Stack with id '.$stack.' does not exist')
|
|
{
|
|
return null;
|
|
}
|
|
throw new \Exception($ex->getAwsErrorMessage(), $ex->getAwsErrorCode());
|
|
}
|
|
return new Stack($stack_description['Stacks'][0]);
|
|
}
|
|
|
|
public function createStack(string $stack_name, string $template_body, array $parameters = []): Stack
|
|
{
|
|
$response = $this->cfn->createStack($this->buildStackRequest($stack_name, $template_body, $parameters));
|
|
return $this->getStack($response['StackId']);
|
|
}
|
|
|
|
public function updateStack(string $stack_name, string $template_body, array $parameters = []): Stack
|
|
{
|
|
$response = $this->cfn->updateStack($this->buildStackRequest($stack_name, $template_body, $parameters));
|
|
return $this->getStack($response['StackId']);
|
|
}
|
|
|
|
protected function buildStackRequest(string $stack_name, string $template_body, array $parameters = []): array
|
|
{
|
|
$request = [
|
|
'StackName' => $stack_name,
|
|
'TemplateBody' => $template_body,
|
|
'Capabilities' => ['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'],
|
|
'TimeoutInMinutes' => 5
|
|
];
|
|
if (sizeof($parameters))
|
|
{
|
|
$request['Parameters'] = [];
|
|
foreach ($parameters as $k => $v)
|
|
{
|
|
$request['Parameters'][] = [
|
|
'ParameterKey' => $k,
|
|
'ParameterValue' => $v
|
|
];
|
|
}
|
|
}
|
|
return $request;
|
|
}
|
|
}
|
|
|