繁体   English   中英

在 AWS lambda [ERROR] NameError: name 'filecontent' is not defined Traceback(最近一次调用最后)中运行的 Py 脚本:

[英]Py Script running in AWS lambda [ERROR] NameError: name 'filecontent' is not defined Traceback (most recent call last):

我有一个 Python 脚本作为 lambda function 运行以将 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM