4

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

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. Your code is working fine. It seems I am not able to download file using - os.system("wget jsonplaceholder.typicode.com/posts/1 -P /tmp") do I need to create deployment package to use bash command in os.system("bash command")?
@Navjot I have never checked whether wget works in lambda. You can make use of urllib module otherwise. urllib.urlretrieve("https://jsonplaceholder.typicode.com/posts/1", "file.json")
Yeah It works with urllib. Thanks for your suggestion. I think I will have to deploy wget and awscli as part of deployment package.
@liferacer : how did you add wget to deployment package ?
@pankajanand18 He didn't, just used urllib instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.