How to run lambda with CLI, orLambda python command that can call the command

Viewed 37

I am trying to create an AWS Lambda function that creates a file and then uploads it into S3.

The AWS CLI commands would be:

aws ec2 describe-network-interfaces --output table > report.csv
aws s3 cp report.csv s3://testec2lambd

Is there anyway that I can use AWS CLI within my Lambda function?

Alternatively, how do I write it in the function as a python script?

1 Answers

Rather than calling the AWS CLI from an AWS Lambda function, you can do all operations within the Lambda function. For example:

import boto3
import pprint

# Describe Network Interfaces
ec2_client = boto3.client('ec2')

response = ec2_client.describe_network_interfaces()

# Save it to a local file
pretty_print_json = pprint.pformat(response).replace("'", '"')

with open('/tmp/output_file', 'w') as f:
    f.write(pretty_print_json)

# Upload file to S3
s3_client = boto3.client('s3')

s3_client.upload_file('/tmp/output_file', 'mybucket', 'mykey')
Related