Я пытаюсь переместить данные с s3 на es с помощью лямбды в aws. Я хочу протестировать свою функцию, однако считаю, что мой файл не имеет правильного формата, что приводит к ошибке.
Моя лямбда-функция выглядит следующим образом:
region = 'ap-northeast-2' # e.g. us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
host = 'my-es-domain' # the Amazon ES domain,
index = 'lambda-s3-index'
type = 'lambda-type'
url = host + '/' + index + '/' + type
headers = { "Content-Type": "application/json" }
s3 = boto3.client('s3')
# Regular expressions used to parse some simple log lines
ip_pattern = re.compile('(\d+\.\d+\.\d+\.\d+)')
time_pattern = re.compile('\[(\d+\/\w\w\w\/\d\d\d\d:\d\d:\d\d:\d\d\s-\d\d\d\d)\]')
message_pattern = re.compile('\"(.+)\"')
# Lambda execution starts here
def handler(event, context):
for record in event['Records']:
# Get the bucket name and key for the new file
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
# Get, read, and split the file into lines
obj = s3.get_object(Bucket=bucket, Key=key)
body = obj['Body'].read()
lines = body.splitlines()
# Match the regular expressions to each line and index the JSON
for line in lines:
ip = ip_pattern.search(line).group(1)
timestamp = time_pattern.search(line).group(1)
message = message_pattern.search(line).group(1)
document = { "ip": ip, "timestamp": timestamp, "message": message }
r = requests.post(url, auth=awsauth, json=document, headers=headers)
У меня естьправильно настроил тестовые события для файла в моем ведре. Когда я проверяю, он выдает:
Response:
{
"errorMessage": "cannot use a string pattern on a bytes-like object",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/task/lambdaPackage.py\", line 40, in handler\n ip = ip_pattern.search(line).group(1)\n"
]
}
Что, по моему мнению, является проблемой с моим файлом. мой файл находится в файле sample.log и выглядит следующим образом:
12.345.678.90 - [10/Oct/2000:13:55:36 -0700] "PUT /some-file.jpg"
12.345.678.91 - [10/Oct/2000:14:56:14 -0700] "GET /some-file.jpg"
Как я могу его отформатировать, например: использовать .py, .json и т. д. ... это мой первый раз с aws и log Infos. Заранее спасибо!
Я пытался создать другой файл в таком формате:
12.345.678.90 - [10/Oct/2000:13:55:36 -0700] "PUT /some-file.jpg"\n
12.345.678.91 - [10/Oct/2000:14:56:14 -0700] "GET /some-file.jpg"
, который выдает ту же ошибку.