【问题标题】:"errorMessage": "name ' ' is not defined" [closed]“errorMessage”:“名称''未定义”[关闭]
【发布时间】:2019-12-18 14:24:59
【问题描述】:

我想使用 Python 在 Lambda 中创建一个保存在 CSV 文件中的报告。所以你会找到函数的代码:

import boto3
import re
import csv

def lambda_handler(event,context):
    client = boto3.client('ce')
    response = client.get_cost_and_usage(
        TimePeriod={
            'Start': "2019-02-01",
            'End':  "2019-08-01"
        },
        Granularity='MONTHLY',
        Metrics=['BlendedCost'],
        GroupBy=[
            {
                'Type': 'TAG',
                'Key': 'Project'
            },
        ]
    )

    temp_csv_file = csv.writer(open("/tmp/csv_file.csv", "w+"))
    # writing the column names
    temp_csv_file.writerow(["Account Name", "Month", "Cost"])

    # writing rows in to the CSV file
    for detail in participant_details:
        temp_csv_file.writerow([response['account_name'],
                                response['month'],
                                response['cost']
                                ])

    client = boto3.client('s3')
    client.upload_file('/tmp/csv_file.csv', BUCKET_NAME,'final_report.csv')

如何解决以下错误?

"errorMessage": "name 'participant_details' is not defined",

【问题讨论】:

  • for detail in participant_details: 什么是参与者详细信息?你在哪里定义它?
  • @sertsedat 所以应该是响应而不是参与者详细信息?

标签: python python-3.x amazon-web-services amazon-ec2 aws-lambda


【解决方案1】:

在您的程序中,您尚未定义变量participant_details,因此无法查找其值。您应该先定义该变量,然后再访问它。

【讨论】:

  • 所以应该是响应而不是参与者详细信息?
  • 这取决于client.get_cost_and_usage 的作用。您能否编辑问题以显示您在拨打电话后希望 response 包含的内容的示例?
猜你喜欢
  • 2021-08-23
  • 2016-12-20
  • 2023-03-05
  • 2020-12-23
  • 2014-03-18
  • 2021-03-17
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多