You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-03-24-program-to-function.md
+41-13Lines changed: 41 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -14,29 +14,29 @@ hide_header_image: true
14
14
15
15
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.
16
16
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.
18
18
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.
20
20
21
21
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.
22
22
23
23
## What is a Serverless Function?
24
24
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.
26
26
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.
28
28
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/).
30
30
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.
32
32
33
33
So what if we could take the concept of functions, but solve for some of these issues?
34
34
35
35
**How OpenFaaS helps**
36
36
37
37
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.
38
38
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.
40
40
41
41
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.
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
+
240
242
### Environment variables for configuration
241
243
242
244
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:
262
264
lang: golang-middleware
263
265
handler: ./flags
264
266
image: ttl.sh/flags:latest
265
-
+environment:
266
-
+VERBOSE: "1"
267
+
+ environment:
268
+
+ VERBOSE: "1"
267
269
```
268
270
269
271
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:
286
288
lang: golang-middleware
287
289
handler: ./flags
288
290
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}
291
305
```
292
306
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
+
293
321
Here's how we can read the environment variable in Go:
294
322
295
323
```golang
@@ -369,7 +397,7 @@ mkdir -p tmpl/static
369
397
370
398
cat <<EOF > tmpl/static/welcome.html.tpl
371
399
<html>
372
-
Hello, {{.Name}}
400
+
{% raw %}Hello, {{.Name}}{% endraw %}
373
401
</html>
374
402
EOF
375
403
```
@@ -400,7 +428,7 @@ func init() {
400
428
}
401
429
402
430
typeWelcomeRequeststruct {
403
-
Namestring`json:"name"`
431
+
Namestring
404
432
}
405
433
406
434
funcHandle(whttp.ResponseWriter, r *http.Request) {
0 commit comments