0

::: Python newbie :::

I'm doing a lambda that generates a report of the resources on an account. The report is a csv file that gets uploaded into an S3 bucket (full code at the bottom)

PROBLEM When I look for a value that doesn't exist, I get a key error. for example, on this part

table10 = []
rows10 = []
cloudfront_title = 'CloudFront description chart'
client10 = boto3.client('cloudfront')
cfs = client10.list_distributions()
for cf in cfs['DistributionList']['Items']:
 rows10 = [cf['Id'], cf['DomainName']]
 table10.append(rows10)

Some accounts have cloud front, but others don't . When they don't I get a key error

{
  "errorMessage": "'Items'",
  "errorType": "KeyError",
  "stackTrace": [
    "  File \"/var/task/index.py\", line 133, in lambda_handler\n    for cf in cfs['DistributionList']['Items']:\n"
  ]
}

QUESTION Is there a way to validate if the value exist, and if exists bring it, otherwise bring None/Null or something like it

-*-

NEXT PROBLEM I repeat a lot of code, like a looot. I create a table and a row for each resource and then when I do the csv file I write each chart, for example:

csvfile.write(vpc_title)
csvfile.write('\n')
writer.writerows([headers])
writer.writerows(table)
csvfile.write('\n')

csvfile.write(dns_title)
csvfile.write('\n')
writer.writerows([headers1])
writer.writerows(table1)
csvfile.write('\n')

QUESTION is there a way I can avoid repeating the same code 17 times? maybe one of those magical Python loops

Laura Jorge
  • 21
  • 1
  • 5
  • 1
    Question 1: https://stackoverflow.com/questions/46024562/how-do-i-avoid-keyerror-when-working-with-dictionaries Question 2: look at two blocks of code that repeat, create a method from one of them and extract anything that is different compared to the other, put it as a parameter. – luk2302 Jul 22 '21 at 16:40
  • 1
    And don't repost questions: https://stackoverflow.com/q/68407847/2442804 – luk2302 Jul 22 '21 at 16:41
  • @luk2302 https://stackoverflow.com/questions/68407847/aws-account-report-with-boto3 is closed, I edited it, but remains close – Laura Jorge Jul 22 '21 at 16:46
  • Are you asking how to test if a key exists in a dictionary? – jarmod Jul 22 '21 at 18:28
  • If you ever need to catch an Exception (eg when CloudFront doesn't exist), you can use Python Try Except. Or use `if 'Items' in cfs['DistributionList']` before attempting to loop through the contents. If you are going to ask a question, please try to focus on a specific issue rather than saying "here is my extensive code" and expecting somebody to figure out what's happening. – John Rotenstein Jul 22 '21 at 22:49
  • @JohnRotenstein that actually worked!! Thanks!! I did: `if 'Items' in cfs['DistributionList']: for cf in cfs['DistributionList']['Items']: rows10 = [cf['Id'], cf['DomainName']] table10.append(rows10) headers10 = ['Id', 'DomainName'] ` I got that I was ambiguos before and I tried to correct it by explicit saying I have this problem on this part of the code, have this error,here my question. I added all the code at the end just for context I'm learning and I will be more assertive and direct next time. You helped me a lot, I was tryin to get the if within the loop, thanks again – Laura Jorge Jul 23 '21 at 09:26
  • @luk2302 still working on the recommended method – Laura Jorge Jul 23 '21 at 09:28

0 Answers0