简体   繁体   English

如何在PHP中获取AWS EC2实例ID

[英]How to get AWS EC2 Instance id in PHP

I have 2 Instance in AWS EC2 and I want check request by Instance id. 我在AWS EC2中有2个实例,我想按实例ID检查请求。

require_once 'vendor/autoload.php';

use Aws\Ec2\Ec2Client;
use Aws\Rds\RdsClient;
$instance_id = $ec2->describeInstances();

if($instance_id == 'i0-jkedsf54325123' || $instance_id == 'i0-jkedsf543251321'){
    echo "request instance id is allow";
}else{ 
    echo "request not allow";
}

Need to check which instance request is coming. 需要检查哪个实例请求即将到来。

If you wish to find the Instance ID on which some code is running, you can access the Instance Metadata . 如果您希望找到运行某些代码的实例ID,则可以访问实例元数据

To do it from PHP, use (from Get Amazon AWS Instance ID (PHP / wget) · GitHub ): 要通过PHP进行操作,请使用(从Get Amazon AWS Instance ID(PHP / wget)·GitHub ):

<?php
//---------------------------------------------------------------------------------
// You can get the metadata for an AWS instance by loading the following URL
// Note: This URL must be loaded from an AWS instance
//
//---------------------------------------------------------------------------------
// URL:
//      http://169.254.169.254/latest/meta-data/instance-id
//
//---------------------------------------------------------------------------------
// wget:
//      wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
//
//---------------------------------------------------------------------------------

echo @file_get_contents("http://instance-data/latest/meta-data/instance-id");

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM