-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathcreate-storage-account.sh
101 lines (85 loc) · 3.91 KB
/
create-storage-account.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
#Variables
resourceGroupName="StorageAccountsRG"
storageAccountName="baboterraform"
containerName="tfstate"
location="WestEurope"
sku="Standard_LRS"
subscriptionName=$(az account show --query name --output tsv)
# Create resource group
echo "Checking if [$resourceGroupName] resource group actually exists in the [$subscriptionName] subscription..."
az group show --name $resourceGroupName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$resourceGroupName] resource group actually exists in the [$subscriptionName] subscription"
echo "Creating [$resourceGroupName] resource group in the [$subscriptionName] subscription..."
# Create the resource group
az group create \
--name $resourceGroupName \
--location $location 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$resourceGroupName] resource group successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$resourceGroupName] resource group in the [$subscriptionName] subscription"
exit
fi
else
echo "[$resourceGroupName] resource group already exists in the [$subscriptionName] subscription"
fi
# Create storage account
echo "Checking if [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription..."
az storage account --name $storageAccountName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountName] storage account in the [$subscriptionName] subscription..."
az storage account create \
--resource-group $resourceGroupName \
--name $storageAccountName \
--sku $sku \
--encryption-services blob 1>/dev/null
# Create the storage account
if [[ $? == 0 ]]; then
echo "[$storageAccountName] storage account successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountName] storage account in the [$subscriptionName] subscription"
exit
fi
else
echo "[$storageAccountName] storage account already exists in the [$subscriptionName] subscription"
fi
# Get storage account key
echo "Retrieving the primary key of the [$storageAccountName] storage account..."
storageAccountKey=$(az storage account keys list --resource-group $resourceGroupName --account-name $storageAccountName --query [0].value -o tsv)
if [[ -n $storageAccountKey ]]; then
echo "Primary key of the [$storageAccountName] storage account successfully retrieved"
else
echo "Failed to retrieve the primary key of the [$storageAccountName] storage account"
exit
fi
# Create blob container
echo "Checking if [$containerName] container actually exists in the [$storageAccountName] storage account..."
az storage container show \
--name $containerName \
--account-name $storageAccountName \
--account-key $storageAccountKey &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$containerName] container actually exists in the [$storageAccountName] storage account"
echo "Creating [$containerName] container in the [$storageAccountName] storage account..."
# Create the container
az storage container create \
--name $containerName \
--account-name $storageAccountName \
--account-key $storageAccountKey 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$containerName] container successfully created in the [$storageAccountName] storage account"
else
echo "Failed to create [$containerName] container in the [$storageAccountName] storage account"
exit
fi
else
echo "[$containerName] container already exists in the [$storageAccountName] storage account"
fi
# Print data
echo "----------------------------------------------------------------------------------------------"
echo "storageAccountName: $storageAccountName"
echo "containerName: $containerName"
echo "access_key: $storageAccountKey"