Skip to content

Commit 196e5f3

Browse files
authored
Merge 5a09ebb into 032cd3e
2 parents 032cd3e + 5a09ebb commit 196e5f3

File tree

8 files changed

+464
-1
lines changed

8 files changed

+464
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ crash.*.log
1313
# password, private keys, and other secrets. These should not be part of version
1414
# control as they are data points which are potentially sensitive and subject
1515
# to change depending on the environment.
16-
*.tfvars
1716
*.tfvars.json
1817

1918
# Ignore override files as they are usually used to override resources locally and so

terraform-infrastructure/.terraform.lock.hcl

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform-infrastructure/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Azure Infrastructure Terraform Templates
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-05-19
9+
10+
----------
11+
12+
<div align="center">
13+
<img src="" alt="Centered Image" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
14+
</div>
15+
16+
## Overview
17+
18+
Templates structure:
19+
20+
```
21+
.
22+
├── README.md
23+
├────── main.tf
24+
├────── variables.tf
25+
├────── provider.tf
26+
├────── terraform.tfvars
27+
├────── outputs.tf
28+
```
29+
30+
- main.tf `(Main Terraform configuration file)`: This file contains the core infrastructure code. It defines the resources you want to create, such as virtual machines, networks, and storage. It's the primary file where you describe your infrastructure in a declarative manner.
31+
- 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.
32+
- provider.tf `(Provider configurations)`: Providers are plugins that Terraform uses to interact with cloud providers, SaaS providers, and other APIs. This file specifies which providers (e.g., AWS, Azure, Google Cloud) you are using and any necessary configuration for them, such as authentication details.
33+
- 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.
34+
- 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 IP addresses, resource IDs, and other important details. They can also be used as inputs for other Terraform configurations or scripts.
35+
36+
## How to execute it
37+
38+
```mermaid
39+
graph TD;
40+
A[az login] --> B(terraform init)
41+
B --> C{Terraform provisioning stage}
42+
C -->|Review| D[terraform plan]
43+
C -->|Order Now| E[terraform apply]
44+
C -->|Delete Resource if needed| F[terraform destroy]
45+
```
46+
47+
> [!IMPORTANT]
48+
> Please modify `terraform.tfvars` with your information, then run the following flow. If you need more visual guidance, please check the video that illustrates the provisioning steps.
49+
50+
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.
51+
52+
> Go to the path where Terraform files are located:
53+
54+
```sh
55+
cd terraform-infrastructure
56+
```
57+
58+
```sh
59+
az login
60+
```
61+
62+
<img width="550" alt="img" src="https://github.com/user-attachments/assets/53b47aa7-134e-4cf7-b0b8-cdebdd0583ed" />
63+
64+
<img width="550" alt="img" src="https://github.com/user-attachments/assets/1d9a247d-3dc9-472f-9305-4e4f0ecb72f1" />
65+
66+
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.
67+
68+
``` sh
69+
terraform init
70+
```
71+
72+
<img width="550" alt="img" src="https://github.com/user-attachments/assets/a7a32891-ad72-423a-a1fe-bdb50925b546" />
73+
74+
3. **Terraform Provisioning Stage**:
75+
76+
- **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`.
77+
78+
```sh
79+
terraform plan -var-file terraform.tfvars
80+
```
81+
82+
> At the end, you will see a message in green if everything was executed successfully:
83+
84+
<img width="550" alt="Screenshot 2025-03-18 145143" src="https://github.com/user-attachments/assets/4741e863-1ccd-4f2a-a0b8-d5d1964bd890" />
85+
86+
- **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`.
87+
88+
```sh
89+
terraform apply -var-file terraform.tfvars
90+
```
91+
92+
> At the end, you will see a message in green if everything was executed successfully:
93+
94+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/2b32b63f-3e9f-46da-a5e9-c39360135251">
95+
96+
- **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`.
97+
98+
```sh
99+
terraform destroy -var-file terraform.tfvars
100+
```
101+
102+
> At the end, you will see a message in green if everything was executed successfully:
103+
104+
<img width="550" alt="image" src="https://github.com/user-attachments/assets/f2089d03-3a3d-431d-b462-8148ef519104">
105+
106+
<div align="center">
107+
<h3 style="color: #4CAF50;">Total Visitors</h3>
108+
<img src="https://profile-counter.glitch.me/brown9804/count.svg" alt="Visitor Count" style="border: 2px solid #4CAF50; border-radius: 5px; padding: 5px;"/>
109+
</div>

terraform-infrastructure/main.tf

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
# Storage Account
12+
resource "azurerm_storage_account" "storage" {
13+
name = var.storage_account_name
14+
resource_group_name = azurerm_resource_group.rg.name
15+
location = azurerm_resource_group.rg.location
16+
account_tier = "Standard"
17+
account_replication_type = "LRS"
18+
19+
depends_on = [azurerm_resource_group.rg]
20+
21+
# Output the storage account name
22+
provisioner "local-exec" {
23+
command = "echo Storage Account: ${self.name}"
24+
}
25+
}
26+
27+
# Blob Container for Input Files
28+
resource "azurerm_storage_container" "input_container" {
29+
name = "input"
30+
storage_account_id = azurerm_storage_account.storage.id
31+
container_access_type = "private"
32+
33+
depends_on = [azurerm_storage_account.storage]
34+
35+
# Output the container name
36+
provisioner "local-exec" {
37+
command = "echo Input Container: ${self.name}"
38+
}
39+
}
40+
41+
# Blob Container for Output Files
42+
resource "azurerm_storage_container" "output_container" {
43+
name = "output"
44+
storage_account_id = azurerm_storage_account.storage.id
45+
container_access_type = "private"
46+
47+
depends_on = [azurerm_storage_account.storage]
48+
49+
# Output the container name
50+
provisioner "local-exec" {
51+
command = "echo Output Container: ${self.name}"
52+
}
53+
}
54+
55+
# Linux Function App
56+
resource "azurerm_linux_function_app" "function_app" {
57+
name = var.function_app_name
58+
location = azurerm_resource_group.rg.location
59+
resource_group_name = azurerm_resource_group.rg.name
60+
service_plan_id = azurerm_service_plan.asp.id
61+
storage_account_name = azurerm_storage_account.storage.name
62+
storage_account_access_key = azurerm_storage_account.storage.primary_access_key
63+
64+
site_config {
65+
# Other configurations can go here
66+
}
67+
68+
depends_on = [azurerm_service_plan.asp]
69+
70+
provisioner "local-exec" {
71+
command = "echo Function App: ${self.name}"
72+
}
73+
}
74+
75+
76+
# Service Plan
77+
resource "azurerm_service_plan" "asp" {
78+
name = var.app_service_plan_name
79+
location = azurerm_resource_group.rg.location
80+
resource_group_name = azurerm_resource_group.rg.name
81+
os_type = "Linux"
82+
sku_name = "Y1" # Consumption plan
83+
84+
depends_on = [azurerm_resource_group.rg]
85+
86+
# Output the service plan name
87+
provisioner "local-exec" {
88+
command = "echo Service Plan: ${self.name}"
89+
}
90+
}
91+
92+
# Application Insights
93+
resource "azurerm_application_insights" "appinsights" {
94+
name = var.app_insights_name
95+
location = azurerm_resource_group.rg.location
96+
resource_group_name = azurerm_resource_group.rg.name
97+
application_type = "web"
98+
workspace_id = azurerm_log_analytics_workspace.loganalytics.id
99+
100+
depends_on = [azurerm_resource_group.rg]
101+
102+
provisioner "local-exec" {
103+
command = "echo Application Insights: ${self.name}"
104+
}
105+
}
106+
107+
# Log Analytics Workspace
108+
resource "azurerm_log_analytics_workspace" "loganalytics" {
109+
name = var.log_analytics_workspace_name
110+
location = azurerm_resource_group.rg.location
111+
resource_group_name = azurerm_resource_group.rg.name
112+
sku = "PerGB2018"
113+
114+
depends_on = [azurerm_resource_group.rg]
115+
116+
# Output the log analytics workspace name
117+
provisioner "local-exec" {
118+
command = "echo Log Analytics Workspace: ${self.name}"
119+
}
120+
}
121+
122+
# Key Vault
123+
resource "azurerm_key_vault" "keyvault" {
124+
name = var.key_vault_name
125+
location = azurerm_resource_group.rg.location
126+
resource_group_name = azurerm_resource_group.rg.name
127+
tenant_id = data.azurerm_client_config.current.tenant_id
128+
sku_name = "standard"
129+
130+
depends_on = [azurerm_resource_group.rg]
131+
132+
# Output the key vault name
133+
provisioner "local-exec" {
134+
command = "echo Key Vault: ${self.name}"
135+
}
136+
}
137+
138+
# Data source to get tenant ID
139+
data "azurerm_client_config" "current" {}
140+
141+
# CosmosDB
142+
resource "azurerm_cosmosdb_account" "cosmosdb" {
143+
name = var.cosmosdb_account_name
144+
location = azurerm_resource_group.rg.location
145+
resource_group_name = azurerm_resource_group.rg.name
146+
offer_type = "Standard"
147+
kind = "GlobalDocumentDB"
148+
consistency_policy {
149+
consistency_level = "Session"
150+
}
151+
152+
geo_location {
153+
location = azurerm_resource_group.rg.location
154+
failover_priority = 0
155+
}
156+
157+
depends_on = [azurerm_resource_group.rg]
158+
}
159+
160+
# Azure Form Recognizer (Document Intelligence)
161+
resource "azurerm_cognitive_account" "form_recognizer" {
162+
name = var.form_recognizer_name
163+
location = azurerm_resource_group.rg.location
164+
resource_group_name = azurerm_resource_group.rg.name
165+
kind = "FormRecognizer"
166+
sku_name = "S0"
167+
168+
depends_on = [azurerm_resource_group.rg]
169+
170+
# Output the Form Recognizer name
171+
provisioner "local-exec" {
172+
command = "echo Form Recognizer: ${self.name}"
173+
}
174+
}

terraform-infrastructure/output.tf

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
output "resource_group_name" {
2+
description = "The name of the resource group."
3+
value = azurerm_resource_group.rg.name
4+
}
5+
6+
output "storage_account_name" {
7+
description = "The name of the storage account"
8+
value = azurerm_storage_account.storage.name
9+
}
10+
11+
output "input_container_name" {
12+
description = "The name of the input container"
13+
value = azurerm_storage_container.input_container.name
14+
}
15+
16+
output "output_container_name" {
17+
description = "The name of the output container"
18+
value = azurerm_storage_container.output_container.name
19+
}
20+
21+
output "function_app_name" {
22+
description = "The name of the Linux Function App."
23+
value = azurerm_linux_function_app.function_app.name
24+
}
25+
26+
output "app_service_plan_name" {
27+
description = "The name of the Service Plan"
28+
value = azurerm_service_plan.asp.name
29+
}
30+
31+
output "app_insights_name" {
32+
description = "The name of the Application Insights instance"
33+
value = azurerm_application_insights.appinsights.name
34+
}
35+
36+
output "log_analytics_workspace_name" {
37+
description = "The name of the Log Analytics workspace"
38+
value = azurerm_log_analytics_workspace.loganalytics.name
39+
}
40+
41+
output "key_vault_name" {
42+
description = "The name of the Key Vault"
43+
value = azurerm_key_vault.keyvault.name
44+
}
45+
46+
47+
output "cosmosdb_account_name" {
48+
description = "The name of the CosmosDB account."
49+
value = azurerm_cosmosdb_account.cosmosdb.name
50+
}
51+
52+
# Output the Form Recognizer name
53+
output "form_recognizer_name" {
54+
value = azurerm_cognitive_account.form_recognizer.name
55+
}
56+
57+
# Output the Form Recognizer endpoint
58+
output "form_recognizer_endpoint" {
59+
value = azurerm_cognitive_account.form_recognizer.endpoint
60+
}

0 commit comments

Comments
 (0)