ChumiestBucket
ChumiestBucket

Reputation: 1067

access to /tmp or other temporary file structure in Serverless Lambda Function

AWS Lambda Function Using Console

In AWS Lambda, my Function would save a file from S3 to the /tmp directory like so:

local_filepath = '/tmp/file.txt'

s3.download_file(
  Bucket=bucket,
  Key=key,
  Filename=local_filepath
)

and life was good.

AWS Lambda Function Using Serverless

Using Serverless however is a different story.

The same setup results in the following error:

[Errno 2] No such file or directory: '/tmp/processed.txt.7E4850BD'

So I would guess there is no /tmp dir in the Serverless execution env.

I've tried to just save the file to the current directory, with local_filepath = 'file.txt', but I get a OSError(30, 'Read-only file system') error.

Upvotes: 1

Views: 6026

Answers (1)

nosklo
nosklo

Reputation: 223052

Try using the tempfile module, it has a collection of techs to retrieve a temporary directory to use:

local_filepath = os.path.join(tempfile.gettempdir(), 'file.txt')

Upvotes: 8

Related Questions