renoki-co/laravel-ec2-metadata

Retrieve the EC2 Metadata using Laravel's eloquent syntax.

1.0.0 2022-02-03 20:50 UTC

README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Retrieve the EC2 Metadata using Laravel's eloquent syntax.

🤝 Supporting

If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦

🚀 Installation

You can install the package via composer:

composer require renoki-co/laravel-ec2-metadata

🙌 Usage

The package was made to be easier for you to implement your own methods and keep it simple, without hassling too much about requests.

In this brief example, you can calculate the seconds left until the EC2 Spot instance will be terminated.

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.";
}

Setting Version

The default version of the Ec2Metadata class is latest, but to avoid your code to break due to API changes, define the version to run on.

You can see the list of available versions in IMDSv2 documentation, under Get the available versions of the instance metadata:

use RenokiCo\Ec2Metadata\Ec2Metadata;

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

Calling Custom Endpoints

The IMDSv2 API is pretty complex, and there are some functions you can use from the Ec2Metadata class, just for convenience. When you want to retrieve data from an endpoint that's not implemented, you can either define a macro or use the get() and getJson() functions to retrieve in plain-text or as a JSON-decoded array:

Take this example for retrieving the kernel ID (under /meta-data/kernel-id):

use RenokiCo\Ec2Metadata\Ec2Metadata;

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

To retrieve JSON values, you may call getJson. This will work properly only if the expected value from the endpoint you call will be a JSON-encoded response.

In the implementation, terminationNotice uses the getJson() to retrieve the response:

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

Macros

Alternatively to using get() and getJson(), you can define macros:

use RenokiCo\Ec2Metadata\Ec2Metadata;

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

$kernelId = Ec2Metadata::kernelId();

Testing Your Code

The package is using HTTP Client, a Laravel feature that leverages Guzzle and you can handle requests and test them by mocking responses.

Testing properly your app means you should be fully trained with the AWS EC2's IMDSv2 API, in order to provider appropriate responses.

When pushing the responses in testing, make sure to take into account that the first call would be the token retrieval.

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'];
    },
]);

🐛 Testing

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.

🎉 Credits