-1
Amazonのラムダ関数としてPythonスクリプトを書いています。スクリプトはAmazonのクラウドウォッチにRabbitMQメトリクスを公開しています。私は数回試してみましたが、rabbitmqの深みを得ることができましたが、ラムダ関数はクラウドウォッチにメトリクスを公開できませんでした。Amazon Lambda関数としてPythonスクリプトを書くには?
from __future__ import with_statement, print_function
from pyrabbit.api import Client
import boto3
import os
host = ""
username = ""
password = ""
vhost = ""
namespace = ""
def get_queue_depths(host, username, password, vhost):
cl = Client(host, username, password)
if not cl.is_alive():
raise Exception("Failed to connect to rabbitmq")
depths = {}
queues = [q['name'] for q in cl.get_queues(vhost=vhost)]
for queue in queues:
if queue == "aliveness-test":
continue
if 'celery' in queue:
continue
depths[queue] = cl.get_queue_depth(vhost, queue)
return depths
def publish_queue_depth_to_cloudwatch(cwc, queue_name, depth, namespace):
float(depth)
cwc = boto3.client('cloudwatch',region_name="us-east-1")
response = client.put_metric_data(
Namespace=namespace,
MetricData=[ { 'MetricName': queue_name, 'Value': depth, 'Unit': 'Count' } ]
)
print("Putting metric namespace=%s name=%s unit=Count value=%f" %
(namespace, queue_name, depth))
def publish_depths_to_cloudwatch(depths, namespace):
for queue in depths:
publish_queue_depth_to_cloudwatch(cwc, queue, depths[queue], namespace)
def get_queue_depths_and_publish_to_cloudwatch(host, username, password, vhost, namespace):
depths = get_queue_depths(host, username, password, vhost)
publish_depths_to_cloudwatch(depths, namespace)
if __name__ == "__main__":
while True:
get_queue_depths_and_publish_to_cloudwatch(host, username, password, vhost, namespace)
具体的な質問は、あなたのラムダ関数からCloudWatchに公開することですか?ラムダ関数のCloudWatchログに、CloudWatchにメトリックを公開できなかったときのエラーメッセージは何でしたか? Lambda機能はVPCの内部または外部で実行されていますか? CloudWatchにメトリックを公開する機能に適切なIAMの役割を割り当てましたか? –
うまく私のラムダ関数はrabbitmqの深さを管理していますが、ログにそれらを見ることができますが、クラウドウォッチへのパブリッシュに失敗するとタイムアウトが発生して終了すると、関数はVPCで実行されています。機能 –
VPCで動作している場合は、ラムダ機能がCloudWatchなどのAWSリソースを含むVPC外のものにアクセスできるように、NATゲートウェイをVPCに追加する必要があります。 –