Build Azure Infrastructure Using AI and Terraform

5 min read

Imagine deploying a fully working Azure infrastructure with production-ready Terraform modules directly from your application code. This isn't a dream - it's Nitric! We've just released support for generating Terraform for Azure, and in this post, I'll walk you through how it works, from code to cloud in minutes, with a special focus on using AI to enhance the development experience.

Prefer a video version? Check it out below:

Prerequisites

Before we begin, make sure you have the following installed:

  • Node.js
  • Docker
  • Nitric CLI (install using one of the following methods):
# macOS
brew install nitrictech/tap/nitric
# Windows (using scoop)
scoop bucket add nitric https://github.com/nitrictech/scoop-bucket.git
scoop install nitric
# Linux
curl -L "https://nitric.io/install?version=latest" | bash

For more installation options and dependency information, review the full installation guide.

Initial Setup

Let's create a new Nitric project:

nitric new my-azure-app ts-starter

This will create a new project with the following structure:

my-azure-app/
├── services/
│ └── api.ts
├── .gitignore
├── nitric.yaml
└── package.json

Install the project dependencies:

cd my-azure-app
yarn install # or your preferred package manager

Project Setup

Let's start with a simple Nitric project. Open the services/api.ts file and replace its contents with the following Express application code:

import { http } from '@nitric/sdk'
import express from 'express'
const app = express()
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() })
})
// Echo endpoint
app.get('/echo', (req, res) => {
res.json({
message: 'Echo received',
data: req.query,
timestamp: new Date().toISOString(),
})
})
// Wrap the Express app with Nitric's HTTP resource
http(app, () => {
console.log('Application started')
})

This code creates an Express application with:

  • A health check endpoint that returns the current status and timestamp
  • An echo endpoint that returns the received query parameters along with a timestamp

The Express app is wrapped using Nitric's HTTP resource, making it deployable to the cloud. The nitric.yaml file contains the default configuration that tells Nitric:

  • Where to find services using glob patterns
  • How to build them using a standard Node.js Dockerfile

Generating Terraform for Azure

To deploy our application to Azure, we'll create a new Terraform stack:

nitric stack new tf azure-tf

This generates a new stack configuration file where we can specify:

  • The Azure region
  • Organization details
  • Subscription ID

Deploying with Terraform

Running nitric up does all the heavy lifting:

  1. Builds the Docker image
  2. Analyzes the infrastructure needs
  3. Generates Terraform modules using CDKTF

The generated Terraform configuration is stored in the cdktf.out folder, containing:

  • The main cdk.tf.json file
  • All the cloud Terraform modules such as service, storage, etc.

Key Generated Modules

  1. HTTP Proxy Module

Location: cdktf.out/stacks/my-azure-app/assets/../nitric_modules/http_proxy

  • Provisions an API Gateway
  • Connects to our service
  • Includes input variables
  1. Service Module

Location: cdktf.out/stacks/my-azure-app/assets/../nitric_modules/service

  • Handles Docker image builds
  • Manages container app hosting
  • Sets up permissions and IAM assignments

You can find all the Nitric Azure Terraform modules in the Nitric repository.

Deploying the Infrastructure

To deploy the generated infrastructure:

cd cdktf.out/stacks/my-azure-app/
terraform init
terraform apply

This will create all necessary Azure resources, including:

  • API Management
  • Container Apps
  • Resource Groups
  • And more

Testing the Deployment

Once deployed, we can test our endpoints using curl:

# Health check
curl https://your-api-url/health
# Echo endpoint
curl https://your-api-url/echo?message=Hello

Enhancing Development with AI

One of the most powerful features of this workflow is the ability to use AI to enhance development. For example, we can use Cursor AI to add new features to our application. Let's add a to-do list feature using Nitric's KV store:

  1. Open the Cursor AI chat (ctrl+l)
  2. Request the implementation:
use nitric KV store to manage a to-do list. use https://nitric.io/docs/keyvalue to help
  1. The AI will generate a full CRUD implementation including:
  • Properly scoped KV resource
  • Required permissions
  • API endpoints
  • Example curl commands

The AI-generated code can be quickly reviewed and implemented, making it easy to extend your application with new features.

Testing the New Features

Test the new to-do list endpoints:

# Create a todo, replace the data to match your generated todo structure
curl -X POST https://your-api-url/todos -d '{"title":"New Todo"}'
# List todos
curl https://your-api-url/todos
# Get specific todo
curl https://your-api-url/todos/{id}
# Delete todo
curl -X DELETE https://your-api-url/todos/{id}'

Regenerating Infrastructure

After making changes with AI assistance:

  1. Run nitric up to regenerate Terraform
  2. The new deployment will include:
    • Storage account
    • Storage table for KV store
    • Updated permissions
    • New Docker image

Cleaning Up

When you're done, clean up all resources with:

terraform destroy

Conclusion

That's how you can build Azure infrastructure using AI and Terraform with Nitric - a powerful combination that makes cloud development more efficient and accessible.

Get Started

Ready to get started?

Join our Discord community to ask questions or share feedback.

Get the most out of Nitric

Ship your first app faster with Next-gen infrastructure automation

Explore the docs