PHP code example of renoki-co / laravel-ec2-metadata

1. Go to this page and download the library: Download renoki-co/laravel-ec2-metadata library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

renoki-co / laravel-ec2-metadata example snippets


use Carbon\Carbon;
use RenokiCo\Ec2Metadata\Ec2Metadata;

if ($termination = Ec2Metadata::terminationNotice()) {
    // The instance is terminating...

    $secondsRemaining = Carbon::parse($termination['time'])->diffInSeconds(now());

    echo "The instance is terminating in {$secondsRemaining} seconds.";
}

use RenokiCo\Ec2Metadata\Ec2Metadata;

Ec2Metadata::version('2016-09-02');

use RenokiCo\Ec2Metadata\Ec2Metadata;

$kernelId = Ec2Metadata::get('kernel-id');

class Ec2Metadata
{
    public static function terminationNotice(): array
    {
        // Expected response is {"action": "terminate", "time": "2017-09-18T08:22:00Z"}
        return static::getJson('/spot/instance-action');
    }
}

use RenokiCo\Ec2Metadata\Ec2Metadata;

Ec2Metadata::macro('kernelId', function () {
    return static::get('kernel-id');
});

$kernelId = Ec2Metadata::kernelId();

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use RenokiCo\Ec2Metadata\Ec2Metadata;

Http::fake([
    'http://169.254.169.254/*' => Http::sequence()
        ->push('some-token', 200)
        ->push('ami-1234', 200),
]);

$this->assertEquals('ami-1234', Ec2Metadata::ami());

Http::assertSentInOrder([
    function (Request $request) {
        return $request->method() === 'PUT' &&
            $request->url() === 'http://169.254.169.254/latest/api/token' &&
            $request->header('X-AWS-EC2-Metadata-Token-TTL-Seconds') === ['21600'];
    },
    function (Request $request) {
        return $request->method() === 'GET' &&
            $request->url() === 'http://169.254.169.254/latest/meta-data/ami-id' &&
            $request->header('X-AWS-EC2-Metadata-Token') === ['some-token'];
    },
]);