Skip to content

Commit a91f73f

Browse files
committed
Feedback from Mark on the article
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 60ccb28 commit a91f73f

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

_posts/2025-03-24-program-to-function.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ hide_header_image: true
1414

1515
In this post we'll look at how to take a regular command-line program, script, or HTTP server and convert it into a serverless function, along with some of the benefits of doing so.
1616

17-
I just got off a call with a Director of IT for a non-profit in North Carolina. He told me that he had around 40 scripts that he kept on his laptop, and ran manually from time to time. He also wanted to make one of them available to around 600 employees to submit an annual report, for central processing. You could think of this collection of code as traditional "back office" processing - the parts that make the system work. He found out about functions, and thought it would be easier to manage than writing an API and deploying it to a cloud VM.
17+
I just got off a call with a Director of IT for a non-profit in North Carolina. He told me that he had around 40 Python scripts that he kept on his laptop, and ran manually from time to time. He also wanted to make one of them available to around 600 employees to submit an annual report, for central processing. You could think of this collection of code as traditional "back office" processing - the parts that make the system work. He found out about functions, and thought it would be easier to manage than writing an API and deploying it to a cloud VM.
1818

19-
This post is for you (if like him) want to get your code into production in a quick and reliable way, without getting bogged down with making choices about infra, hosting, monitoring, and security. Serverless covers most of this for you, so you can focus on solving the problem at hand.
19+
This post is for you, if like him, want to get your code into production in a quick and reliable way, without getting bogged down with making choices about infra, hosting, monitoring, and security. Serverless covers most of this for you, so you can focus on solving the problem at hand.
2020

2121
We'll first look at the concept of a function, how they run on cloud solutions, and how self-hosted can sometimes be a better option. We'll then go through the mechanics of input, output, configuration, state, dealing with files and secrets, and there'll be lots of code examples along the way.
2222

2323
## What is a Serverless Function?
2424

25-
Functions are stateless, ephemeral, and event-driven, meaning they can be triggered by various events such as HTTP requests, file uploads, or database changes. With the idea that developers focus on writing code, rather than managing servers.
25+
Functions are stateless, ephemeral, and event-driven, meaning they can be triggered by various events such as HTTP requests, file uploads, or database changes. Developers can focus on writing code, rather than managing servers.
2626

27-
The concept originated from the cloud, being popularlised by the AWS Lambda service and is now widely available from other providers such as Google Cloud Functions and Azure Functions. Lambda is a SaaS service designed to serve hundreds of thousands of tenants in an efficient, and cost-effective manner.
27+
The concept originated from the cloud, being popularised by the AWS Lambda service and is now widely available from other providers such as Google Cloud Functions and Azure Functions. Lambda is a SaaS service designed to serve hundreds of thousands of tenants in an efficient, and cost-effective manner.
2828

29-
In order to run a reliable and profitable service, AWS had to implement a stringent set of limits, and capabilities, which can leave developers feeling frustrated when they want to do something outside of the set limits, such as running an execution over an extended period of time, running on-premises, or deploying existing code to another cloud provider, [or even using a GPU](https://www.openfaas.com/blog/transcribe-audio-with-openai-whisper/).
29+
In order to run a reliable and profitable service, AWS had to implement a stringent set of limits, and capabilities, which can leave developers feeling frustrated when they want to do something outside of the set limits, such as running an execution over an extended period of time, running on-premises, deploying existing code to another cloud provider, or [even using a GPU](https://www.openfaas.com/blog/transcribe-audio-with-openai-whisper/).
3030

31-
Functions also offer benefits over traditional server-based applications by simplifying packaging, deployment, and management
31+
Functions also offer benefits over traditional server-based applications by simplifying packaging, deployment, and management.
3232

3333
So what if we could take the concept of functions, but solve for some of these issues?
3434

3535
**How OpenFaaS helps**
3636

3737
OpenFaaS takes the familiar model of functions, and makes them portable, and configurable. You can now run them not only on AWS using a service like AWS EKS, but on Google Cloud, Azure, Oracle Cloud, and even on-premises with your own hardware.
3838

39-
How? The paradigm shifts when you self-host functions using containers & Kubernetes. Where once you were limited to a 15 minute timeout, you can now run an execution for hours, or even days. Where you couldn't use a GPU, you can now allocate one or more to a function, or even package a popular LLM such as Deepseek to serve requests.
39+
How? The paradigm shifts when you self-host functions using containers and Kubernetes. Where once you were limited to a 15 minute timeout, you can now run an execution for hours, or even days. Where you couldn't use a GPU, you can now allocate one or more to a function, or even package a popular LLM such as Deepseek to serve requests.
4040

4141
It also improves the developer experience. You can install the same platform on your machine and test your functions fully on your own machine, with a fast feedback loop, before publishing them to production.
4242

@@ -237,6 +237,8 @@ echo | faas-cli invoke http-to-json \
237237
-H "X-Parse-Url=https://hacker-news.firebaseio.com/v0/topstories.json"
238238
```
239239

240+
The faas-cli command requires an input from STDIN, so we can either run the command and give an input, or pass an empty input with `echo`.
241+
240242
### Environment variables for configuration
241243

242244
Environment variables are used for static configuration for many kinds of programs, including HTTP servers. You may be setting an option for log verbosity, or the URL for a dataset that is needed for the program to operate.
@@ -262,8 +264,8 @@ functions:
262264
lang: golang-middleware
263265
handler: ./flags
264266
image: ttl.sh/flags:latest
265-
+ environment:
266-
+ VERBOSE: "1"
267+
+ environment:
268+
+ VERBOSE: "1"
267269
```
268270

269271
Alternatively, we can supply the name of an environment file. This is useful for when you want to deploy the same function to multiple different environments or regions, and just want to change the environment variables for each one.
@@ -286,10 +288,36 @@ functions:
286288
lang: golang-middleware
287289
handler: ./flags
288290
image: ttl.sh/flags:latest
289-
+ environment_file:
290-
+ - dev.env
291+
+ environment_file:
292+
+ - dev.env
293+
```
294+
295+
Then, if you needed to replace the name of the environment file, you could do so in the `stack.yml` file using environment variables.
296+
297+
```diff
298+
functions:
299+
flags:
300+
lang: golang-middleware
301+
handler: ./flags
302+
image: ttl.sh/flags:latest
303+
environment_file:
304+
+ - ${ENV_FILE:-dev.env}
291305
```
292306

307+
Then you have the option to set the environment variable `ENV_FILE` to the name of the file you want to use.
308+
309+
```bash
310+
# Deploy with the development configuration
311+
faas-cli up
312+
313+
# Deploy with the staging configuration
314+
ENV_FILE=staging.env faas-cli up
315+
316+
# Deploy with the production configuration
317+
ENV_FILE=prod.env faas-cli up
318+
```
319+
320+
293321
Here's how we can read the environment variable in Go:
294322

295323
```golang
@@ -369,7 +397,7 @@ mkdir -p tmpl/static
369397

370398
cat <<EOF > tmpl/static/welcome.html.tpl
371399
<html>
372-
Hello, {{.Name}}
400+
{% raw %}Hello, {{.Name}}{% endraw %}
373401
</html>
374402
EOF
375403
```
@@ -400,7 +428,7 @@ func init() {
400428
}
401429

402430
type WelcomeRequest struct {
403-
Name string `json:"name"`
431+
Name string
404432
}
405433

406434
func Handle(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)