|
| 1 | +--- |
| 2 | +title: Create and manage Azure Cosmos DB with terraform |
| 3 | +description: Use terraform to create and configure Azure Cosmos DB for Core (SQL) API |
| 4 | +author: ginsiucheng |
| 5 | +ms.service: cosmos-db |
| 6 | +ms.subservice: cosmosdb-sql |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 09/16/2022 |
| 9 | +ms.author: gicheng |
| 10 | +ms.reviewer: mjbrown |
| 11 | +--- |
| 12 | + |
| 13 | +# Manage Azure Cosmos DB Core (SQL) API resources with terraform |
| 14 | + |
| 15 | +[!INCLUDE[appliesto-sql-api](../includes/appliesto-sql-api.md)] |
| 16 | + |
| 17 | +In this article, you learn how to use terraform to deploy and manage your Azure Cosmos DB accounts, databases, and containers. |
| 18 | + |
| 19 | +This article shows terraform samples for Core (SQL) API accounts. |
| 20 | + |
| 21 | +> [!IMPORTANT] |
| 22 | +> |
| 23 | +> * Account names are limited to 44 characters, all lowercase. |
| 24 | +> * To change the throughput (RU/s) values, redeploy the terraform file with updated RU/s. |
| 25 | +> * When you add or remove locations to an Azure Cosmos account, you can't simultaneously modify other properties. These operations must be done separately. |
| 26 | +> * To provision throughput at the database level and share across all containers, apply the throughput values to the database options property. |
| 27 | +
|
| 28 | +To create any of the Azure Cosmos DB resources below, copy the example into a new terraform file (main.tf) or alternatively, have 2 separate files for resources (main.tf) and variables (variables.tf). Be sure to include the azurerm provider either in the main terraform file or split out to a separate providers file. All examples can be found on the [terraform samples repository](https://github.com/Azure/terraform). |
| 29 | + |
| 30 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-autoscale/providers.tf"::: |
| 31 | + |
| 32 | +<a id="create-autoscale"></a> |
| 33 | + |
| 34 | +## Azure Cosmos account with autoscale throughput |
| 35 | + |
| 36 | +Create an Azure Cosmos account in two regions with options for consistency and failover, with database and container configured for autoscale throughput that has most index policy options enabled. |
| 37 | + |
| 38 | +### main.tf |
| 39 | + |
| 40 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-autoscale/main.tf"::: |
| 41 | + |
| 42 | +### variables.tf |
| 43 | + |
| 44 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-autoscale/variables.tf"::: |
| 45 | + |
| 46 | +<a id="create-analytical-store"></a> |
| 47 | + |
| 48 | +## Azure Cosmos account with analytical store |
| 49 | + |
| 50 | +Create an Azure Cosmos account in one region with a container with Analytical TTL enabled and options for manual or autoscale throughput. |
| 51 | + |
| 52 | +### main.tf |
| 53 | + |
| 54 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-analyticalstore/main.tf"::: |
| 55 | + |
| 56 | +### variables.tf |
| 57 | + |
| 58 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-analyticalstore/variables.tf"::: |
| 59 | + |
| 60 | +<a id="create-manual"></a> |
| 61 | + |
| 62 | +## Azure Cosmos account with standard provisioned throughput |
| 63 | + |
| 64 | +Create an Azure Cosmos account in two regions with options for consistency and failover, with database and container configured for standard throughput that has most policy options enabled. |
| 65 | + |
| 66 | +### main.tf |
| 67 | + |
| 68 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-manualscale/main.tf"::: |
| 69 | + |
| 70 | +### variables.tf |
| 71 | + |
| 72 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-manualscale/variables.tf"::: |
| 73 | + |
| 74 | +<a id="create-sproc"></a> |
| 75 | + |
| 76 | +## Azure Cosmos DB container with server-side functionality |
| 77 | + |
| 78 | +Create an Azure Cosmos account, database and container with a stored procedure, trigger, and user-defined function. |
| 79 | + |
| 80 | +### main.tf |
| 81 | + |
| 82 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-serverside-functionality/main.tf"::: |
| 83 | + |
| 84 | +### variables.tf |
| 85 | + |
| 86 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-serverside-functionality/variables.tf"::: |
| 87 | + |
| 88 | +<a id="create-rbac"></a> |
| 89 | + |
| 90 | +## Azure Cosmos DB account with Azure AD and RBAC |
| 91 | + |
| 92 | +Create an Azure Cosmos account, a natively maintained Role Definition, and a natively maintained Role Assignment for an Azure Active Directory identity. |
| 93 | + |
| 94 | +### main.tf |
| 95 | + |
| 96 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-aad-rbac/main.tf"::: |
| 97 | + |
| 98 | +### variables.tf |
| 99 | + |
| 100 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-aad-rbac/variables.tf"::: |
| 101 | + |
| 102 | +<a id="free-tier"></a> |
| 103 | + |
| 104 | +## Free tier Azure Cosmos DB account |
| 105 | + |
| 106 | +Create a free-tier Azure Cosmos account and a database with shared throughput that can be shared with up to 25 containers. |
| 107 | + |
| 108 | +### main.tf |
| 109 | + |
| 110 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-free-tier/main.tf"::: |
| 111 | + |
| 112 | +### variables.tf |
| 113 | + |
| 114 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-cosmos-db-free-tier/variables.tf"::: |
| 115 | + |
| 116 | +## Next steps |
| 117 | + |
| 118 | +Here are some additional resources: |
| 119 | + |
| 120 | +* [Install Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) |
| 121 | +* [Terraform Azure Tutorial](https://learn.hashicorp.com/collections/terraform/azure-get-started) |
| 122 | +* [Terraform tools](https://www.terraform.io/docs/terraform-tools) |
| 123 | +* [Azure Provider Terraform documentation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs) |
| 124 | +* [Terraform documentation](https://www.terraform.io/docs) |
0 commit comments