renoki-co/laravel-ec2-metadata
Composer 安装命令:
composer require renoki-co/laravel-ec2-metadata
包简介
Retrieve the EC2 Metadata using Laravel's eloquent syntax.
README 文档
README
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
renoki-co/laravel-ec2-metadata 适用场景与选型建议
renoki-co/laravel-ec2-metadata 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「metadata」 「amazon」 「aws」 「ec2」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 renoki-co/laravel-ec2-metadata 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 renoki-co/laravel-ec2-metadata 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 renoki-co/laravel-ec2-metadata 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Yii2 Component to manage SEO data and metadata
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
Extremely simple meta tag generator.
A simple Laravel 5/6/7/8 service provider for including the AWS SDK for PHP.
NinjaImg PHP SDK.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-09-11