Skip to content

Lab 7 · Lambda & Fargate

Performance · Est. time: 25–30 min · Cost: ✅ Lambda's free tier (1M requests + 400,000 GB-s/month) covers this easily. The optional Fargate task is a few cents.

Goal. Create a Lambda function with an execution role, invoke it, and understand where Lambda fits vs Fargate — the Compute Choices judgment.


Step 1 — Create a Lambda function

  1. Lambda → Create function → Author from scratch. Name lab-fn, runtime Python 3.13.
  2. Under permissions, let Lambda create a new execution role with basic Lambda permissions. Create function.
  3. Replace the code with:
    def handler(event, context):
        name = event.get("name", "world")
        return {"statusCode": 200, "body": f"Hello, {name}!"}
    
    Set the handler to lambda_function.handler (match the file/function name). Deploy.
# zip your function.py containing handler(), then:
aws lambda create-function --function-name lab-fn --runtime python3.13 \
  --handler function.handler --zip-file fileb://function.zip \
  --role arn:aws:iam::<acct>:role/<lambda-exec-role>

The execution role is the point

The function gets its AWS permissions from its execution role — temporary credentials, no stored keys. To let it read DynamoDB/S3, you'd attach those permissions to this role (Topic 1).


Step 2 — Invoke it

Test tab → create an event {"name":"Ashish"}Test. You'll see the response and the execution duration/memory in the log.

aws lambda invoke --function-name lab-fn --payload '{"name":"Ashish"}' out.json
cat out.json

Step 3 — See the limits that decide exam questions

On the Configuration → General configuration tab, note:

  • Timeout — default 3s, max 15 minutes. Anything longer must run on Fargate/EC2/Batch.
  • Memory — 128 MB–10 GB (CPU scales with it).

Try setting timeout to its max and observe the ceiling: 15 min. That single number rules Lambda in or out of many scenarios.


Step 4 (optional) — Where Fargate fits

You don't need to build a Fargate task to learn the distinction, but conceptually:

  • If this workload were a long-running container (say a 40-minute job) or a persistent service, you'd run it on ECS/Fargate — serverless containers, no 15-minute cap, no servers to manage.
  • Lambda = short, event-driven functions; Fargate = serverless containers.

(To actually try it: ECS → create a cluster → run a task on Fargate from a public image like nginx, then stop it. It costs a few cents; stop the task promptly.)


Teardown

Lambda → lab-fn → Delete. If you made an optional ECS cluster/task, stop the task and delete the cluster. Remove the auto-created execution role if you don't need it.

aws lambda delete-function --function-name lab-fn

✅ What you should understand now

  • A Lambda function runs from an execution role — temporary credentials, no stored keys.
  • Lambda's hard limit is 15 minutes; longer or persistent work → Fargate/EC2/Batch.
  • Lambda = serverless functions; Fargate = serverless containers — both scale without managing servers.
  • You pay per request + duration, and it scales to zero when idle — ideal for spiky, event-driven work.

Related: Learn — Compute Choices