|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# The purpose of this script is to provision the resources needed to store |
| 4 | +# terraform states on AZURE. It will provision a container on Azure Storage account as well as a |
| 5 | +# container to support state locking and consistency checking. |
| 6 | +# |
| 7 | +# Official documentation about terraform state in Azure Storage Account => |
| 8 | +# https://developer.hashicorp.com/terraform/language/settings/backends/azurerm |
| 9 | +# Bash strict mode |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +REGION=westeurope |
| 13 | +NAME= |
| 14 | +RESOURCE_GROUP_NAME=administration |
| 15 | +SUBSCRIPTION= |
| 16 | + |
| 17 | +function help() { |
| 18 | + cat <<EOF |
| 19 | +Provision the resources needed to store terraform states on Azure. |
| 20 | +A storage account is created and container for store state. |
| 21 | +Usage : $0 -n NAME [options] |
| 22 | +
|
| 23 | +Mandatory arguments : |
| 24 | + -n NAME Set the name of created resources. |
| 25 | +Available options : |
| 26 | + -r The name of the region (default $REGION). |
| 27 | + -s The name of the subscription. |
| 28 | + -rg The name of Ressource Group (default $RESOURCE_GROUP_NAME). |
| 29 | + -st The name of Storage Account. |
| 30 | + -ct The name of Container. |
| 31 | + -h Display this help. |
| 32 | +EOF |
| 33 | +} |
| 34 | + |
| 35 | +while getopts "n:s:r:h:rg:st:ct" opt; do |
| 36 | + case "$opt" in |
| 37 | + h) |
| 38 | + help |
| 39 | + exit 0 |
| 40 | + ;; |
| 41 | + r) |
| 42 | + REGION=$OPTARG |
| 43 | + ;; |
| 44 | + n) |
| 45 | + NAME=$OPTARG |
| 46 | + ;; |
| 47 | + rg) |
| 48 | + RESOURCE_GROUP_NAME=$OPTARG |
| 49 | + ;; |
| 50 | + st) |
| 51 | + STORAGE_ACCOUNT_NAME=$OPTARG |
| 52 | + ;; |
| 53 | + ct) |
| 54 | + CONTAINER_NAME=$OPTARG |
| 55 | + ;; |
| 56 | + s) |
| 57 | + SUBSCRIPTION=$OPTARG |
| 58 | + ;; |
| 59 | + |
| 60 | + esac |
| 61 | +done |
| 62 | + |
| 63 | +if [ "$NAME" == "" ]; then |
| 64 | + echo "Name was not specified, aborting !" |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +if [ "$SUBSCRIPTION" == "" ]; then |
| 69 | + echo "Subscription not specified, the subscription is the default sub of the account." |
| 70 | + SUBSCRIPTION=$(az account list --query "[?isDefault].id | [0]" -o tsv) |
| 71 | +fi |
| 72 | + |
| 73 | +#Set var with name input |
| 74 | +STORAGE_ACCOUNT_NAME=${NAME}staccount |
| 75 | +CONTAINER_NAME=tfstate${STORAGE_ACCOUNT_NAME} |
| 76 | + |
| 77 | +export AZURE_extension_use_dynamic_install=yes_without_prompt |
| 78 | +user=$(az ad signed-in-user show --query userPrincipalName --output tsv) |
| 79 | + |
| 80 | +# Create Storage Account |
| 81 | +echo "Creating storage account : ${RESOURCE_GROUP_NAME}" |
| 82 | +res=$(az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob) |
| 83 | + |
| 84 | +echo "Creating Storage Account : ${RESOURCE_GROUP_NAME}" |
| 85 | +# Create Storage Account Container |
| 86 | +res=$(az storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME --auth-mode login) |
| 87 | + |
| 88 | +echo "storage_account_name: $STORAGE_ACCOUNT_NAME" |
| 89 | +echo "container_name: $CONTAINER_NAME" |
0 commit comments