The Secret Life of AWS: The On-Demand Function
Why pay for a server 24/7 when your code only runs for five minutes
Part 9 of The Secret Life of AWS
Timothy sat at his desk, staring at a very long bill. He tapped his pen against the paper, looking annoyed.
"Margaret," he said. "We have a problem. The 'Nightly Report' server."
Margaret looked up from her tea. "What about it?"
"It costs us a fortune," Timothy said. "I have a t3.large instance running 24 hours a day. But the report script only takes five minutes to run at midnight. For the other 23 hours and 55 minutes, the server just sits there. Doing nothing."
"But I still have to pay for it," he sighed. "Because if I turn it off, the script won't run."
Margaret stood up and walked to the chalkboard. "You are thinking in terms of Servers, Timothy. You need to think in terms of Functions."
She wiped the board clean. "Today, we discuss AWS Lambda."
Serverless (The Concept)
Margaret drew a traditional server box on the left, and a floating piece of paper on the right.
"With EC2," she said pointing to the box, "you rent the house. You pay rent whether you are sleeping, cooking, or on vacation. You are responsible for the walls, the plumbing, and the operating system."
She pointed to the paper.
"With Lambda, you do not rent the house. You simply hand AWS a piece of code—a Function."
"Where does the code live?" Timothy asked.
"Ideally, nowhere," Margaret said. "It sits in storage, dormant. It does not exist as a running process. You pay nothing. Zero."
"Then how does it run?"
Events (The Trigger)
"It waits for a Trigger," Margaret said. She wrote EVENT on the board with an arrow pointing to the Function.
"A trigger can be anything:
- A file uploaded to S3.
- A user visiting a URL (API Gateway).
- A schedule (Cron job) at midnight."
"When the clock strikes midnight," she explained, "AWS spins up a tiny, temporary environment just for your code. It runs your script. It generates the report. And the moment the script finishes..."
She snapped her fingers.
"It vanishes. The environment is destroyed. You stop paying the millisecond it finishes."
Scaling (The Superpower)
"But what if we get busy?" Timothy asked. "What if 1,000 people upload files at the exact same moment?"
"That is the magic," Margaret smiled. "With a server, it would crash. With Lambda, AWS simply spins up 1,000 separate functions simultaneously. Each one handles one file."
"You do not need to buy more servers. You do not need to configure auto-scaling. It just happens."
Pricing (The Millisecond)
Timothy’s eyes widened. "So instead of paying for 24 hours..."
"You pay for 5 minutes," Margaret said. "Actually, you pay for Duration (milliseconds) and Memory (RAM). If your script takes 300 milliseconds, you pay for 300 milliseconds. If no one runs the report, you pay absolutely nothing."
"This is perfect!" Timothy cheered. "I will move everything to Lambda! The web server, the database, the long-running archives!"
Margaret held up a hand. "Not so fast. Lambda is powerful, but it has rules."
The Limitations (Timeouts & State)
Margaret drew a stopwatch on the board set to 15:00.
"Rule Number One: The Timeout."
"Lambda is for short, bursty tasks," she warned. "The maximum time a function can run is 15 minutes. If your report takes 16 minutes to generate, the function will simply die. AWS will kill it mid-process. It is not for long-running jobs."
"And Rule Number Two," she added, is "Statelessness."
"Because the environment vanishes when the code finishes, nothing is saved. Think of each function as a temporary employee with no memory of yesterday's work."
"If your function counts website visits," she explained, "it cannot store that number in its own memory. The next time it runs, the memory will be wiped clean. If you want to save data, you must send it to an external database like DynamoDB or S3."
The Cold Start (The Trade-off)
"Is there a catch?" Timothy asked. "It sounds too efficient."
"There is the Cold Start," Margaret admitted.
She drew a sleeping figure.
"Since your code is dormant, it takes a moment to wake up. When the first event comes in, AWS has to find a server, download your code, and start the environment. This might take 100 milliseconds to a few seconds."
"For a nightly report, that is fine," Timothy said.
"For a nightly report, it is irrelevant," Margaret agreed. "But for a high-speed trading application? That one-second delay might be unacceptable."
The Lesson
Timothy looked at his bill again. He crossed out the line item for the 'Nightly Report Server'.
"I don't need a butler standing in the kitchen 24 hours a day," he realized. "I just need someone to come in, pour the tea, and leave."
"Precisely," Margaret smiled. "We call this Event-Driven Architecture. We do not pay for idle time. We pay only for value."
She handed him the eraser.
"Now, go delete that server. And Timothy?"
"Yes?"
"Make sure your script finishes in under 15 minutes. Or you will have a very incomplete report."
Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like a Genius.


Comments
Post a Comment