0

So, the function I have written involves saving a string to a text file at the end of each execution, and then calling that string in the next. It works fine locally, but not on Lambda. From what I can find, it looks like Lambda only supports writing to files in a /tmp folder, but it would only last for one execution, which doesn't work for me. How do I go about this?

Here is the relevant code, it's part of a tweepy bot.

def reply():
    with open('lastid.txt', 'r+') as mf:
        last = mf.readline()
        response = client.get_users_mentions(user_id, since_id=last)
        for tweet in response.data:
            text = tweet.text
            id = tweet.id
            if any(x in text.lower() for x in strings):
                client.create_tweet(in_reply_to_tweet_id=id, text=greet)
            else:
                client.create_tweet(in_reply_to_tweet_id=id, text=reply)
            mf.write(str(tweet.id))

Thank you very much for any help! Maybe the answer is obvious but honestly my brain is a little fried at the moment.

2
  • 1
    It's entirelly possible before the next execution your lambda instance terminates and a new one is spawned (or the next execution just runs on a new one without the old one terminating). You have very little control over this. Commented Sep 10, 2022 at 5:05
  • Also, for what it's worth, that code will only ever store the first line from lastid.txt in the last variable. Commented Sep 10, 2022 at 21:08

2 Answers 2

1

You can write to an s3 file and use it in the next execution. Before that, you need to give access to your lambda function to access the s3 location.

Sign up to request clarification or add additional context in comments.

Comments

0

Files written to /tmp aren't necessarily removed between executions. They can be retained across multiple Lambda executions if container reuse happens.

However, there is no guarantee that this will happen. And if you want to retain the file across new Lambda function version deployments then it isn't possible at all using /tmp.

Your options are:

Given your use case I would suggest the EFS solution.

Comments

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.