AWS S3 Glacier Retrieval Lambda


AWS S3 Glacier Retrieval Lambda


import json
import boto3
import os
import base64
import gzip

client = boto3.client('s3')

def lambda_handler(event, context):
    print(event)
    try:
        logs = event['awslogs']['data']
        decoded_logs = base64.b64decode(logs)
        uncompressed_logs = gzip.decompress(decoded_logs)
        logs_data = uncompressed_logs.decode('utf-8')
        print(logs_data)
        logs_json = json.loads(logs_data)
        print(logs_json)
        log_event = logs_json['logEvents'][0]['message']
        print(log_event)
        logs_json1 = json.loads(log_event)
        print(logs_json1)
        bucketName = logs_json1['requestParameters']['bucketName']
        print(bucketName)
        key = logs_json1['requestParameters']['key']
        print(key)
        daysCount=os.environ['DAYS_COUNT']
        daysCount = int(daysCount)
        
        response = client.restore_object(
            Bucket=bucketName,
            Key=key,
            RestoreRequest={
                'Days': daysCount,
                'GlacierJobParameters': {
                    'Tier': 'Standard'
                }
            }
        )
        print(response)
    except Exception as ex:
        print(ex)
        
    return {
        'respon': 'Success'
    }