Skip to content

Commit e2801fa

Browse files
authored
Merge pull request #1 from MicrosoftCloudEssentials-LearningHub/terraform-infra-required
terraform infra - source control
2 parents eb044ac + 4017879 commit e2801fa

10 files changed

Lines changed: 467 additions & 9 deletions

File tree

.gitignore

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Local .terraform directories
22
**/.terraform/*
3-
3+
**/.terraform
4+
*lock.hcl
45
# .tfstate files
56
*.tfstate
67
*.tfstate.*
@@ -9,12 +10,12 @@
910
crash.log
1011
crash.*.log
1112

12-
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
13-
# password, private keys, and other secrets. These should not be part of version
14-
# control as they are data points which are potentially sensitive and subject
15-
# to change depending on the environment.
16-
*.tfvars
17-
*.tfvars.json
13+
# # Exclude all .tfvars files, which are likely to contain sensitive data, such as
14+
# # password, private keys, and other secrets. These should not be part of version
15+
# # control as they are data points which are potentially sensitive and subject
16+
# # to change depending on the environment.
17+
# *.tfvars
18+
# *.tfvars.json
1819

1920
# Ignore override files as they are usually used to override resources locally and so
2021
# are not checked in

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File Upload Summary Tool - Accelerator
1+
# Azure File Upload and Summary Generator - Accelerator
22

33
Costa Rica
44

@@ -9,7 +9,7 @@ Last updated: 2025-02-04
99

1010
----------
1111

12-
> Provides an example of creating an automated file processing and summary generation tool using Microsoft Azure technologies. It demonstrates how to utilize Azure Blob Storage, Azure Functions, and Python to upload files, search for key values, and generate summaries. The tool supports tabular data in xlsx and csv formats, as well as text data from docs and pdf files. Summaries can be generated in few formats, including xlsx, csv, and text files.
12+
> Provides an example of creating an automated file processing and summary generation tool using Microsoft Azure technologies. It demonstrates how to use Azure AI Services, Azure Blob Storage, Azure Functions, and Python to upload files, search for key values, and generate summaries. The tool supports tabular data in xlsx and csv formats, as well as text data from docs and pdf files. Summaries can be generated in few formats, including xlsx, csv, and text files.
1313
1414
> [!IMPORTANT]
1515
> Disclaimer: Please note, this example of solution `it is not an official solution guide`. For official guidance, support, or more detailed information, please refer to Microsoft's official documentation or contact Microsoft directly: [Microsoft Sales and Support](https://support.microsoft.com/contactus?ContactUsExperienceEntryPointAssetId=S.HP.SMC-HOME)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<mxfile host="Electron" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/26.0.9 Chrome/128.0.6613.186 Electron/32.2.5 Safari/537.36" version="26.0.9">
2+
<diagram name="general-deployment" id="VzGhtupkYtY4OB6_fRJi">
3+
<mxGraphModel dx="1884" dy="477" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
4+
<root>
5+
<mxCell id="0" />
6+
<mxCell id="1" parent="0" />
7+
<mxCell id="UkH3Tmcc9s9qquvXN7uM-32" value="" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
8+
<mxGeometry x="-720" y="170" width="590" height="340" as="geometry" />
9+
</mxCell>
10+
<mxCell id="UkH3Tmcc9s9qquvXN7uM-30" value="Subscription" style="image;sketch=0;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/mscae/Key.svg;" parent="1" vertex="1">
11+
<mxGeometry x="-734.5" y="153.44" width="31" height="50" as="geometry" />
12+
</mxCell>
13+
<mxCell id="UkH3Tmcc9s9qquvXN7uM-31" value="Resource Group" style="image;sketch=0;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/mscae/ResourceGroup.svg;" parent="1" vertex="1">
14+
<mxGeometry x="-738.5" y="488" width="50" height="40" as="geometry" />
15+
</mxCell>
16+
<mxCell id="Vv6FFmmm0vw7hVMtTU7b-1" value="Blob Storage" style="image;sketch=0;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/mscae/BlobBlock.svg;" parent="1" vertex="1">
17+
<mxGeometry x="-640" y="213.91999999999996" width="58" height="48.72" as="geometry" />
18+
</mxCell>
19+
<mxCell id="Vv6FFmmm0vw7hVMtTU7b-8" value="Function App" style="image;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/azure2/compute/Function_Apps.svg;" parent="1" vertex="1">
20+
<mxGeometry x="-510" y="203.44" width="68" height="60" as="geometry" />
21+
</mxCell>
22+
<mxCell id="Vv6FFmmm0vw7hVMtTU7b-12" value="Application&amp;nbsp;&lt;br&gt;Insights" style="image;sketch=0;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/mscae/Application_Insights.svg;" parent="1" vertex="1">
23+
<mxGeometry x="-360" y="203.44" width="32" height="50" as="geometry" />
24+
</mxCell>
25+
<mxCell id="Vv6FFmmm0vw7hVMtTU7b-13" value="Log Analytics Workspace" style="image;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/azure2/management_governance/Log_Analytics_Workspaces.svg;" parent="1" vertex="1">
26+
<mxGeometry x="-240" y="198.64000000000001" width="64" height="64" as="geometry" />
27+
</mxCell>
28+
<mxCell id="3NK07bUQuaUZu8PJls8a-1" value="AI Services" style="image;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/azure2/ai_machine_learning/Cognitive_Services.svg;" vertex="1" parent="1">
29+
<mxGeometry x="-650" y="340" width="68" height="48" as="geometry" />
30+
</mxCell>
31+
<mxCell id="3NK07bUQuaUZu8PJls8a-2" value="Key Vault" style="image;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/azure2/security/Key_Vaults.svg;" vertex="1" parent="1">
32+
<mxGeometry x="-510" y="330" width="68" height="68" as="geometry" />
33+
</mxCell>
34+
</root>
35+
</mxGraphModel>
36+
</diagram>
37+
</mxfile>

terraform/src/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Terraform deployment for Azure resources required for this use case
2+
3+
Costa Rica
4+
5+
[![GitHub](https://img.shields.io/badge/--181717?logo=github&logoColor=ffffff)](https://github.com/)
6+
[brown9804](https://github.com/brown9804)
7+
8+
Last updated: 2025-02-04
9+
10+
------------------------------------------
11+
12+
> Terraform templates to deploy an Azure-based automated file processing and summary generation tool. The solution includes:
13+
14+
<div align="center">
15+
<img src="https://github.com/user-attachments/assets/51809c83-47b5-44e0-b170-b7f22cd15975" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px; width: 700px;"/>
16+
</div>
17+
18+
<details>
19+
<summary><b>Table of Content </b> (Click to expand)</summary>
20+
21+
- [Overview](#overview)
22+
- [Resources](#resources)
23+
- [How to execute it](#how-to-execute-it)
24+
25+
</details>
26+
27+
28+
## Overview
29+
30+
```
31+
.
32+
├── README.md
33+
├── src
34+
├────── main.tf
35+
├────── variables.tf
36+
├────── provider.tf
37+
├────── terraform.tfvars
38+
├────── remote-storage.tf
39+
├────── outputs.tf
40+
```
41+
42+
- **main.tf** `(Main Terraform configuration file)`: This file contains the core infrastructure code. It defines the resources you want to create, such as Azure Blob Storage, Azure Functions, and other necessary services. It's the primary file where you describe your infrastructure in a declarative manner.
43+
- **variables.tf** `(Variable definitions)`: This file is used to define variables that can be used throughout your Terraform configuration. By using variables, you can make your configuration more flexible and reusable. For example, you can define variables for resource names, sizes, and other parameters that might change between environments.
44+
- **provider.tf** `(Provider configurations)`: Providers are plugins that Terraform uses to interact with cloud providers, SaaS providers, and other APIs. This file specifies the Azure provider and any necessary configuration for it, such as authentication details.
45+
- **terraform.tfvars** `(Variable values)`: This file contains the actual values for the variables defined in `variables.tf`. By separating variable definitions and values, you can easily switch between different sets of values for different environments (e.g., development, staging, production) without changing the main configuration files.
46+
- **remote-storage.tf** `(Remote state storage configuration)`: Terraform uses a state file to keep track of the resources it manages. This file configures remote state storage, which allows you to store the state file in a remote location (e.g., Azure Blob Storage). Remote state storage is crucial for collaboration and ensuring that the state file is not lost or corrupted.
47+
- **outputs.tf** `(Output values)`: This file defines the output values that Terraform should return after applying the configuration. Outputs are useful for displaying information about the resources created, such as resource IDs, connection strings, and other important details. They can also be used as inputs for other Terraform configurations or scripts.
48+
49+
## Resources
50+
51+
| Resource | Description |
52+
|---------------------------|-------------------------------------------------------------------------------------------------------|
53+
| Azure Resource Group | A logical container that organizes and manages all the related Azure resources. |
54+
| Azure Blob Storage | Stores input files (xlsx, csv, docs, pdfs) and output summary files. |
55+
| Azure Functions | Executes serverless compute tasks to process the uploaded files and generate summaries. |
56+
| Azure AI Services | Provides text analytics and OCR capabilities to handle text data from docs and pdfs. |
57+
| Azure Storage Account | Manages Blob Storage and other storage services. |
58+
| Azure Key Vault | Stores and manages sensitive information such as connection strings and API keys. |
59+
| Application Insights | Monitors the performance and usage of the file processing tool. |
60+
| Log Analytics Workspace | Collects and analyzes log data from various sources. |
61+
62+
## How to execute it
63+
64+
```mermaid
65+
graph TD;
66+
A[az login] --> B(terraform init)
67+
B --> C{Terraform provisioning stage}
68+
C -->|Review| D[terraform plan]
69+
C -->|Order Now| E[terraform apply]
70+
C -->|Delete Resource if needed| F[terraform destroy]
71+
```
72+
73+
> [!IMPORTANT]
74+
> Please modify `terraform.tfvars` with your information. Then run the following flow:
75+
76+
1. **Login to Azure**: This command logs you into your Azure account. It opens a browser window where you can enter your Azure credentials. Once logged in, you can manage your Azure resources from the command line.
77+
78+
```sh
79+
cd ./terraform/src/
80+
```
81+
82+
```sh
83+
az login
84+
```
85+
86+
<div align="center">
87+
<img src="https://github.com/user-attachments/assets/caed0c96-2a3e-4e1a-a788-b1b43b8d5939" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
88+
</div>
89+
90+
91+
2. **Initialize Terraform**: Initializes the working directory containing the Terraform configuration files. It downloads the necessary provider plugins and sets up the backend for storing the state.
92+
93+
``` sh
94+
terraform init
95+
```
96+
97+
<div align="center">
98+
<img src="https://github.com/user-attachments/assets/8b4c3960-b9f6-44dc-8325-f58ef15eb1dc" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
99+
</div>
100+
101+
102+
3. **Terraform Provisioning Stage**:
103+
104+
- **Review**: Creates an execution plan, showing what actions Terraform will take to achieve the desired state defined in your configuration files. It uses the variable values specified in `terraform.tfvars`.
105+
106+
```sh
107+
terraform plan -var-file terraform.tfvars
108+
```
109+
110+
<div align="center">
111+
<img src="https://github.com/user-attachments/assets/1cd5e195-9441-4a4e-ba7a-0ef046370834" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
112+
</div>
113+
114+
- **Order Now**: Applies the changes required to reach the desired state of the configuration. It prompts for confirmation before making any changes. It also uses the variable values specified in `terraform.tfvars`.
115+
116+
```sh
117+
terraform apply -var-file terraform.tfvars
118+
```
119+
120+
<div align="center">
121+
<img src="https://github.com/user-attachments/assets/81f79658-c35f-465a-98e8-55da2b1561fa" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
122+
</div>
123+
124+
<div align="center">
125+
<img src="https://github.com/user-attachments/assets/bef1b08c-46d8-464e-a87b-e0b93034cc54" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
126+
</div>
127+
128+
129+
- **Remove**: Destroys the infrastructure managed by Terraform. It prompts for confirmation before deleting any resources. It also uses the variable values specified in `terraform.tfvars`.
130+
131+
```sh
132+
terraform destroy -var-file terraform.tfvars
133+
```
134+
135+
<div align="center">
136+
<h3 style="color: #4CAF50;">Total Visitors</h3>
137+
<img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
138+
</div>
139+

terraform/src/main.tf

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Resource Group
2+
resource "azurerm_resource_group" "rg" {
3+
name = var.resource_group_name
4+
location = var.location
5+
6+
# Output the resource group name
7+
provisioner "local-exec" {
8+
command = "echo Resource Group: ${self.name}"
9+
}
10+
}
11+
12+
# Storage Account
13+
resource "azurerm_storage_account" "storage" {
14+
name = var.storage_account_name
15+
resource_group_name = azurerm_resource_group.rg.name
16+
location = azurerm_resource_group.rg.location
17+
account_tier = "Standard"
18+
account_replication_type = "LRS"
19+
20+
depends_on = [azurerm_resource_group.rg]
21+
22+
# Output the storage account name
23+
provisioner "local-exec" {
24+
command = "echo Storage Account: ${self.name}"
25+
}
26+
}
27+
28+
# Blob Container for Input Files
29+
resource "azurerm_storage_container" "input_container" {
30+
name = "input"
31+
storage_account_name = azurerm_storage_account.storage.name
32+
container_access_type = "private"
33+
34+
depends_on = [azurerm_storage_account.storage]
35+
36+
# Output the container name
37+
provisioner "local-exec" {
38+
command = "echo Input Container: ${self.name}"
39+
}
40+
}
41+
42+
# Blob Container for Output Files
43+
resource "azurerm_storage_container" "output_container" {
44+
name = "output"
45+
storage_account_name = azurerm_storage_account.storage.name
46+
container_access_type = "private"
47+
48+
depends_on = [azurerm_storage_account.storage]
49+
50+
# Output the container name
51+
provisioner "local-exec" {
52+
command = "echo Output Container: ${self.name}"
53+
}
54+
}
55+
56+
# Azure Function App
57+
resource "azurerm_function_app" "function_app" {
58+
name = var.function_app_name
59+
location = azurerm_resource_group.rg.location
60+
resource_group_name = azurerm_resource_group.rg.name
61+
storage_account_name = azurerm_storage_account.storage.name
62+
storage_account_access_key = azurerm_storage_account.storage.primary_access_key
63+
app_service_plan_id = azurerm_service_plan.asp.id
64+
os_type = "linux"
65+
66+
app_settings = {
67+
FUNCTIONS_WORKER_RUNTIME = "python"
68+
}
69+
70+
depends_on = [azurerm_service_plan.asp]
71+
72+
# Output the function app name
73+
provisioner "local-exec" {
74+
command = "echo Function App: ${self.name}"
75+
}
76+
}
77+
78+
# Service Plan
79+
resource "azurerm_service_plan" "asp" {
80+
name = var.app_service_plan_name
81+
location = azurerm_resource_group.rg.location
82+
resource_group_name = azurerm_resource_group.rg.name
83+
os_type = "Linux"
84+
sku_name = "Y1" # Consumption plan
85+
86+
depends_on = [azurerm_resource_group.rg]
87+
88+
# Output the service plan name
89+
provisioner "local-exec" {
90+
command = "echo Service Plan: ${self.name}"
91+
}
92+
}
93+
94+
# Application Insights
95+
resource "azurerm_application_insights" "appinsights" {
96+
name = var.app_insights_name
97+
location = azurerm_resource_group.rg.location
98+
resource_group_name = azurerm_resource_group.rg.name
99+
application_type = "web"
100+
101+
depends_on = [azurerm_resource_group.rg]
102+
103+
# Output the application insights name
104+
provisioner "local-exec" {
105+
command = "echo Application Insights: ${self.name}"
106+
}
107+
}
108+
109+
# Log Analytics Workspace
110+
resource "azurerm_log_analytics_workspace" "loganalytics" {
111+
name = var.log_analytics_workspace_name
112+
location = azurerm_resource_group.rg.location
113+
resource_group_name = azurerm_resource_group.rg.name
114+
sku = "PerGB2018"
115+
116+
depends_on = [azurerm_resource_group.rg]
117+
118+
# Output the log analytics workspace name
119+
provisioner "local-exec" {
120+
command = "echo Log Analytics Workspace: ${self.name}"
121+
}
122+
}
123+
124+
# Key Vault
125+
resource "azurerm_key_vault" "keyvault" {
126+
name = var.key_vault_name
127+
location = azurerm_resource_group.rg.location
128+
resource_group_name = azurerm_resource_group.rg.name
129+
tenant_id = data.azurerm_client_config.current.tenant_id
130+
sku_name = "standard"
131+
132+
depends_on = [azurerm_resource_group.rg]
133+
134+
# Output the key vault name
135+
provisioner "local-exec" {
136+
command = "echo Key Vault: ${self.name}"
137+
}
138+
}
139+
140+
# Data source to get tenant ID
141+
data "azurerm_client_config" "current" {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
backend "azurerm" {
3+
resource_group_name = "" # The name of the resource group for remote state storage
4+
storage_account_name = "" # The name of the storage account for remote state storage
5+
container_name = "tfstate" # The name of the container for remote state storage
6+
key = "terraform.tfstate" # The key for the remote state file
7+
}
8+
}

0 commit comments

Comments
 (0)