Reputation: 309
My Lambda function invokes CloudWatch describe-alarms API. Then those alarms are removed. I'm using CloudWatch Cron Event as a trigger.
The impression I have is that responses with alarms are cached, that is even if they are deleted are still being appeared.
Is there any caching system within AWS Lambda?
Upvotes: 7
Views: 12623
Reputation: 3274
Thanks to Noels answer, I was facing similar issue where API GW was using lambda(python runtime) functions(with api gw cache disabled).The problem was that i defined the db connection outside the lambda handler. The result of the code below was old data(even after database table updates) from api.
db = pymysql.connect(host=DB_HOST,user=DB_USER, password=DB_PSWD,database=DB_NAME,cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()
def lambda_handler(event, context):
try:
cursor.execute("SELECT id, product FROM repository")
return { "body":json.dumps(cursor.fetchall()), "statusCode":200}
except Exception as e:
return { "body":json.dumps(event), "statusCode":500}
To fix this i moved the db connection inside lambda handler:
def lambda_handler(event, context):
db = pymysql.connect(host=DB_HOST,user=DB_USER, password=DB_PSWD,database=DB_NAME,cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()
try:
cursor.execute("SELECT id, product FROM repository")
return { "body":json.dumps(cursor.fetchall()), "statusCode":200}
except Exception as e:
return { "body":json.dumps(event), "statusCode":500}
Upvotes: 0
Reputation: 7492
Complementing: If you are reaching your AWS Lambda through the API Gateway, then you can activate cache on the API Gateway level, which is great for speed and reducing costs with Lambda. That caching system allows you to use the params, request headers, etc, as keys for the calls, making it simple and efficient.
Upvotes: 0
Reputation: 16037
It's your code that's caching the response. Not Lambda.
To fix it, you have to fix your code by making sure that you invoke the API inside your handler and return it without storing it outside your handler function's scope.
For illustration purposes,
Don't
const response = callAnApi()
async function handler(event, context, callback) {
// No matter how many times you call the handler,
// response will be the same
return callback(null, response)
}
Do
async function handler(event, context, callback) {
// API is called each time you call the handler.
const response = await callAnApi()
return callback(null, response)
}
Reference: AWS Lambda Execution Model
Any declarations in your Lambda function code (outside the handler code, see Programming Model) remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations. We suggest adding logic in your code to check if a connection exists before creating one.
Upvotes: 24
Reputation: 1043
There is no caching mechanism in AWS Lambda to my knowledge,
That said, after a (successful) request the container Lambda created is "frozen" to preventing it from doing "async" or "background" work. A subsequent request will reuse the container and pass the new event to your function handler. This container will remain in the cluster, ready to be reused and serve requests so long that it isn’t idle for too long, after which it may be discarded entirely. These details are unspecified by AWS.
Because the container sits around waiting for subsequent requests and the memory allocated to it does not magically disappear each time, we can store data for future requests. (But would not recommend it)
Upvotes: 5