Merge pull request #41 from Microsoft/feature-add-tf-prefix
Add Terraform prefix
This commit is contained in:
Коммит
5eeb4416d0
|
@ -18,7 +18,7 @@ az login
|
|||
|
||||
Navigate to [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and use the code output from the above command and login using an account associated to an Azure subscription.
|
||||
|
||||
## Step 2 (Optional)
|
||||
## Step 2 (optional)
|
||||
|
||||
If your account is associated to more than one tenant or subscription then you can switch to the specific one you want to work with now using
|
||||
|
||||
|
@ -26,14 +26,55 @@ If your account is associated to more than one tenant or subscription then you c
|
|||
az account set --subscription {subscription id}
|
||||
```
|
||||
|
||||
## Step 3
|
||||
## Step 3 (optional)
|
||||
|
||||
Now run Terraform so that it provisions the environment
|
||||
To store the Terraform state in the cloud and not locally, you should create an Azure Storage Account. You can do this easily, by running the following commands.
|
||||
|
||||
```bash
|
||||
RESOURCE_GROUP_NAME=terraform
|
||||
STORAGE_ACCOUNT_NAME=terraformstate$RANDOM
|
||||
CONTAINER_NAME=tfstate
|
||||
|
||||
# Create resource group
|
||||
az group create --name $RESOURCE_GROUP_NAME --location eastus
|
||||
|
||||
# Create storage account
|
||||
az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob
|
||||
|
||||
# Get storage account key
|
||||
ACCOUNT_KEY=$(az storage account keys list --resource-group $RESOURCE_GROUP_NAME --account-name $STORAGE_ACCOUNT_NAME --query [0].value -o tsv)
|
||||
|
||||
# Create blob container
|
||||
az storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME --account-key $ACCOUNT_KEY
|
||||
|
||||
echo "storage_account_name: $STORAGE_ACCOUNT_NAME"
|
||||
echo "container_name: $CONTAINER_NAME"
|
||||
echo "access_key: $ACCOUNT_KEY"
|
||||
```
|
||||
|
||||
Once this is done, navigate to the `Terraform/foundations.tf` file and replace the `storage_account_name` and `access_key` placeholders with the one from your console output.
|
||||
|
||||
```
|
||||
terraform {
|
||||
backend "azurerm" {
|
||||
storage_account_name = "__TFSTATE-STORAGE-ACCOUNT-NAME__" # <-- Here
|
||||
container_name = "tfstate"
|
||||
key = "terraform.tfstate"
|
||||
access_key = "__TFSTATE-STORAGE-ACCESS-KEY__" # <-- Here
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If you want to use the local state, just comment the above section out.
|
||||
|
||||
## Step 4
|
||||
|
||||
Now run Terraform so that it provisions the environment. Make sure to set a prefix to ensure that your resource names are unique.
|
||||
|
||||
```bash
|
||||
terraform init
|
||||
|
||||
terraform apply
|
||||
terraform apply -var 'prefix=your_unique_prefix'
|
||||
```
|
||||
|
||||
## Step 4
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_application_insights" "workshop" {
|
||||
name = "${var.resource_name}insights"
|
||||
name = "${var.prefix}${var.resource_name}insights"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
application_type = "Web"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_app_service_plan" "workshop" {
|
||||
name = "${var.resource_name}serviceplan"
|
||||
name = "${var.prefix}${var.resource_name}serviceplan"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
|
||||
|
@ -10,7 +10,7 @@ resource "azurerm_app_service_plan" "workshop" {
|
|||
}
|
||||
|
||||
resource "azurerm_app_service" "workshop" {
|
||||
name = "${var.resource_name}api"
|
||||
name = "${var.prefix}${var.resource_name}api"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
app_service_plan_id = "${azurerm_app_service_plan.workshop.id}"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_cosmosdb_account" "workshop" {
|
||||
name = "${var.resource_name}db"
|
||||
name = "${var.prefix}${var.resource_name}db"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
offer_type = "Standard"
|
||||
|
|
|
@ -1,12 +1,23 @@
|
|||
resource "random_id" "workshop" {
|
||||
byte_length = 2
|
||||
# Comment the following object to use local state
|
||||
terraform {
|
||||
backend "azurerm" {
|
||||
storage_account_name = "__TFSTATE-STORAGE-ACCOUNT-NAME__" # <-- TODO: Replace this
|
||||
container_name = "tfstate"
|
||||
key = "terraform.tfstate"
|
||||
access_key = "__TFSTATE-STORAGE-ACCESS-KEY__" # <-- TODO: Replace this
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_resource_group" "workshop" {
|
||||
name = "${var.resource_name}-${random_id.workshop.dec}"
|
||||
location = "westeurope"
|
||||
variable "prefix" {
|
||||
description = "A personal prefix (1-10 chars) that is attached to every resource to ensure its name is unique."
|
||||
}
|
||||
|
||||
variable "resource_name" {
|
||||
default = "contosomaintenance"
|
||||
default = "contoso"
|
||||
description = "Name of the project. Will be attached to every resource."
|
||||
}
|
||||
|
||||
resource "azurerm_resource_group" "workshop" {
|
||||
name = "${var.prefix}${var.resource_name}workshop"
|
||||
location = "westeurope"
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_app_service_plan" "func" {
|
||||
name = "${var.resource_name}functionsserviceplan"
|
||||
name = "${var.prefix}${var.resource_name}functionsserviceplan"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
kind = "FunctionApp"
|
||||
|
@ -11,7 +11,7 @@ resource "azurerm_app_service_plan" "func" {
|
|||
}
|
||||
|
||||
resource "azurerm_function_app" "workshop" {
|
||||
name = "${var.resource_name}functions"
|
||||
name = "${var.prefix}${var.resource_name}functions"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
app_service_plan_id = "${azurerm_app_service_plan.func.id}"
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
provider "azurerm" {
|
||||
version = "~> 1.15"
|
||||
}
|
||||
|
||||
provider "random" {
|
||||
version = "~> 1.3"
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_search_service" "workshop" {
|
||||
name = "${var.resource_name}search"
|
||||
name = "${var.prefix}${var.resource_name}search"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
sku = "standard"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_storage_account" "workshop" {
|
||||
name = "${var.resource_name}store"
|
||||
name = "${var.prefix}${var.resource_name}storage"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
location = "${azurerm_resource_group.workshop.location}"
|
||||
account_tier = "Standard"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
resource "azurerm_template_deployment" "workshop" {
|
||||
name = "${var.resource_name}vision"
|
||||
name = "${var.prefix}${var.resource_name}vision"
|
||||
resource_group_name = "${azurerm_resource_group.workshop.name}"
|
||||
|
||||
template_body = <<DEPLOY
|
||||
|
@ -47,7 +47,7 @@ resource "azurerm_template_deployment" "workshop" {
|
|||
DEPLOY
|
||||
|
||||
parameters {
|
||||
"name" = "vision${random_id.workshop.dec}"
|
||||
"name" = "${var.prefix}${var.resource_name}vision"
|
||||
"location" = "${azurerm_resource_group.workshop.location}"
|
||||
"apiType" = "ComputerVision"
|
||||
"sku" = "S1"
|
||||
|
|
Загрузка…
Ссылка в новой задаче