/azure-serverless-openapi-boilerplate

Boilerplate project for a serverless API with automated OpenAPI specification on Azure Functions

Primary LanguageHCLMIT LicenseMIT

OpenAPI Azure Function Typescript Boilerplate

License

Boilerplate project for a serverless API with automated OpenAPI specification on Azure Functions

This project is currently just me figuring out the best ways to do Serverless APIs in Azure with nice tooling.

Features

  • Local development with Azure Functions Core Tools
  • Azure infra deployment with Terraform
    • Azure Function App with Linux Node.js runtime
    • Azure Database for PostgreSQL
    • Azure Redis Cache
  • Continuous deployment using ZIP deployment on Azure
  • Automated OpenAPI specification

Quick Start

Requirements:

source .env.sample # Set up environment variables
npm install
# docker-compose up --detach # PostgreSQL listening at port 5432
# npm run migrate # Set up database schema with knex migrations
npm run dev # Core Tools Start: http://localhost:9000

Deploy

Requirements:

In Azure Portal, gather or create the following items:

  • Subscription
    • ID
  • Resource Group
    • Name
  • Storage Account
    • Name
    • Access Key
  • Blob Storage Container
    • Name

In your .env file, fill the missing environment variables.

# .env
export PROJECT_NAME=<unique name for your project>

export ARM_SUBSCRIPTION_ID=<Subscription ID>
export ARM_RESOURCE_GROUP=<Resource Group Name>
export ARM_ACCESS_KEY=<Storage Account Access Key>

Then source the file to load the environment variables.

source .env

If you wish, you can configure the terraform remote backend to a blob storage container.

See terraform-backend.tf.sample:

terraform {
  backend "azurerm" {
    storage_account_name = "<Storage Account Name>"
    container_name = "<Blob Storage Container Name>"
    key = "terraform.tfstate"
  }
}

Init Terraform and deploy!

terraform init
terraform apply

Publish

npm run build
func azure functionapp publish $PROJECT_NAME-<stage> --zip