PHPackages                             renoki-co/laravel-ec2-metadata - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. renoki-co/laravel-ec2-metadata

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

renoki-co/laravel-ec2-metadata
==============================

Retrieve the EC2 Metadata using Laravel's eloquent syntax.

1.0.0(4y ago)43[3 PRs](https://github.com/renoki-co/laravel-ec2-metadata/pulls)Apache-2.0PHPPHP ^8.0CI passing

Since Sep 17Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/renoki-co/laravel-ec2-metadata)[ Packagist](https://packagist.org/packages/renoki-co/laravel-ec2-metadata)[ Docs](https://github.com/renoki-co/laravel-ec2-metadata)[ GitHub Sponsors](https://github.com/rennokki)[ RSS](/packages/renoki-co-laravel-ec2-metadata/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (6)Versions (7)Used By (0)

Laravel EC2 Metadata
====================

[](#laravel-ec2-metadata)

[![CI](https://github.com/renoki-co/laravel-ec2-metadata/workflows/CI/badge.svg?branch=master)](https://github.com/renoki-co/laravel-ec2-metadata/workflows/CI/badge.svg?branch=master)[![codecov](https://camo.githubusercontent.com/7df333bd1ac1d801cef704525a00b07b63679378c2701338ab659e42d4fd9e40/68747470733a2f2f636f6465636f762e696f2f67682f72656e6f6b692d636f2f6c61726176656c2d6563322d6d657461646174612f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/renoki-co/laravel-ec2-metadata/branch/master)[![StyleCI](https://camo.githubusercontent.com/b7806dce5f038aa4406fe004d0bf6fea8475d47cdd6e522352eec6473feef47d/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3430343236353930312f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/404265901)[![Latest Stable Version](https://camo.githubusercontent.com/5398224630ec162a9b4fa8a11901645bd77250dce93d803cd3703fb3f85a0e0b/68747470733a2f2f706f7365722e707567782e6f72672f72656e6f6b692d636f2f6c61726176656c2d6563322d6d657461646174612f762f737461626c65)](https://packagist.org/packages/renoki-co/laravel-ec2-metadata)[![Total Downloads](https://camo.githubusercontent.com/54fbdf9807c7a8b72234340b35bd82aeff94e73a7905fc6c115b1541fd82c6af/68747470733a2f2f706f7365722e707567782e6f72672f72656e6f6b692d636f2f6c61726176656c2d6563322d6d657461646174612f646f776e6c6f616473)](https://packagist.org/packages/renoki-co/laravel-ec2-metadata)[![Monthly Downloads](https://camo.githubusercontent.com/fb66f78c401c2b1f7fa4309ef8ebd1b5b178fe0f2506f66e9367bfbd73dff43f/68747470733a2f2f706f7365722e707567782e6f72672f72656e6f6b692d636f2f6c61726176656c2d6563322d6d657461646174612f642f6d6f6e74686c79)](https://packagist.org/packages/renoki-co/laravel-ec2-metadata)[![License](https://camo.githubusercontent.com/ceb5f127699f181b5e21397cea0ad5a97347a188639a2530972681ba28af9e68/68747470733a2f2f706f7365722e707567782e6f72672f72656e6f6b692d636f2f6c61726176656c2d6563322d6d657461646174612f6c6963656e7365)](https://packagist.org/packages/renoki-co/laravel-ec2-metadata)

Retrieve the EC2 Metadata using Laravel's eloquent syntax.

🤝 Supporting
------------

[](#-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](https://github.com/sponsors/rennokki). 📦**

[![](https://camo.githubusercontent.com/60f53f83a4d4298ddc9991be4dfe252a4b32e6f5efbfd727bde6e85eedf70edb/68747470733a2f2f6769746875622d636f6e74656e742e73332e66722d7061722e7363772e636c6f75642f7374617469632f32302e6a7067)](https://github-content.renoki.org/github-repo/20)

🚀 Installation
--------------

[](#-installation)

You can install the package via composer:

```
composer require renoki-co/laravel-ec2-metadata
```

🙌 Usage
-------

[](#-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
---------------

[](#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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html), under `Get the available versions of the instance metadata`:

```
use RenokiCo\Ec2Metadata\Ec2Metadata;

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

Calling Custom Endpoints
------------------------

[](#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
------

[](#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
-----------------

[](#testing-your-code)

The package is using [HTTP Client](https://laravel.com/docs/8.x/http-client), a Laravel feature that leverages Guzzle and you can handle requests and test them by [mocking responses](https://laravel.com/docs/8.x/http-client#testing).

Testing properly your app means you should be fully trained with the [AWS EC2's IMDSv2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html) 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
---------

[](#-testing)

```
vendor/bin/phpunit
```

🤝 Contributing
--------------

[](#-contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

🔒 Security
----------

[](#--security)

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

🎉 Credits
---------

[](#-credits)

- [Alex Renoki](https://github.com/rennokki)
- [All Contributors](../../contributors)

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance50

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 66.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Total

3

Last Release

1612d ago

Major Versions

0.x-dev → 1.0.02022-02-03

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/21983456?v=4)[rennokki](/maintainers/rennokki)[@rennokki](https://github.com/rennokki)

---

Top Contributors

[![rennokki](https://avatars.githubusercontent.com/u/21983456?v=4)](https://github.com/rennokki "rennokki (49 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (20 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (5 commits)")

---

Tags

awsec2instancelaravelmetadataphpphplaravelamazonawsec2metadatainstanceimdsamazonaws

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/renoki-co-laravel-ec2-metadata/health.svg)

```
[![Health](https://phpackages.com/badges/renoki-co-laravel-ec2-metadata/health.svg)](https://phpackages.com/packages/renoki-co-laravel-ec2-metadata)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[spatie/laravel-export

Create a static site bundle from a Laravel app

674146.0k6](/packages/spatie-laravel-export)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.0k](/packages/simplestats-io-laravel-client)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1235.9k20](/packages/fleetbase-core-api)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
