|
| 1 | +## TL;DR |
| 2 | + |
| 3 | +This terraform aim to run on both CloudShell and CI. |
| 4 | + |
| 5 | +Environment | Description | Use case |
| 6 | +---- | ---- | ---- |
| 7 | +CloudShell | Managed Console with MSI. | Each developer's cloud dev env. |
| 8 | +Local | az login credential | Each developer's local dev env. |
| 9 | +CI | Service Principal Id of application registration, terraform. | CI or any non-interactive env |
| 10 | + |
| 11 | +## Notice |
| 12 | + |
| 13 | +* terraform version : Will depends on what CloudShell supports. -> see terraform.tf |
| 14 | +* Env : ARM_ACCESS_KEY is required for terraform's backend blob authentication. |
| 15 | +* Env : ARM_USE_MSI will redirect your credential with local SP and CloudShell's MSI. |
| 16 | + |
| 17 | +## Run on Local (already az login) |
| 18 | + |
| 19 | +> SAMPLE : bashrc_local_azlogin |
| 20 | +
|
| 21 | +```bashrc |
| 22 | +export ARM_ACCESS_KEY=<....> |
| 23 | +export TF_VAR_SP_OBJECT_ID=<....> |
| 24 | +export TF_VAR_FUNCTION_APP_EVENTTRIGGER_SLACKWEBHOOKURL=<....> |
| 25 | +export TF_VAR_FUNCTION_APP_SLACKINCOMINGWEBHOOKURL=<....> |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Run on CI (service principal id for Terraform) |
| 30 | + |
| 31 | +> SAMPLE : bashrc_serviceprincipal |
| 32 | +
|
| 33 | +```bashrc |
| 34 | +export ARM_ACCESS_KEY=<....> |
| 35 | +export ARM_SUBSCRIPTION_ID=<....> |
| 36 | +export ARM_CLIENT_ID=<....> |
| 37 | +export ARM_CLIENT_SECRET=<....> |
| 38 | +export ARM_TENANT_ID=<....> |
| 39 | +export TF_VAR_SP_OBJECT_ID=<....> |
| 40 | +export TF_VAR_FUNCTION_APP_EVENTTRIGGER_SLACKWEBHOOKURL=<....> |
| 41 | +export TF_VAR_FUNCTION_APP_SLACKINCOMINGWEBHOOKURL=<....> |
| 42 | +``` |
| 43 | + |
| 44 | +## Run on CloudShell |
| 45 | + |
| 46 | + |
| 47 | +[](https://shell.azure.com) |
| 48 | + |
| 49 | +### Prerequisite for CloudShell |
| 50 | + |
| 51 | +Please create any blob storage you want to store your state. |
| 52 | +Run following at first time you logged in to CloudShell. |
| 53 | + |
| 54 | +#### Public Repo |
| 55 | + |
| 56 | +> SAMPLE : bashrc_cloudshell_public |
| 57 | +
|
| 58 | +```bashrc |
| 59 | +# load custom bashrc on startup |
| 60 | +$ echo "source bashrc" >> .bashrc |
| 61 | +$ cat << 'EOF' > bashrc |
| 62 | +export ARM_ACCESS_KEY=<....> |
| 63 | +export TF_VAR_SP_OBJECT_ID=<....> |
| 64 | +export TF_VAR_FUNCTION_APP_EVENTTRIGGER_SLACKWEBHOOKURL=<....> |
| 65 | +export TF_VAR_FUNCTION_APP_SLACKINCOMINGWEBHOOKURL=<....> |
| 66 | +EOF |
| 67 | +``` |
| 68 | + |
| 69 | +done! Let's try restart cloud shell. |
| 70 | + |
| 71 | +#### Private Repo |
| 72 | + |
| 73 | +if you are cloning from private repo, then use ssh auth. |
| 74 | + |
| 75 | +> SAMPLE : bashrc_cloudshell_private |
| 76 | +
|
| 77 | +```bashrc |
| 78 | +# load custom bashrc on startup |
| 79 | +$ echo "source bashrc" >> .bashrc |
| 80 | +$ cat << 'EOF' > bashrc |
| 81 | +export ARM_ACCESS_KEY=<....> |
| 82 | +export TF_VAR_SP_OBJECT_ID=<....> |
| 83 | +export TF_VAR_FUNCTION_APP_EVENTTRIGGER_SLACKWEBHOOKURL=<....> |
| 84 | +export TF_VAR_FUNCTION_APP_SLACKINCOMINGWEBHOOKURL=<....> |
| 85 | +# ssh agent (for private repo only) |
| 86 | +eval $(ssh-agent -s) |
| 87 | +ssh-add .ssh/id_rsa |
| 88 | +ssh-add -l |
| 89 | +EOF |
| 90 | +``` |
| 91 | + |
| 92 | +Github Authentication with Generate SSH key. |
| 93 | + |
| 94 | +> [Generating a new SSH key and adding it to the ssh-agent](https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/ |
| 95 | +) |
| 96 | + |
| 97 | +```shell |
| 98 | +$ ssh-keygen -t rsa -b 4096 -C "YOUR_USER_NAME@users.noreply.github.com" |
| 99 | +``` |
| 100 | + |
| 101 | +Copy ssh pub and paste to Github Public Key. |
| 102 | + |
| 103 | +> https://github.com/YOUR_ACCOUNT/REPOSITORY/settings/keys |
| 104 | +
|
| 105 | +```bash |
| 106 | +$ cat ~/.ssh/id_rsa.pub |
| 107 | +``` |
| 108 | + |
| 109 | +done! Let's try restart cloud shell. |
| 110 | +You will find ssh-agen loaded with specified ssh key. |
| 111 | + |
| 112 | +### Git Clone |
| 113 | + |
| 114 | +Move to clouddrive and clone git repo. |
| 115 | + |
| 116 | +```shell |
| 117 | +cd clouddrive |
| 118 | +``` |
| 119 | + |
| 120 | +* If you are using Public repo, you clone repo with https. |
| 121 | +* If you are using Private repo, you clone repo with ssh. |
| 122 | + |
| 123 | +```shell |
| 124 | +$ git clone git clone https://github.com/guitarrapc/AzureFunctionsIntroduction.git |
| 125 | +``` |
| 126 | + |
| 127 | +You may required git config for git commit/push ops. |
| 128 | + |
| 129 | +``` |
| 130 | +$ git config --global user.name "John Doe" |
| 131 | +$ git config --global user.email johndoe@users.noreply.github.com |
| 132 | +``` |
| 133 | + |
| 134 | +## Terraform Ops |
| 135 | + |
| 136 | + |
| 137 | +move to repo's terraform directory. |
| 138 | + |
| 139 | +```shell |
| 140 | +$ cd ~/clouddrive/AzureFunctionsIntroduction/Terraform/v2 |
| 141 | +``` |
| 142 | + |
| 143 | +### tfstate Blob settings |
| 144 | + |
| 145 | +If you chaned your blob, modify `terraform.tf` the remote state file. |
| 146 | + |
| 147 | +```shell |
| 148 | +$ cat terraform.tf |
| 149 | + |
| 150 | +terraform { |
| 151 | + required_version = "> 0.11.0" |
| 152 | + |
| 153 | + backend "azurerm" { |
| 154 | + storage_account_name = "terraformguitarrapc" |
| 155 | + container_name = "azurefunctionsintroduction" |
| 156 | + key = "azure/v2/terraform.tfstate" |
| 157 | + resource_group_name = "terraform" |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### Terraform init |
| 163 | + |
| 164 | +Operate with terraform init, plan, apply! |
| 165 | + |
| 166 | +```shell |
| 167 | +$ terraform init |
| 168 | + |
| 169 | +Initializing the backend... |
| 170 | + |
| 171 | +Successfully configured the backend "azurerm"! Terraform will automatically |
| 172 | +use this backend unless the backend configuration changes. |
| 173 | + |
| 174 | +Initializing provider plugins... |
| 175 | +- Checking for available provider plugins on https://releases.hashicorp.com... |
| 176 | +- Downloading plugin for provider "azurerm" (1.13.0)... |
| 177 | + |
| 178 | +Terraform has been successfully initialized! |
| 179 | + |
| 180 | +You may now begin working with Terraform. Try running "terraform plan" to see |
| 181 | +any changes that are required for your infrastructure. All Terraform commands |
| 182 | +should now work. |
| 183 | + |
| 184 | +If you ever set or change modules or backend configuration for Terraform, |
| 185 | +rerun this command to reinitialize your working directory. If you forget, other |
| 186 | +commands will detect it and remind you to do so if necessary. |
| 187 | +``` |
| 188 | +
|
| 189 | +
|
| 190 | +### Terraform plan and Apply |
| 191 | +
|
| 192 | +```shell |
| 193 | +$ terraform plan |
| 194 | +$ terraform apply |
| 195 | +``` |
0 commit comments