Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)
I have JSON data with respect to my requirements, how can I use AWS lambda to create csv file from JSON?

1 Answer

0 votes
by (12.4k points)
edited by

I'm providing the lambda code for saving CSV int S3 change it as per your specification.

import boto3

import csv

import io

s3 = boto3.client('s3')

ec2 = boto3.client('ec2')

def lambda_handler(event, context):

    csvio = io.StringIO()

    writer = csv.writer(csvio)

    writer.writerow(['id', 'name'])

    paginator = ec2.get_paginator('describe_security_groups').paginate()

    for page in paginator:

        for item in page['SecurityGroups']:

            identity = item['GroupId']

            name = item['GroupName']

            writer.writerow([identity, name])

    s3.put_object(Body=csvio.getvalue(), ContentType='text/csv', Bucket='<bucket>', Key='<filename.csv>') 

    csvio.close()

Want to learn more about AWS, Come & join: AWS Certification

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
0 answers
asked Apr 14, 2021 in AWS by xtdeka01 (120 points)
0 votes
1 answer

Browse Categories

...