Py Script running in AWS lambda [ERROR] NameError: name 'filecontent' is not defined Traceback (most recent call last):
Py Script running in AWS lambda [ERROR] NameError: name 'filecontent' is not defined Traceback (most recent call last):
我有一个 Python 脚本 运行 作为 lambda 函数,用于将 S3 中的数据发送到 Splunk。它可以读取我的数据,但无法将数据发送到 Splunk。任何人都可以对附加的脚本进行任何更改吗?
from base64 import b64decode
import io
import os
import urllib
import boto3
from botocore.vendored import requests
import sys
# Define Global Variables
splunk_host = os.environ['splunk_host']
splunk_index = os.environ['splunk_index']
region = os.environ['region']
print(splunk_host,splunk_index)
# Disable SSL Warnings
#urllib3.disable_warnings()
def get_object(bucket, object):
# Setup connection with S3
session = boto3.Session()
s3 = session.client('s3')
# Download file
obj = s3.get_object(Bucket=bucket, Key=object)
return obj
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
session = boto3.Session()
s3 = session.client('s3')
# Download file
obj = s3.get_object(Bucket=bucket, Key=key)
file_content = obj["Body"].read().decode('utf-8')
print("Returned Object: {}".format(obj))
print(file_content)
for value in filecontent.split('\n'):
data_json = str(file_content)
payload = {}
payload.update({"index": splunk_index})
payload.update({"source": "waf"})
payload.update({"event": data_json})
#Send data to splunk
send_to_splunk(splunk_host, get_secrets('tropos-splunk')['splunk-hec-token-dev'], payload)
print(raja)
def get_object(bucket, object):
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket, Key=object)
return obj
#Configure SPLUNK Connection
def send_to_splunk(host, token, logdata):
url = 'https://' + host + ':8088/services/collector'
auth_header = {'Authorization': 'Splunk ' + token}
r = requests.post(url, headers=auth_header, json=logdata, verify=False)
print(r)
return r
def get_secrets(secret_id):
client = boto3.client(service_name='secretsmanager',region_name=region)
get_secret_value_response = client.get_secret_value(SecretId=secret_id)
return eval(get_secret_value_response['SecretString'])
代码进入 printf(file_content)
,我收到以下错误。请建议对脚本进行任何更改。
[ERROR] NameError: name 'filecontent' is not defined
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 43, in lambda_handler
for value in filecontent.split('\n'):
变量名是file_content
而不是filecontent
。
我有一个 Python 脚本 运行 作为 lambda 函数,用于将 S3 中的数据发送到 Splunk。它可以读取我的数据,但无法将数据发送到 Splunk。任何人都可以对附加的脚本进行任何更改吗?
from base64 import b64decode
import io
import os
import urllib
import boto3
from botocore.vendored import requests
import sys
# Define Global Variables
splunk_host = os.environ['splunk_host']
splunk_index = os.environ['splunk_index']
region = os.environ['region']
print(splunk_host,splunk_index)
# Disable SSL Warnings
#urllib3.disable_warnings()
def get_object(bucket, object):
# Setup connection with S3
session = boto3.Session()
s3 = session.client('s3')
# Download file
obj = s3.get_object(Bucket=bucket, Key=object)
return obj
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
session = boto3.Session()
s3 = session.client('s3')
# Download file
obj = s3.get_object(Bucket=bucket, Key=key)
file_content = obj["Body"].read().decode('utf-8')
print("Returned Object: {}".format(obj))
print(file_content)
for value in filecontent.split('\n'):
data_json = str(file_content)
payload = {}
payload.update({"index": splunk_index})
payload.update({"source": "waf"})
payload.update({"event": data_json})
#Send data to splunk
send_to_splunk(splunk_host, get_secrets('tropos-splunk')['splunk-hec-token-dev'], payload)
print(raja)
def get_object(bucket, object):
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket, Key=object)
return obj
#Configure SPLUNK Connection
def send_to_splunk(host, token, logdata):
url = 'https://' + host + ':8088/services/collector'
auth_header = {'Authorization': 'Splunk ' + token}
r = requests.post(url, headers=auth_header, json=logdata, verify=False)
print(r)
return r
def get_secrets(secret_id):
client = boto3.client(service_name='secretsmanager',region_name=region)
get_secret_value_response = client.get_secret_value(SecretId=secret_id)
return eval(get_secret_value_response['SecretString'])
代码进入 printf(file_content)
,我收到以下错误。请建议对脚本进行任何更改。
[ERROR] NameError: name 'filecontent' is not defined
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 43, in lambda_handler
for value in filecontent.split('\n'):
变量名是file_content
而不是filecontent
。