Environment - Windows Server 2012, Scriptcase 9.10.009 (13)
I am working on an application that will allow our users to start/stop/reboot EC2 instances on AWS cloud (yes, they can use the AWS console, but I want something easier to use). I am using the AWS PHP SDK as an external library and have it working. However, I am having trouble determining where can I put the code that creates the $ec2client class where it can be used throughout my grid application. Below is the code I have on the onRecord event. Noticed that I created an execute once code block so it does not execute that block that creates the ec2client class for every record. I tried putting that block on onScriptinit but the $ec2client is not recognized when I use it in the Onrecord event hence why I did the execute once block to create the class. I also want to use the $ec2class on the “action bar” so that I can stop/start/reboot the instance for each record on the grid, but I have to add all this code for every action button. So, my question is does Scriptcase have a better way where I can use the AWS external library where the $ec2client class can be used without recreating it multiple times? It seems that I should be able to include the library, create the ec2client class only once then use it throughout my application.
onRecord event code
/*
- This macro includes a PHP file from a library in the application. In this instance, the AWS SDK in being included.
*/
sc_include_library(“prj”, “awssdk”, “aws-autoloader.php”, true,true);
// only execute this code block once
if (!defined(‘EXECUTED’)) {
$region =‘us-east-2’;
$version = ‘latest’;
$credentials = new Aws\Credentials\Credentials(‘ACCESS_KEY’,‘SECRET_KEY’);
// Create the ec2 client class
$ec2Client = new Aws\Ec2\Ec2Client([
‘region’ => $region,
‘version’ => $version,
‘credentials’ => $credentials
]);
define('EXECUTED', true);
}
//Get all instances details
try {
$result = $ec2Client->describeInstances([
‘InstanceIds’ => [
{inst_id},
],
]);
foreach ($result[‘Reservations’] as $reservation) {
foreach ($reservation[‘Instances’] as $instance) {
$instanceName = ‘’;
foreach ($instance[‘Tags’] as $tag) {
if ($tag[‘Key’] == ‘Name’) {
$instanceName = $tag[‘Value’];
}
}
{inst_name} = $instanceName;
{inst_type} = $instance['InstanceType'];
{inst_state} = $instance['State']['Name'];
{inst_priv_ip} = $instance['PrivateIpAddress'];
{inst_launch_time} = $instance['LaunchTime'];
// Add more details as needed
}
}
} catch (Exception $ex) {
{aws_error} = ‘Your access key is invalid or an error occurred.’;
}