Skip to content

Example Azure OpenAI deployment and RBAC role for your user account for keyless access

License

Notifications You must be signed in to change notification settings

Azure-Samples/azure-openai-keyless-js

Repository files navigation

Azure OpenAI keyless deployment

Open project in GitHub Codespaces Watch to learn more about GenAI with JS on YouTube Node version License

⭐ If you like this sample, star it on GitHub — it helps a lot!

OverviewGet startedRun the sampleResourcesGuidance

Overview

This sample shows how to to provision an Azure OpenAI account with an RBAC role permission for your user account to access, so that you can use the OpenAI API SDKs with keyless (Entra) authentication. Keyless authentication is a recommended security practice to reduce risks with API keys.

Getting started

There are multiple ways to get started with this project.

The quickest way is to use GitHub Codespaces that provides a preconfigured environment for you. Alternatively, you can set up your local environment following the instructions below.

Use your local environment

You need to install following tools to work on your local machine:

  • Node.js LTS
  • Azure Developer CLI
  • Git
  • PowerShell 7+ (for Windows users only)
    • Important: Ensure you can run pwsh.exe from a PowerShell command. If this fails, you likely need to upgrade PowerShell.
    • Instead of Powershell, you can also use Git Bash or WSL to run the Azure Developer CLI commands.

Then you can get the project code:

  1. Fork the project to create your own copy of this repository.
  2. On your forked repository, select the Code button, then the Local tab, and copy the URL of your forked repository.
Screenshot showing how to copy the repository URL
3. Open a terminal and run this command to clone the repo: git clone <your-repo-url>

Use GitHub Codespaces

You can run this project directly in your browser by using GitHub Codespaces, which will open a web-based VS Code:

Open in GitHub Codespaces

Use a VSCode dev container

A similar option to Codespaces is VS Code Dev Containers, that will open the project in your local VS Code instance using the Dev Containers extension.

You will also need to have Docker installed on your machine to run the container.

Open in Dev Containers

Run the sample

Azure prerequisites

  • Azure account. If you're new to Azure, get an Azure account for free to get free Azure credits to get started. If you're a student, you can also get free credits with Azure for Students.
  • Azure subscription with access enabled for the Azure OpenAI service. You can request access with this form.
  • Azure account permissions:
    • Your Azure account must have Microsoft.Authorization/roleAssignments/write permissions, such as Role Based Access Control Administrator, User Access Administrator, or Owner. If you don't have subscription-level permissions, you must be granted RBAC for an existing resource group and deploy to that existing group by running these commands:
      azd env set AZURE_RESOURCE_GROUP <name of existing resource group>
      azd env set AZURE_LOCATION <location of existing resource group>
    • Your Azure account also needs Microsoft.Resources/deployments/write permissions on the subscription level.

Costs

Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. However, you can use the Azure pricing calculator for the resources below to get an estimate.

  • Azure OpenAI: Standard tier, GPT model. Pricing per 1K tokens used, and at least 1K tokens are used per question. Pricing

Provision Azure OpenAI resources

  1. Open a terminal and navigate to the root of the project.
  2. Authenticate with Azure by running azd auth login.
  3. Run azd provision to provision the Azure resources.
    • You will be prompted to select a location for your OpenAI resource. If you're unsure of which location to choose, select eastus2. See OpenAI model availability table for more information.

The deployment process will take a few minutes. Once it's done, a .env file will be created in the root folder with the environment variables needed to run the application.

Run the JavaScript sample

First make sure you have provisioned the Azure OpenAI resources, and that you have the .env file in the root folder.

  1. Run npm install to install the dependencies.
  2. Run npm start to run the example.

This will use the OpenAI SDK to make a request to the OpenAI API and print the response to the console.

Clean up

To clean up all the Azure resources created by this sample:

  1. Run azd down --purge
  2. When asked if you are sure you want to continue, enter y

The resource group and all the resources will be deleted.

Resources

Here are some resources to learn more about Azure OpenAI and related technologies:

You can also find more Azure AI samples here.

Guidance

Region availability

This template uses model gpt-35-turbo (0613) which may not be available in all Azure regions. Check for up-to-date region availability and select a region during deployment accordingly.

We recommend using East US 2 if you're unsure of which region to choose.

Security

This template has Managed Identity built in to eliminate the need for developers to manage these credentials. Applications can use managed identities to obtain Microsoft Entra tokens without having to handle any secrets in the code. Additionally, we're using Microsoft Security DevOps GitHub Action to scan the infrastructure-as-code files and generates a report containing any detected issues.

Troubleshooting

If you have any issue when running or deploying this sample, please check the troubleshooting guide. If you can't find a solution to your problem, please open an issue in this repository.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.