0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【実務で使えるLambda】describe_instancesで色々やってみる

Last updated at Posted at 2024-02-04

はじめに

実際に実務で扱ったLambdaの汎用的なコードを備忘録として残しています。
(※機密情報は載せてないのでご安心ください)

インスタンスIDをリストで取得してみる

とりあえずこれをコピペすればOK

import boto3

client = boto3.client('ec2', region_name='ap-northeast-1') # リージョンは任意

instances = client.describe_instances()

instance_ids = [] # 空のリストを用意

for reservation in instances['Reservations']:
     for instance in reservation['Instances']:
         instance_id = instance['InstanceId']
         instance_ids.append(instance_id)

インスタンスに付いているタグ名をリストで取得してみる

さっき取得したinstance_idsを使う

tag_names = []

for instance_id in instance_ids:
    tags_info = client.describe_instances(
            Filters=[
                {
                    'Name': 'tag-key',
                    'Values':['Name']
                },
                {
                    'Name': 'instance-id',
                    'Values':[instance_id]
                }
            ]
        )['Reservations'][0]['Instances'][0]['Tags']
        for res in tags_info :
            if res['Key'] == "Name":
                tag_name = res['Value']
                tag_names.append(tag_name)

リスト取得したインスタンスIDに対して「停止」「起動」をする

instance_idsを使う

# 停止
ec2.stop_instances(InstanceIds=instance_ids)

# 起動
ec2.start_instances(InstanceIds=instance_ids)

2024.2.1から特定のサービスに割り当てられているかどうかに関わらず、すべてのパブリックIPv4 アドレスの利用に対して1IPアドレスあたり 0.005 USD/時間が課金になったため、コストの面で注意が必要になった。

(CloudWatch)describe_alarmsを組み合わせて、設定されているCloudWatchアラーム一覧を取得してみる

instance_idsを使う

for instance_id in instance_ids:
    tags_info = ec2.describe_instances(
        Filters = [
            {
                'Name' : 'tag-key',
                'Values' : ['Name']
            },
            {
                'Name' : 'instance-id',
                'Values' : [instance_id]
            }
        ]
    )['Reservations'][0]['Instances'][0]['Tags']
    for res in tags_info:
        if res['Key'] == "Name":
            tag_name = res['Value']

    alarm_list = cloudWatch.describe_alarms(
        AlarmNamePrefix = tag_name
        )['MetricAlarms']

CloudWatchアラーム設定されている特定期間のメトリクスを取得してみる

import datetime
from dateutil.relativedelta import relativedelta

alarm_values = []

for instance_id in instance_ids:
    tags_info = ec2_client.describe_instances(
        Filters = [
            {
                'Name' : 'tag-key',
                'Values' : ['Name']
            },
            {
                'Name' : 'instance-id',
                'Values' : [instance_id]
            }
        ]
    )['Reservations'][0]['Instances'][0]['Tags']
    for res in tags_info:
        if res['Key'] == "Name":
            tag_name = res['Value']

    alarm_list = cw_client.describe_alarms(
        AlarmNamePrefix = tag_name
        )['MetricAlarms']

    for alarmList in alarm_list:
        values_list = []
        alarm_value = cw_client.get_metric_statistics(
            Namespace = alarmList['Namespace'],
            MetricName = alarmList['MetricName'],
            Dimensions = alarmList['Dimensions'],
            StartTime = datetime.datetime.now() - relativedelta(days=1) + datetime.timedelta(hours=9),
            EndTime = datetime.datetime.now() + datetime.timedelta(hours=9),
            Period = 1800,
            Statistics = [
                'Maximum',
            ]
        )

        if len(alarm_value) != 0:
            for value in alarm_value['Datapoints']:
                values_list.append(value['Maximum'])
                # print(value['Maximum'])

        alarm_values.append(values_list)

print(alarm_values)
取得データ
[[0.344827586207367,
  0.491803278688077,
  0.508474576270724,
  0.500000000000303,
  0.33898305084792,
  0.500000000000303,
  0.499999999999545,
  0.344827586207367,
  0.508474576270724,
  0.508474576271495,
  0.344827586206583,
  0.33898305084792,
  0.508474576271495,
  0.344827586206583,
  0.344827586206583,
  0.508474576270724,
  0.33898305084792,
  0.508474576270724,
  0.500000000000303,
  0.508474576271495,
  0.499999999999545,
  1.83333333333394,
  0.500000000000303,
  0.344827586206583,
  0.344827586206583,
  0.508474576271495,
  0.500000000000303,
  0.344827586206583,
  0.66666666666606,
  0.508474576270724]]

メトリクス取得の関数にget_metric_dataもあるが、無料利用枠の範囲外となるため、get_metric_statisticsを使用

おわりに

定期的に更新していこうと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?