Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute bash commands in aws lambda

I am trying to schedule a job in AWS lambda where i get data fromm a Json API. I want to transfer JSON file to amazon S3 everytime.I have set up S3 bucket and aws lambda function with proper IAM roles. I am writing AWS lambda function in Python. Code works fine on an EC2 instance but It's not transferring file to S3 if I put it in AWS Lambda.

import os


def lambda_handler(event, context):
    #changing the directory to /tmp
    os.chdir("/tmp")
    print "loading function"
    #downloading file to 
    os.system("wget https://jsonplaceholder.typicode.com/posts/1 -P /tmp")
    #using aws-cli to transfer file to amazon S3
    os.system("aws s3 sync . s3://targetbucket")

I am new to aws lambda. I am not getting any error but it's not giving me expected output

like image 788
liferacer Avatar asked Sep 17 '25 11:09

liferacer


1 Answers

AWS Lambda does not have the aws cli by default.

You can either Create a deployment package with awscli in it or Use python boto3 library.

import boto3

s3client = boto3.client('s3')
for filename in os.listdir('/tmp'): # assuming there will not be any sub-directories
    fpath = os.path.join('/tmp',filename)
    if os.path.isfile(fpath): 
        s3client.upload_file(fpath, 'targetbucket', filename)
like image 170
franklinsijo Avatar answered Sep 20 '25 01:09

franklinsijo