Lambda function and event source
This step creates the runner and connects the queue to it.
Create the function
The settings go in an env.json file, which avoids shell-quoting the TASKS
JSON (see Configuration):
{
"Variables": {
"TASKS": "{\"calculator.add\": \"task://calculator/add\", \"calculator.subtract\": \"task://calculator/subtract\", \"calculator.divide\": \"task://calculator/divide\", \"reports.summarize\": \"task://reports/summarize\", \"remote.sum\": \"lambda://arn:aws:lambda:eu-central-1:123456789012:function:remote-sum\"}",
"CELERY_BACKEND_TABLE": "lambda-runner-tasks",
"CELERY_BACKEND_REGION": "eu-central-1",
"LOG_LEVEL": "INFO"
}
}
aws lambda create-function --function-name $FUNCTION --region $AWS_REGION \
--package-type Image \
--code ImageUri=$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/celery-cloud-runner:1 \
--role arn:aws:iam::$ACCOUNT_ID:role/$FUNCTION \
--timeout 300 --memory-size 512 \
--environment file://env.json
Note
A role created seconds ago may not be usable yet; if create-function says it
cannot assume the role, wait a few seconds and retry.
Sizing
Setting |
Guidance |
|---|---|
|
The longest task you expect, plus margin (maximum 900 s = 15 minutes,
the AWS limit: no task can run longer, see Execution time limit). If you use
|
|
Start at 512 MB and adjust from the |
Concurrency |
Each concurrent invocation handles one batch. Cap it with
|
Connect the queue
aws lambda create-event-source-mapping --region $AWS_REGION \
--function-name $FUNCTION \
--event-source-arn $QUEUE_ARN \
--batch-size 10 \
--function-response-types ReportBatchItemFailures
ReportBatchItemFailures is required: it lets the runner ask SQS to redeliver
only the messages whose state it could not record (see
Result expiration and Concepts and behaviour). Without it, messages of a batch
that did not raise are deleted whatever happened.
Optional tuning of the mapping:
--maximum-batching-window-in-seconds 5: wait up to 5 s to fill a batch (fewer invocations, more latency).--scaling-config MaximumConcurrency=10: limit how many invocations run at once.
Update the function
# new code (a new image tag)
aws lambda update-function-code --function-name $FUNCTION --region $AWS_REGION \
--image-uri $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/celery-cloud-runner:2
# new settings (this REPLACES all the variables: send them all)
aws lambda update-function-configuration --function-name $FUNCTION \
--region $AWS_REGION --environment file://env.json
Smoke test
Send one task with a client (Client: sending a task) and follow the logs:
aws logs tail /aws/lambda/$FUNCTION --follow --region $AWS_REGION
You should see the invocation with its trace_id, and the client prints the
result.