import boto3
import re,json,base64,gzip
import requests
from requests_aws4auth import AWS4Auth

region = 'cn-northwest-1'
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key,credentials.secret_key,region,service,session_token=credentials.token)

host = 'https://search-es-test-rssutkotldfmnca64hpfbi5jya.cn-northwest-1.es.amazonaws.com.cn'
index = 'lambda-cw'
type_a = 'lambda-type'
url = host + '/' + index + '/' + type_a

headers = {"Content-Type":"application/json"}


def lambda_handler(event, context):

    de_content = base64.b64decode(event['awslogs']['data'])
    ret = json.loads(gzip.decompress(de_content).decode('utf8'))

    # log_msg = ret['logEvents'][0]['message']
    # tup_log = log_msg.split(' ', -1)
    # tup_log = json.loads(tup_log)
    # print(type(tup_log))

    r = requests.post(url,auth=awsauth,json=ret,headers=headers)
    print(r)
    
