Trying to call AWS API via PHP

0 votes

Trying to call an aws api which gives the master IP address. When I'm calling this api via command it is working fine. Using powershell for this. But, I want to call this api UI using php.While calling via php I am not able to call this api.

My powershell script is,

Clear-Host
Clear-History
$Username = "**"
$Password = "***"
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$ips = @() 

Write-Host "----------------------------------"

Try
{
    echo "Creating Instances will start now..."
    python C:\AWS\Distributed-setup-3\CreateInstance.py
}
Catch
{
    echo "Instance not created"
    exit
}

Write-Host "----------------------------------"

echo "Creating setup...."
Start-Sleep -Seconds 60

python C:\AWS\Distributed-setup-3\statusCheck.py

Start-Sleep -Seconds 60

$content = Get-Content C:\AWS\Distributed-setup-3\InstanceId.txt
echo $content

Write-Host "----------------------------------"

ForEach ($id In $content){
    echo "execute.ps1 id"
    echo $id    
    $instances = aws ec2 describe-instances --instance-ids $id --query Reservations[].Instances[].PublicIpAddress 
    $instance = $instances[1]
    $instance = $instance -replace '"' , ""
    $instance =  $instance.Trim()
    $ips = $ips+$instance
}

Write-Host "----------------------------------"

Clear-Content 'C:\AWS\Distributed-setup-3\ipconfig.txt'

ForEach ($i In $ips){
    #echo $i
    if($i -eq ($ips[0])){
        Write-Host "Master is "$i
    }
    else
    {
        Write-Host "Client "$i
        $index = [array]::indexof($ips,$i)
        $index = $index+1
        if($ips[$index] -eq $null){
            $i | Add-Content 'C:\AWS\Distributed-setup-3\ipconfig.txt'
        }
        else{
            $i = $i+","
            $i | Add-Content 'C:\AWS\Distributed-setup-3\ipconfig.txt'
        }
    }
}

Write-Host "----------------------------------"

aws s3 cp C://AWS/Distributed-setup-3/ipconfig.txt s3://s3merahkee
aws s3 cp C://AWS/Distributed-setup-3/Input.yaml s3://s3merahkee


Write-Host "----------------------------------"

Write-Host "Executing jmeter distributed test on remote system" $ips

Write-Host "----------------------------------"

ForEach ($ip In $ips){ 
   if($ip -eq ($ips[0])){
        #echo "Executing hub"
        Write-Host "Master ip is "$ip
   }
   else
   {
        #Write-Host "Executing slave "$ip
        Start-Job "C:\AWS\Distributed-setup-3\instance_2.ps1" -ArgumentList $ip
   } 
}

Start-Sleep -Seconds 60

Write-Host "----------------------------------"

Get-job | Receive-Job

Write-Host "----------------------------------"

$hub = $ips[0]
echo -n "Executing Master :"$hub

Start-Sleep -Seconds 10
Invoke-Command -ComputerName $hub -ScriptBlock { cmd.exe /c C:\Users\Administrator\Desktop\gitclone.bat } -credential $Cred

Write-Host "----------------------------------"

ForEach ($ip In $ips){
    echo -n "Executing on remote system for reports " $ip
    Try
    {
        Invoke-Command -ComputerName $ip -ScriptBlock { cmd.exe /c C:\Users\Administrator\Desktop\report.bat } -credential $Cred    
    }
    Catch{
        echo "Unable to connect check the remote connection permission"
    }
}

Write-Host "----------------------------------"

ForEach ($id In $content){
    $instances = aws ec2 describe-instances --instance-ids $id --query 'Reservations[].Instances[].PrivateIpAddress' 
    $instance = $instances[1]
    $instance = $instance -replace '"' , ""
    $i =  $instance.Trim()
    #echo $i
    aws s3 cp  s3://s3merahkee/Logs/$i C:\AWS\Distributed-setup-3\Reports --recursive
    cd C:\AWS\Distributed-setup-3\Reports
    Rename-Item jmeter-server.log jmeter-server_$i.log
}

Write-Host "----------------------------------"

echo "Terminating instance"
ForEach ($id In $content){
   aws ec2 terminate-instances --instance-ids $id
}

Write-Host "----------------------------------"
Write-Host "Execution completed"

In php file,

<?php

        ini_set('max_execution_time', 500);
        $output= shell_exec('aws ec2 describe-instances --instance-ids i-03bb2e6b53fcd4335 --query Reservations[].Instances[].PublicIpAddress');
        echo( '<pre>' );
        echo( $output );
        echo( '</pre>' );          
 ?>

Here the api is,

aws ec2 describe-instances --instance-ids $id --query Reservations[].Instances[].PublicIpAddress 

Trying to do it quite a lot time, still unsuccessful in this, can anyone help  ?

Jun 6, 2018 in AWS by Flying geek
• 3,280 points
1,742 views

1 answer to this question.

0 votes

Try using AWS SDK for PHP, Link is here:  AMAZON DOCS

Like in this example:

require 'vendor/autoload.php';
use Aws\Ec2\Ec2Client;

$ec2Client = new Ec2Client([
'region' => 'us-west-2',
'version' => '2016-11-15',
'profile' => 'default'
]);
$result = $ec2Client->describeInstances(
  array('InstanceIds' => $ids)
);

Check out this AWS Certification program to learn more about AWS API.

Thanks!

answered Jun 6, 2018 by Cloud gunner
• 4,670 points

Related Questions In AWS

0 votes
1 answer

How to test code written against AWS API

The actual uploading and the tests that ...READ MORE

answered May 17, 2018 in AWS by Flying geek
• 3,280 points
664 views
+1 vote
3 answers

Not able to pass params POST to AWS Lambda from Amazon API Gateway

For this template : application/x-www-form-urlencoded  This line below will ...READ MORE

answered Jun 13, 2018 in AWS by Cloud gunner
• 4,670 points
8,744 views
0 votes
1 answer

How to Enable CORS for an AWS API Gateway Resource

Since you're using Lambda Proxy integration for ...READ MORE

answered Jul 10, 2018 in AWS by Hammer
• 360 points
4,063 views
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
0 votes
2 answers
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP