Skip to content

Commit f066c15

Browse files
committed
Apply terraform code references and includes
1 parent ffe5e8c commit f066c15

File tree

1 file changed

+13
-275
lines changed

1 file changed

+13
-275
lines changed

articles/iot-dps/quick-setup-auto-provision-terraform.md

Lines changed: 13 additions & 275 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quickstart - Use Terraform to create a DPS instance
33
description: Learn how to deploy an Azure IoT Device Provisioning Service (DPS) resource with Terraform in this quickstart.
44
keywords: azure, devops, terraform, device provisioning service, DPS, IoT, IoT Hub DPS
55
ms.topic: quickstart
6-
ms.date: 09/27/2022
6+
ms.date: 10/27/2022
77
ms.custom: devx-track-terraform
88
author: kgremban
99
ms.author: kgremban
@@ -34,292 +34,44 @@ In this article, you learn how to:
3434

3535
## Prerequisites
3636

37-
- **Azure subscription**: If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
37+
[!INCLUDE [open-source-devops-prereqs-azure-subscription.md](~/azure-dev-docs-pr/articles/includes/open-source-devops-prereqs-azure-subscription.md)]
38+
3839
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
3940

41+
## Implement the Terraform code
42+
4043
> [!NOTE]
4144
> The example code in this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/). See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/developer/terraform/)
4245
43-
## Implement the Terraform code
44-
4546
1. Create a directory in which to test and run the sample Terraform code and make it the current directory.
4647

4748
1. Create a file named `providers.tf` and insert the following code:
4849

49-
```terraform
50-
terraform {
51-
required_version = ">=0.12"
52-
53-
required_providers {
54-
azurerm = {
55-
source = "hashicorp/azurerm"
56-
version = "~>2.0"
57-
}
58-
random = {
59-
source = "hashicorp/random"
60-
version = "~>3.0"
61-
}
62-
}
63-
}
64-
65-
provider "azurerm" {
66-
features {}
67-
}
68-
```
50+
[!code-terraform[master](~/terraform_samples/quickstart/201-iot-hub-with-device-provisioning-service/providers.tf)]
6951

7052
1. Create a file named `main.tf` and insert the following code:
7153

72-
```terraform
73-
resource "random_pet" "rg_name" {
74-
prefix = var.resource_group_name_prefix
75-
}
76-
77-
resource "azurerm_resource_group" "rg" {
78-
location = var.resource_group_location
79-
name = random_pet.rg_name.id
80-
}
81-
82-
# Create storage account & container
83-
resource "random_string" "sa_name" {
84-
length = 12
85-
special = false
86-
upper = false
87-
}
88-
89-
resource "azurerm_storage_account" "sa" {
90-
name = random_string.sa_name.id
91-
resource_group_name = azurerm_resource_group.rg.name
92-
location = azurerm_resource_group.rg.location
93-
account_tier = "Standard"
94-
account_replication_type = "LRS"
95-
}
96-
97-
resource "azurerm_storage_container" "my_terraform_container" {
98-
name = "mycontainer"
99-
storage_account_name = azurerm_storage_account.sa.name
100-
container_access_type = "private"
101-
}
102-
103-
104-
# Create an Event Hub & Authorization Rule
105-
resource "random_pet" "eventhubnamespace_name" {
106-
prefix = var.eventhub_namespace_name_prefix
107-
}
108-
109-
resource "azurerm_eventhub_namespace" "namespace" {
110-
name = random_pet.eventhubnamespace_name.id
111-
resource_group_name = azurerm_resource_group.rg.name
112-
location = azurerm_resource_group.rg.location
113-
sku = "Basic"
114-
}
115-
116-
resource "azurerm_eventhub" "my_terraform_eventhub" {
117-
name = "myEventHub"
118-
resource_group_name = azurerm_resource_group.rg.name
119-
namespace_name = azurerm_eventhub_namespace.namespace.name
120-
partition_count = 2
121-
message_retention = 1
122-
}
123-
124-
resource "azurerm_eventhub_authorization_rule" "my_terraform_authorization_rule" {
125-
resource_group_name = azurerm_resource_group.rg.name
126-
namespace_name = azurerm_eventhub_namespace.namespace.name
127-
eventhub_name = azurerm_eventhub.my_terraform_eventhub.name
128-
name = "acctest"
129-
send = true
130-
}
131-
132-
133-
# Create an IoT Hub
134-
resource "random_pet" "iothub_name" {
135-
prefix = var.iothub_name_prefix
136-
length = 1
137-
}
138-
139-
resource "azurerm_iothub" "iothub" {
140-
name = random_pet.iothub_name.id
141-
resource_group_name = azurerm_resource_group.rg.name
142-
location = azurerm_resource_group.rg.location
143-
144-
sku {
145-
name = "S1"
146-
capacity = "1"
147-
}
148-
149-
endpoint {
150-
type = "AzureIotHub.StorageContainer"
151-
connection_string = azurerm_storage_account.sa.primary_blob_connection_string
152-
name = "export"
153-
batch_frequency_in_seconds = 60
154-
max_chunk_size_in_bytes = 10485760
155-
container_name = azurerm_storage_container.my_terraform_container.name
156-
encoding = "Avro"
157-
file_name_format = "{iothub}/{partition}_{YYYY}_{MM}_{DD}_{HH}_{mm}"
158-
}
159-
160-
endpoint {
161-
type = "AzureIotHub.EventHub"
162-
connection_string = azurerm_eventhub_authorization_rule.my_terraform_authorization_rule.primary_connection_string
163-
name = "export2"
164-
}
165-
166-
route {
167-
name = "export"
168-
source = "DeviceMessages"
169-
condition = "true"
170-
endpoint_names = ["export"]
171-
enabled = true
172-
}
173-
174-
route {
175-
name = "export2"
176-
source = "DeviceMessages"
177-
condition = "true"
178-
endpoint_names = ["export2"]
179-
enabled = true
180-
}
181-
182-
enrichment {
183-
key = "tenant"
184-
value = "$twin.tags.Tenant"
185-
endpoint_names = ["export", "export2"]
186-
}
187-
188-
cloud_to_device {
189-
max_delivery_count = 30
190-
default_ttl = "PT1H"
191-
feedback {
192-
time_to_live = "PT1H10M"
193-
max_delivery_count = 15
194-
lock_duration = "PT30S"
195-
}
196-
}
197-
198-
tags = {
199-
purpose = "testing"
200-
}
201-
}
202-
203-
#Create IoT Hub Access Policy
204-
resource "azurerm_iothub_shared_access_policy" "hubaccesspolicy" {
205-
name = "terraform-policy"
206-
resource_group_name = azurerm_resource_group.rg.name
207-
iothub_name = azurerm_iothub.iothub.name
208-
209-
registry_read = true
210-
registry_write = true
211-
service_connect = true
212-
}
213-
214-
# Create IoT Hub DPS
215-
resource "random_pet" "dps_name" {
216-
prefix = var.dps_name_prefix
217-
length = 1
218-
}
219-
220-
resource "azurerm_iothub_dps" "dps" {
221-
name = random_pet.dps_name.id
222-
resource_group_name = azurerm_resource_group.rg.name
223-
location = azurerm_resource_group.rg.location
224-
allocation_policy = "Hashed"
225-
226-
sku {
227-
name = "S1"
228-
capacity = "1"
229-
}
230-
231-
linked_hub {
232-
connection_string = azurerm_iothub_shared_access_policy.hubaccesspolicy.primary_connection_string
233-
location = azurerm_resource_group.rg.location
234-
allocation_weight = 150
235-
apply_allocation_policy = true
236-
}
237-
}
238-
```
54+
[!code-terraform[master](~/terraform_samples/quickstart/201-iot-hub-with-device-provisioning-service/main.tf)]
23955

24056
1. Create a file named `variables.tf` and insert the following code:
24157

242-
```terraform
243-
variable "resource_group_location" {
244-
default = "eastus"
245-
description = "Location of the resource group."
246-
}
247-
248-
variable "resource_group_name_prefix" {
249-
default = "rg"
250-
description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
251-
}
252-
253-
variable "storage_account_name_prefix" {
254-
default = "sa"
255-
description = "Prefix of the storage account name that's combined with a random ID so name is unique in your Azure subscription."
256-
}
257-
258-
variable "eventhub_namespace_name_prefix" {
259-
default = "namespace"
260-
description = "Prefix of the event hub namespace name that's combined with a random ID so name is unique in your Azure subscription."
261-
}
262-
263-
variable "iothub_name_prefix" {
264-
default = "iothub"
265-
description = "Prefix of the iot hub name that's combined with a random ID so name is unique in your Azure subscription."
266-
}
267-
268-
variable "dps_name_prefix" {
269-
default = "dps"
270-
description = "Prefix of the dps name that's combined with a random ID so name is unique in your Azure subscription."
271-
}
272-
```
58+
[!code-terraform[master](~/terraform_samples/quickstart/201-iot-hub-with-device-provisioning-service/variables.tf)]
27359

27460
1. Create a file named `outputs.tf` and insert the following code:
27561

276-
```terraform
277-
output "azurerm_iothub_name" {
278-
value = azurerm_iothub.iothub.name
279-
}
280-
281-
output "azurerm_iothub_dps_name" {
282-
value = azurerm_iothub_dps.dps.name
283-
}
284-
285-
output "resource_group_name" {
286-
value = azurerm_resource_group.rg.name
287-
}
288-
```
62+
[!code-terraform[master](~/terraform_samples/quickstart/201-iot-hub-with-device-provisioning-service/outputs.tf)]
28963

29064
## Initialize Terraform
29165

292-
Run [terraform init](https://www.terraform.io/cli/commands/init) to initialize the Terraform deployment. This command downloads the Azure modules required to manage your Azure resources.
293-
294-
```cmd
295-
terraform init
296-
```
66+
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
29767

29868
## Create a Terraform execution plan
29969

300-
Run [terraform plan](https://www.terraform.io/cli/commands/plan) to create an execution plan.
301-
302-
```cmd
303-
terraform plan -out main.tfplan
304-
```
305-
306-
The `terraform plan` command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
307-
308-
The optional `-out` parameter allows you to specify an output file for the plan. Using the `-out` parameter ensures that the plan you reviewed is exactly what is applied.
309-
310-
To read more about persisting execution plans and security, see the [security warning section](https://www.terraform.io/cli/commands/plan#security-warning).
70+
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
31171

31272
## Apply a Terraform execution plan
31373

314-
Run [terraform apply](https://www.terraform.io/cli/commands/apply) to apply the execution plan to your cloud infrastructure.
315-
316-
```cmd
317-
terraform apply main.tfplan
318-
```
319-
320-
The `terraform apply` command above assumes you previously ran `terraform plan -out main.tfplan`.
321-
322-
If you specified a different filename for the `-out` parameter, use that same filename in the call to `terraform apply`. If you didn't use the `-out` parameter, call `terraform apply` without any parameters.
74+
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
32375

32476
## Verify the results
32577

@@ -345,21 +97,7 @@ The names of the resource group and the DPS instance are displayed in the terraf
34597

34698
## Clean up resources
34799

348-
Other articles build upon the resources that you created in this quickstart. If you plan to continue on to subsequent quickstart or to the tutorials, keep the resources created in this quickstart.
349-
350-
If you want to delete the resources created in this quickstart, use the following commands:
351-
352-
1. Run [terraform plan](https://www.terraform.io/cli/commands/plan) and specify the `destroy` flag.
353-
354-
```cmd
355-
terraform plan -destroy -out main.destroy.tfplan
356-
```
357-
358-
2. Run [terraform apply](https://www.terraform.io/cli/commands/apply) to apply the execution plan.
359-
360-
```cmd
361-
terraform apply main.destroy.tfplan
362-
```
100+
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
363101

364102
## Troubleshoot Terraform on Azure
365103

0 commit comments

Comments
 (0)