1
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?

More than 1 year has passed since last update.

LambdaでGlueジョブの実行履歴をひたすら表示する

Last updated at Posted at 2022-01-01

背景・目的

  • GlueAPIのget_job_runsは、デフォルト100件(最大200件)しか返さないので、それ以上の結果を知りたい場合は、何度も実行する必要があったのでプログラム化しました。

結論

  • NextTokenを使う。

内容

APIリクエスト

  • NextTokenがある場合と、それ以外で分ける必要がある。

初回もしくは、NextTokenがない場合

response = glue_client.get_job_runs(JobName=job_name)

NextTokenがある場合

glue_client.get_job_runs(JobName=job_name, NextToken=next_token)

コード全体

import json
import boto3
import time
import os
import logging
import subprocess

logger = logging.getLogger()
logger.setLevel(logging.INFO)

glue_client = boto3.client('glue')

job_name =""

def init(event):
    logger.info('## ENVIRONMENT VARIABLES')
    logger.info(os.environ)
    logger.info('## EVENT')
    logger.info(event)
    
    global job_name
    job_name = event['job_name']
    
def lambda_handler(event, context):
    init(event)
    response = glue_client.get_job_runs(JobName=job_name)
    response_cnt = 0

    while (True):
    
        for line in response['JobRuns']:
            logger.info(line)
            response_cnt = response_cnt + 1

        logger.info(response)
        
        if response.get('NextToken'):
            next_token = response['NextToken']
            logger.info(next_token)
        else:
            break

        response = glue_client.get_job_runs(JobName=job_name, NextToken=next_token)

    print(response_cnt)

    return {
        'statusCode': 200,

    }

考察

  • 他のAWSのAPIでも同様に、NextTokenの考えはあるので流用可能。

参考

1
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
1
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?