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内のCSVファイルをPythonで読み込む

Last updated at Posted at 2024-04-02
import boto3
import datetime
import time
import csv
import logging

def lambda_handler(event, context):
    # 実行日取得
    to_time = datetime.datetime.now()
    # 1日前取得
    from_time = datetime.datetime.now() - datetime.timedelta(days=1)
    # エポック時刻取得(float型)
    epoc_from_time = from_time.timestamp()
    epoc_to_time = to_time.timestamp()
    # エポック時刻をミリ秒にしint型にキャスト
    m_epoc_from_time = int(epoc_from_time * 1000)
    m_epoc_to_time = int(epoc_to_time * 1000)

    file_path = 'xample8.csv'  # Lambdaのデフォルトの作業ディレクトリ
    try:
        with open(file_path, mode='r') as file:
            rows = csv.reader(file)
            client = boto3.client('logs')

            for row in rows:
                logging.basicConfig(level=logging.DEBUG)
                log_group_name = row[0]
                s3_bucket = row[1]

                client.create_export_task(
                    logGroupName=log_group_name,
                    fromTime=m_epoc_from_time,
                    to=m_epoc_to_time,
                    destination=s3_bucket,
                    destinationPrefix=log_group_name
                )

                export_tasks = client.describe_export_tasks(statusCode='RUNNING')
                while len(export_tasks['exportTasks']) >= 1:
                    export_tasks = client.describe_export_tasks(statusCode='RUNNING')
                    time.sleep(5)
    except FileNotFoundError:
        logging.error("CSVファイルが見つかりません: {}".format(file_path))
        # ここで処理を終了するか、適宜他の処理を加えてください
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?