c0ntax/aws-ec2-check-tag

一个非常简单的服务,用于获取指定实例的标签,以查看特定的键值是否相等。

1.0.4 2023-10-27 15:29 UTC

This package is auto-updated.

Last update: 2024-09-27 17:24:29 UTC


README

一个简单的库,用于检查在当前运行的EC2实例上,某个键是否等于特定的值。

简介

这个库是为了解决一个特定问题而构建的,我只想让cron命令和其他服务在一组EC2实例之一上运行。这样,你可以部署一个AMI来为一组负载均衡的机器提供服务,并简单地标记其中一个你希望执行某些操作。例如,假设我们有3个运行代码实例,但我们只想让特定的代码在其中一个上运行。只需为该实例设置一个标签,例如

RunHere: True

然后你需要检查 RunHere 是否被设置为 True

用法

使用上述示例,你可以使用以下代码

use C0ntax\Aws\Ec2\CheckTag\Exceptions\KeyNotFoundException;
use C0ntax\Aws\Ec2\CheckTag\Exceptions\NotOnEc2InstanceException;

public function doSomething()
{
  try {
    if (!$this->getCheckService()->check('RunHere', 'True')) {
      return;
    }
  } catch (NotOnEc2InstanceException $exception) {
    // This is probably running on a local instance so we might want to allow this to run
    // (i.e. we are just going to catch and ignore the exception)
  } catch (KeyNotFoundException $exception) {
    // The 'RunHere' key was not found. It's up to you how you've implimented it. It's probably not an error in most cases
    // It just means that you don't want to run anything here
    
    return;
  }
  
  print 'I am doing something';
}