Skip to content

Commit 9a3cf77

Browse files
Added module - Host a Static Website on Azure Storage
1 parent 28be65d commit 9a3cf77

File tree

9 files changed

+203
-2
lines changed

9 files changed

+203
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ override.tf.json
3535
# example: *tfplan*
3636

3737
*.tfplan*
38+
39+
.DS_Store
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Static Website on Azure</title>
4+
</head>
5+
<body>
6+
<center><h1> Oopsie. Something is wrong </h1><center>
7+
<center><img src="https://www.verisign.com/en_GB/resources/img/crisis-site-goes-down.jpg"</center>
8+
</body>
9+
</html>

beginners/azure/storageAccount/README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Create a Storage account in Azure with Terraform
1+
# Create a Static Website on Azure Storage account with Terraform
22

3-
**This module creates a Storage account in Azure**
3+
**This module creates a Storage account in Azure and hosts a static website**
44

55
> Note 1: This deployment is not free. If you are not on a free trail, it will incur a very small fee. So, its always a good practice to cleanup everything when you are done with the demo.
66
@@ -19,6 +19,14 @@ Click [Here](https://github.com/collabnix/terraform/blob/master/beginners/azure/
1919

2020
> Make sure you are in this directory. This is the directory from where we run terraform commands.
2121
22+
After the storage account is created, copy the url from the output, paste it in a browser. You should see the below page
23+
24+
![success](https://github.com/collabnix/terraform/beginners/azure/storageAccount/images/success.png)
25+
26+
If you want to see the error page, append /error to the url.
27+
28+
![error](https://github.com/collabnix/terraform/beginners/azure/storageAccount/images/error.png)
29+
2230
## After the deployment
2331

2432
- Cleanup everything with **$ terraform destroy -auto-approve**
95.5 KB
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Static Website on Azure</title>
4+
</head>
5+
<body>
6+
<center><h1> This website is running on Azure Storage </h1><center>
7+
<center><img src="http://www.chiangraitimes.com/wp-content/uploads/2015/08/Well-Done.jpg"</center>
8+
</body>
9+
</html>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
2+
#* Storage account with Network Rules - Outputs *#
3+
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
4+
5+
output "storage_account_name" {
6+
description = "Name of the storage account"
7+
value = azurerm_storage_account.sa.name
8+
}
9+
10+
output "website-url" {
11+
description = "URL of the static website"
12+
value = azurerm_storage_account.sa.primary_web_endpoint
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
2+
#* Storage account with Network Rules *#
3+
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
4+
5+
#
6+
# - Provider Block
7+
#
8+
9+
provider "azurerm" {
10+
version = ">=2.6"
11+
client_id = var.client_id
12+
client_secret = var.client_secret
13+
subscription_id = var.subscription_id
14+
tenant_id = var.tenant_id
15+
16+
features {}
17+
}
18+
19+
#
20+
# - Create a Resource Group
21+
#
22+
23+
resource "azurerm_resource_group" "rg" {
24+
name = "${var.prefix}-rg"
25+
location = var.location
26+
tags = var.tags
27+
}
28+
29+
#
30+
# - Create a Random integer to append to Storage account name
31+
#
32+
33+
resource "random_integer" "sa_name" {
34+
min = 1111
35+
max = 9999
36+
# Result will be like this - 1325
37+
}
38+
39+
#
40+
# - Create a Storage account with Network Rules
41+
#
42+
43+
resource "azurerm_storage_account" "sa" {
44+
name = "${var.saVars["name"]}${random_integer.sa_name.result}"
45+
resource_group_name = azurerm_resource_group.rg.name
46+
location = azurerm_resource_group.rg.location
47+
account_kind = var.saVars["account_kind"]
48+
account_tier = var.saVars["account_tier"]
49+
access_tier = var.saVars["access_tier"]
50+
account_replication_type = var.saVars["account_replication_type"]
51+
52+
static_website {
53+
index_document = "index.html"
54+
error_404_document = "404.html"
55+
}
56+
57+
tags = var.tags
58+
}
59+
60+
61+
resource "azurerm_storage_blob" "website" {
62+
for_each = var.blobs
63+
name = each.key
64+
storage_account_name = azurerm_storage_account.sa.name
65+
storage_container_name = "$web"
66+
type = "Block"
67+
content_type = "text/html"
68+
source = each.value
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
2+
#* Storage account with Network Rules - Variables *#
3+
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
4+
5+
# Service Principal Variables
6+
7+
variable "client_id" {
8+
description = "Client ID (APP ID) of the application"
9+
type = string
10+
}
11+
12+
variable "client_secret" {
13+
description = "Client Secret (Password) of the application"
14+
type = string
15+
}
16+
17+
variable "subscription_id" {
18+
description = "Subscription ID"
19+
type = string
20+
}
21+
22+
variable "tenant_id" {
23+
description = "Tenant ID"
24+
type = string
25+
}
26+
27+
# Prefix and Tags
28+
29+
variable "prefix" {
30+
description = "Prefix to append to all resource names"
31+
type = string
32+
default = "collabnix"
33+
}
34+
35+
variable "tags" {
36+
description = "Resouce tags"
37+
type = map(string)
38+
default = {
39+
"project" = "Collabnix"
40+
"deployed_with" = "Terraform"
41+
}
42+
}
43+
44+
# Resource Group
45+
46+
variable "location" {
47+
description = "Location of the resource group"
48+
type = string
49+
default = "East US"
50+
}
51+
52+
# Vnet and Subnet
53+
54+
variable "vnet_address_range" {
55+
description = "IP Range of the virtual network"
56+
type = string
57+
default = "10.0.0.0/16"
58+
}
59+
60+
variable "subnet_address_range" {
61+
description = "IP Range of the virtual network"
62+
type = string
63+
default = "10.0.1.0/24"
64+
}
65+
66+
# Storage account
67+
68+
variable "saVars" {
69+
description = "Variables for Storage account"
70+
type = map(string)
71+
default = {
72+
"name" = "collabnixsa"
73+
"account_kind" = "StorageV2"
74+
"account_tier" = "Standard"
75+
"access_tier" = "Hot"
76+
"account_replication_type" = "LRS"
77+
"default_action" = "Deny"
78+
"ip_rules" = "124.123.72.15"
79+
"bypass" = "None"
80+
}
81+
}
82+
83+
variable "blobs" {
84+
description = "Files to upload to the container"
85+
type = map(string)
86+
default = {
87+
"index.html" = "./index.html"
88+
"404.html" = "./404.html"
89+
}
90+
}
91+

0 commit comments

Comments
 (0)