Skip to content

Commit b04bb5c

Browse files
authored
[1/5] Set up basic Azure sandbox environment, providers, etc. (#136)
1 parent 27a8a07 commit b04bb5c

30 files changed

+396
-0
lines changed

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,45 @@ scripts/python/env
5858
# Ignore autogenerated dummy tokens for local docker-compose cluster
5959
infra/recipes/docker-compose/oh-hadoop-spark/openhouse.token
6060
infra/recipes/docker-compose/oh-hadoop-spark/u_tableowner.token
61+
62+
# For Terraform:
63+
64+
# Local .terraform directories
65+
**/.terraform/*
66+
67+
# .tfstate files
68+
*.tfstate
69+
*.tfstate.*
70+
71+
# Crash log files
72+
crash.log
73+
crash.*.log
74+
75+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
76+
# password, private keys, and other secrets. These should not be part of version
77+
# control as they are data points which are potentially sensitive and subject
78+
# to change depending on the environment.
79+
*.tfvars
80+
*.tfvars.json
81+
82+
# Ignore override files as they are usually used to override resources locally and so
83+
# are not checked in
84+
override.tf
85+
override.tf.json
86+
*_override.tf
87+
*_override.tf.json
88+
89+
# Ignore transient lock info files created by terraform apply
90+
.terraform.tfstate.lock.info
91+
92+
# Include override files you do wish to add to version control using negated pattern
93+
# !example_override.tf
94+
95+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
96+
# example: *tfplan*
97+
98+
# Ignore CLI configuration files
99+
.terraformrc
100+
terraform.rc
101+
102+
.terraform.lock.hcl
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Project structure and description
2+
3+
This directory is intended to provide a customizable configuration for Microsoft Azure using Terraform.
4+
5+
## Directory structure
6+
7+
### Overall structure
8+
.
9+
├── environments # Various specific environment configurations for Azure
10+
├── sandbox # The sandbox environment
11+
├── modules # Reusable Azure components that are pre-defined
12+
├── k8s # The Azure kubernetes configurations and services
13+
├── mysql # The Azure MySQL configurations and services
14+
├── storage # The Azure storage configurations and services
15+
├── vm # The Azure virtual machine and network configurations and services
16+
├── k8s_variables.tf # Specific variables used for the kubernetes provider configuration
17+
├── provider.tf # The global azure and other providers needed for the configurations
18+
├── variables.tf # Global variables that can be reused across modules/environments
19+
├── versions.tf # The Azure Resource Manager version
20+
└── README.md
21+
22+
### Module/environment structure
23+
24+
Each module and environment contains a structure that is similar to the following (with occasional other specific files):
25+
26+
.
27+
├── main.tf # The main terraform file that defines all the necessary services
28+
├── variables.tf # The module/environment specific variables
29+
├── outputs.tf # The output variables from the module/environment
30+
└── common_variables.tf # A symlink from the global variables file
31+
32+
## Adding/editing modules/environments
33+
34+
To add a new module, create a new folder in the `/modules` directory with the same format as above. Define the services in `main.tf`, the local variables in `variables.tf`, and the outputs in `outputs.tf`. `main.tf` will be the primary place to edit/update modules.
35+
36+
To add a new environment, create a new folder in the `/environments` directory with the same format as above. For each environment add a `README` file as well as optional other files such as a `backend.tf` file. See the `environments/sandbox/` environment for examples. Instead of adding numerous new resources to the `main.tf` file in an environment, add modules that have been previously defined to maximize code reusability.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Initialize and Run Azure Sandbox
2+
Ensure you have a Azure account. As part of sandbox we use Terraform to provision a resource group, storage account, and blob container within that storage account.
3+
4+
## Creating an Azure Account
5+
6+
Create an Azure account [here](https://azure.microsoft.com/en-us/pricing/purchase-options/azure-account). If you haven't created one already, you can use the free trial to get free credits. Create a subscription using this free trial, or with another option if the free trial is over.
7+
8+
### Creating a Storage Account
9+
10+
A storage account must be created to hold the backend terraform files. Create a new storage account using an arbitrary resource group. It is typical to name these resources starting with `tfbackend`. Then, within this storage account, create a new blob container. Note the resource group name, storage account name, and blob container name. Also note the storage account key, which can be found in the "Access keys" sidebar option in the storage account. These variables will be used when configuring the backend.
11+
12+
## Azure Login
13+
14+
Install the [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/), and ensure that you are logged in with your Azure account. You can login in multiple ways.
15+
16+
### Using username/password
17+
18+
This is the simplest version. Run `az login` and login to your account on a web browser.
19+
20+
### Using Service Principal
21+
22+
Following the steps [here](https://learn.microsoft.com/en-us/cli/azure/authenticate-azure-cli-service-principal), you can run `az login --service-principal -u <app-id> -p <password-or-cert> --tenant <tenant>` to gain more control over your subscription and account.
23+
24+
## Terraform Backend
25+
26+
Terraform backend state can be stored locally or in Azure blob store. To configure this you will be setting the variables like those in `infra/recipes/terraform/azure/backend.tfvars.template`. These variables are the ones found when setting up the storage account.
27+
28+
### Using Environment Variables
29+
You could set the environment variables for variables to stored terraform backend state. For example, to initialize `storage_account_name` you can set the following environment variable.
30+
`export TF_VAR_storage_account_name="foobar"`
31+
32+
### Using tfvars
33+
1. Create a copy of the file backend.tfvars.template by running
34+
`cp backend.tfvars.template backend.tfvars`.
35+
2. Edit the backend.tfvars to configure the necessary TF backend state.
36+
37+
## Deployment
38+
39+
### Installation
40+
41+
Install the `terraform` CLI by following [this](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) link.
42+
43+
### Initialization
44+
45+
Run `terraform init` when setting up a new configuration, changing backend settings, or modifying provider versions. You don't need to run it for regular operations like `terraform plan` and `terraform apply` unless the configuration changes.
46+
47+
_If using tfvars_
48+
`terraform init -backend-config="./backend.tfvars"`
49+
_If using environment variables_
50+
`terraform init`
51+
52+
### Planning & Applying
53+
54+
`terraform plan` to see the plan generated by terraform for the specified deployment configuration. If you are satisfied with the plan, run `terraform apply` to apply the configuration.
55+
56+
## Deconfiguration
57+
58+
Run `terraform destroy` once you are done to tear down the services you have created.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
backend "azurerm" {}
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource_group_name = "<RESOURCE GROUP NAME HERE>"
2+
storage_account_name = "<ACCOUNT NAME HERE>"
3+
container_name = "<BLOB CONTAINER NAME HERE>"
4+
key = "<BLOB CONTAINER KEY HERE>"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../provider.tf
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../variables.tf
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
resource "azurerm_resource_group" "openhouse_sandbox" {
2+
name = var.resource_group_name
3+
location = var.resource_group_location
4+
}
5+
6+
module "vm" {
7+
source = "../../modules/vm"
8+
virtual_network_name = "openhouse-sandbox-network"
9+
resource_group_name = var.resource_group_name
10+
subnet_name = "openhouse-sandbox-subnet"
11+
}
12+
13+
module "mysql" {
14+
source = "../../modules/mysql"
15+
subnet_id = module.vm.subnet_id
16+
resource_group_name = azurerm_resource_group.openhouse_sandbox.name
17+
server_name = "openhouse-sandbox-mysql-server"
18+
db_admin_login = "azureadmin"
19+
db_admin_password = "Pa33word"
20+
db_name = "openhouse-sandbox-db"
21+
}
22+
23+
module "k8s" {
24+
source = "../../modules/k8s"
25+
k8s_cluster_name = "openhouse-sandbox-k8s"
26+
resource_group_name = azurerm_resource_group.openhouse_sandbox.name
27+
node_count = 1
28+
vm_size = "Standard_D2s_v3"
29+
}
30+
31+
module "storage" {
32+
source = "../../modules/storage"
33+
storage_account_name = "openhousestorage729387" // added random string of numbers to make it unique
34+
resource_group_name = azurerm_resource_group.openhouse_sandbox.name
35+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "resource_group_name" {
2+
value = azurerm_resource_group.openhouse_sandbox.name
3+
}

infra/recipes/terraform/azure/environments/sandbox/variables.tf

Whitespace-only changes.

0 commit comments

Comments
 (0)