Skip to content

Instantly share code, notes, and snippets.

@5ran6
Last active October 24, 2023 06:01
Show Gist options
  • Save 5ran6/1a77247b6f24c6c831ca35c4a3fbe487 to your computer and use it in GitHub Desktop.
Save 5ran6/1a77247b6f24c6c831ca35c4a3fbe487 to your computer and use it in GitHub Desktop.
[SERVERLESS] How to use Digital Oceans app functions

OFFICIAL DOCUMENTATION can be found here

KEY TAKEAWAYS

RUNTIME

TIMEOUT

As of September 29, 2022: DigitalOcean Functions now support a maximum timeout of 15 minutes. The 15-minute maximum timeout provides ample execution time for the functions we will build in this auto-provider service. However, it's best practice to optimize the code to use the shortest reasonable runtime. This avoids accruing unnecessary costs for extended execution.

Additionally, be mindful of cold starts when architecting the functions. Cold starts occur when a function first spins up and initializes before it can handle requests. For Go functions on DigitalOcean, the average cold start latency is around 500ms (Go, Node.js, Python, and Ruby functions have been benchmarked in the 400-600ms range for cold starts).

By keeping functions lean and minimizing initialization steps, we can reduce cold start durations. When designing the functions, we'll focus on optimizations like initializing connections once centrally, lazy loading dependencies, and minimizing external calls. With some careful tuning, we can create performant and cost-effective functions.

Longer timeouts enable functions to handle more complex and compute-intensive tasks such as video and image processing, data transformation, and report generation. Can be found here

LANGUAGES

As of October 21, 2023, it supports Go, PHP, JavaScript, and Python language runtimes

ENV (Variables)

You can create your .env file for any runtime. To access your .env variables in your application, use the default way of doing it. E.g, for Go lang, we use os.Getenv("PORT"). For PHP we use, getenv("PORT"). In addition, your .env values has to be referenced in your project.yml file under the environment key for each package or action.

E.G project.yml

    parameters: {}
    environment:
      PORT: ${PORT}
    

DOCTL

doctl is the command line interface for Digital Ocean on your computer. On a Mac, use brew install doctl to install it. On Windows, Linux, and others, visit here for the steps as they are more than one line. You have to create an access token on your DO account so your doctl can use it to access your account. Create one here. Once created, use doctl auth init and supply this key. Now you are connected to your DO account via CLI. The full doctl documentation and guide can be found here or just type doctl in your terminal for a list of options. NB: If you want to manage multiple accounts/workspace/contexts, use this as a guide.

APP, NAMESPACE, PACKAGE AND ACTIONS

An application contains a project namespace. In that namespace, you can have an arbitrary number of packages and each package can have an arbitrary number of actions. That is the hierarchy.

E.G

├── packages
│   └── <package-name>
│       └── <function-name>
│           ├── main.go
│           └── to_be_included.txt
└── project.yml

Implementation

provider-automated-service/
├── packages
│   └── moniFly
│       ├── accounts
│       │   ├── main.go 
│       │   ├── go.sum
│       │   └── go.mod
│       ├── profile
│       │   ├── main.go
│       │   ├── go.sum  
│       │   └── go.mod
│       ├── finance   
│       │   ├── main.go
│       │   ├── go.sum
│       │   └── go.mod
│       ├── options
│       │   ├── main.go
│       │   ├── go.sum
│       │   └── go.mod
│       └── binance
│           ├── kyc
│           │   ├── main.go
│           │   ├── go.sum
│           │   └── go.mod
│           └── trades
│               ├── main.go
│               ├── go.sum
│               └── go.mod
├── project.yml
├── app-spec.yml
└── .env

In the above example, the application name will be auto-generated by DO. It can be named something like shoe-suffer-gym. The namespace is serveless-service. And it has two packages (moniFly and binace). moniFly package has 4 functions and binance has 2 functions. Remember, the folder name package is compulsory as the word 'package' is a key word. This is reflected in the project configurations file like this:

project.yml

parameters: {}
environment:
  PORT: ${PORT}

packages:
  - name: moniFly
    environment: {}
    parameters: {}
    actions:
      - name: accounts
        main: main
        runtime: 'go:default'
        environment: {}
        parameters: {}
      - name: profile
        main: main
        runtime: 'go:default'
        environment: {}
        parameters: {}
      - name: finance
        main: main
        runtime: 'go:default'
        environment: {}
        parameters: {}
      - name: options
        main: main
        runtime: 'go:default'
        environment: {}
        parameters: {}
  - name: binance
    environment: {}
    parameters: {}
    actions:
      - name: kyc
        main: main
        runtime: 'go:default'
        environment: {}
        parameters: {}
      - name: trades
        main: main
        runtime: 'go:default'
        environment: {}
        parameters: {}

app-spec.yml is a file used to create provisioning configurations using doctl. That means this is the configuration to provision your application and its namespaces, packages, and functions if you don't want to do this manually on the console.

E.G app-spec.yml

alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
functions:
- github:
    branch: main
    deploy_on_push: true
    repo: paycrest/provider-automated-service
  name: provider-automated-service
  routes:
  - path: /
  source_dir: /
name: provider-automated-service
region: nyc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment