0

I am not able to see the downloaded file from s3 bucket in /tmp/ folder of lambda.I have provided full access over S3 and Lambda in IAM policy. When I'm testing this code, my tmp folder is always empty(refer screenshot). enter image description here

Can some-one help me out??

My code :

import csv
import boto3


s3_client = boto3.client('s3')
s3_resource = boto3.resource('s3')

def lambda_handler(event, context):
    bucket = 'test-bucket'
    key = 'data/original_file1.csv'
    try:
        temp_path= '/tmp/test_file.csv'
        s3_resource.Bucket(bucket).download_file(key,temp_path)
        
        response = s3_client.get_object(Bucket=bucket, Key=key)
        return response['ContentType']
        
        
    except Exception as e:
        print(e)
Maurice
  • 11,482
  • 2
  • 25
  • 45
ameesha gupta
  • 101
  • 11
  • 1
    What are you looking at is the tmp folder of your source code. You instead are saving the file in the tmp folder of the Lambda Container that runs your function. – kgiannakakis Nov 08 '21 at 13:44
  • @kgiannakakis Thank you for your reply. Now i can understand the difference. Is there a way to check the contents of the **tmp** folder of the Lambda Container that runs my function. Can you advise a way to do so ?? – ameesha gupta Nov 09 '21 at 04:27
  • See https://stackoverflow.com/questions/39477729/aws-lambda-read-contents-of-file-in-zip-uploaded-as-source-code – kgiannakakis Nov 09 '21 at 07:14

1 Answers1

2

There is a misunderstanding here. You seem to assume that the directory structure you see in the Editor in the Management Console is the same that's being used in the Lambda function. This is not the case.

The online editor you're using essentially let's you edit the content of the .zip file that contains the function code for your Lambda.

Whenever the Lambda function is executed and a new execution context ("instance") of the function is required, the Lambda service will start a new micro-VM, download the code assets and extract them to the micro-VM. You then get to access the /tmp/ directory in that execution context.

This is completely disconnected from the editor, so you can't see the changes being made in the /tmp/ directory within the execution context.

Maurice
  • 11,482
  • 2
  • 25
  • 45