NavigationContentFooter
Jump toSuggest an edit

How to secure a function

Reviewed on 08 February 2024Published on 01 February 2023

This page explains how to secure your function.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • A functions namespace
  • A function
  • Created an authentication token for your function

Use secrets to store access keys and sensitive information

Instead of using environment variables (which are stored in clear text) for your functions, use secrets. These are pieces of information that can be used via environment variables, but are encrypted in storage.

Tip

You can create an API key and store it with secrets to grant your functions access to the Scaleway API.

Configure secrets from the Scaleway console

  1. Click Functions in the Serverless section of the side menu. The functions page displays.
  2. Click the relevant function namespace.
  3. Click the name of the function for which you want to define secrets.
  4. Click the Settings tab.
  5. Scroll to the Secrets section of the page and click Add secret. Enter the key and value for your secret. Repeat for additional secrets.
    Important

    Be careful when you type your secrets. You will not be able to read the value in the console once submitted.

  6. Click Save settings to submit your secrets and redeploy your function.

Configure secrets using the Serverless framework

Add secret to your function’s description (more information in the plugin documentation). We recommend using them with global environment variables or a .env file stored independently (and kept secret).

secret:
secret_1: ${env:SCW_SECRET_KEY}
Important

We strongly suggest that you do not commit this in a Version Control System (VCS), and do not share your Project ID or access key. This helps to ensure the security of your configuration file, which may contain sensitive data.

Configure secrets using Terraform

Add the following resource description in Terraform:

secret_environment_variables = { "key" = "secret" }

Restrict access to your functions

You can set Serverless Functions as private if you want to protect your functions from unwanted or unauthorized calls.

Unauthenticated calls will be rejected, and your function will not be triggered. This feature is handy if an event triggers your function (CRON, SQS or NATS trigger) or if you put them behind an API gateway or a proxy server (see examples in serverless-examples).

Restrict access from the Scaleway console

  1. Click Functions in the Serverless section of the side menu. The functions page displays.
  2. Click the relevant function namespace.
  3. Click the name of the function for which you want to define secrets.
  4. Click the Security tab.
  5. Set the Privacy Policy of the function to Private.
  6. If required, create an access token for your function.
curl -H "X-Auth-Token: <YOUR_FUNCTION_TOKEN>" <YOUR_FUNCTION_ENDPOINT>

Restrict access using the Serverless framework

Set privacy: private in your functions description file.

Your function is now private, and requires an X-Auth-Token header to be called:

curl -H "X-Auth-Token: <YOUR_FUNCTION_TOKEN>" <YOUR_FUNCTION_ENDPOINT>

Refer to the How to create an authentication token documentation for more information.

Restrict access using Terraform

Set privacy = "private" in your Terraform resource description.

You can generate access credentials to inject in other applications (containers, functions etc.) directly from Terraform using the function_token resource.

Set up alerts in Observability Cockpit (upcoming feature)

Using Scaleway Observability Cockpit, a managed Grafana solution to which all your functions are connected, you can:

  • Monitor your functions using the default dashboard or create custom ones.
  • Set up notifications to be alerted in case of unexpected behavior.
See also
How to create and manage an authentication token from the consoleHow to delete a function
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway