Skip to content

Commit

Permalink
add terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed May 4, 2024
1 parent 86956d9 commit bc5bb36
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,9 @@ MigrationBackup/
# JetBrains Rider
.idea/
*.sln.iml

# Terraform
.terraform/
.terraform.lock.hcl
terraform.tfstate
terraform.tfstate.backup
24 changes: 24 additions & 0 deletions deployments/Azure/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
```
terraform version
```
```
terraform -help
```
```
terraform fmt
```
```
terraform validate
```
```
terraform init
```
```
terraform plan
```
```
terraform apply
```
```
terraform destroy
```
7 changes: 7 additions & 0 deletions deployments/Azure/terraform/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
common_tags = {
Environment = "Development"
Project = "ClassifiedAds"
Department = "SD"
}
}
133 changes: 133 additions & 0 deletions deployments/Azure/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {
name = "ClassifiedAds_DEV"
location = "Southeast Asia"

tags = local.common_tags
}

resource "azurerm_storage_account" "sa" {
name = var.storageAccountName
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"

tags = local.common_tags
}

resource "azurerm_mssql_server" "sqlserver" {
name = var.sqlServerName
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
version = "12.0"
administrator_login = var.sqlServerUserName
administrator_login_password = var.sqlServerPassword

tags = local.common_tags
}

resource "azurerm_mssql_firewall_rule" "FirewallRule1" {
name = "Allow Azure services and resources to access this server"
server_id = azurerm_mssql_server.sqlserver.id
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
}

resource "azurerm_mssql_firewall_rule" "FirewallRule2" {
name = "Your IP Address"
server_id = azurerm_mssql_server.sqlserver.id
start_ip_address = "171.243.49.233"
end_ip_address = "171.243.49.233"
}

resource "azurerm_mssql_database" "sqlDb" {
name = "classifiedadsdevdb"
server_id = azurerm_mssql_server.sqlserver.id
sku_name = "Basic"

tags = local.common_tags
}

data "azurerm_client_config" "current" {}

resource "azurerm_key_vault" "keyVault" {
name = "classifiedadsdev"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
access_policy {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
key_permissions = ["Get", "List", "Create", "Delete", "Update", "Recover", "Purge", "GetRotationPolicy"]
}

tags = local.common_tags
}

resource "azurerm_service_plan" "appserviceplan" {
name = "ClassifiedAds-Hosts"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
os_type = "Windows"
sku_name = "B1"
tags = local.common_tags
}

resource "azurerm_windows_web_app" "identityserver_classifiedads" {
name = "identityserver-classifiedads"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
service_plan_id = azurerm_service_plan.appserviceplan.id
site_config {
application_stack {
current_stack = "dotnet"
dotnet_version = "v8.0"
}
}
app_settings = {
"ConnectionStrings__ClassifiedAds" = "Server=tcp:${var.sqlServerName}.database.windows.net,1433;Initial Catalog=${azurerm_mssql_database.sqlDb.name};Persist Security Info=False;User ID=${var.sqlServerUserName};Password=${var.sqlServerPassword};MultipleActiveResultSets=False;Encrypt=true;TrustServerCertificate=False;Connection Timeout=30;"
}

tags = local.common_tags
}

resource "azurerm_windows_web_app" "webapi_classifiedads" {
name = "webapi-classifiedads"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
service_plan_id = azurerm_service_plan.appserviceplan.id
site_config {
application_stack {
current_stack = "dotnet"
dotnet_version = "v8.0"
}
}
app_settings = {
"ConnectionStrings__ClassifiedAds" = "Server=tcp:${var.sqlServerName}.database.windows.net,1433;Initial Catalog=${azurerm_mssql_database.sqlDb.name};Persist Security Info=False;User ID=${var.sqlServerUserName};Password=${var.sqlServerPassword};MultipleActiveResultSets=False;Encrypt=true;TrustServerCertificate=False;Connection Timeout=30;"
}

tags = local.common_tags
}

resource "azurerm_windows_web_app" "webmvc_classifiedads" {
name = "webmvc-classifiedads"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
service_plan_id = azurerm_service_plan.appserviceplan.id
site_config {
application_stack {
current_stack = "dotnet"
dotnet_version = "v8.0"
}
}
app_settings = {
"ConnectionStrings__ClassifiedAds" = "Server=tcp:${var.sqlServerName}.database.windows.net,1433;Initial Catalog=${azurerm_mssql_database.sqlDb.name};Persist Security Info=False;User ID=${var.sqlServerUserName};Password=${var.sqlServerPassword};MultipleActiveResultSets=False;Encrypt=true;TrustServerCertificate=False;Connection Timeout=30;"
}

tags = local.common_tags
}
9 changes: 9 additions & 0 deletions deployments/Azure/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "primary_access_key" {
value = azurerm_storage_account.sa.primary_access_key
sensitive = true
description = "The primary access key for the storage account."
}

output "sql_database_connection_string" {
value = "Server=tcp:${var.sqlServerName}.database.windows.net,1433;Initial Catalog=${azurerm_mssql_database.sqlDb.name};Persist Security Info=False;User ID=${var.sqlServerUserName};Password=${var.sqlServerPassword};MultipleActiveResultSets=False;Encrypt=true;TrustServerCertificate=False;Connection Timeout=30;"
}
Empty file.
23 changes: 23 additions & 0 deletions deployments/Azure/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "storageAccountName" {
type = string
description = "Storage Account Name"
default = "classifiedadsdev"
}

variable "sqlServerName" {
type = string
description = "Sql Server Name"
default = "classifiedadsdev"
}

variable "sqlServerUserName" {
type = string
description = "Sql Server UserName "
default = "classifiedads"
}

variable "sqlServerPassword" {
type = string
description = "Sql Server Password"
default = "sqladmin123!@#"
}

0 comments on commit bc5bb36

Please sign in to comment.