Skip to content

Commit e509cab

Browse files
committed
revision-1.0
0 parents  commit e509cab

File tree

9 files changed

+337
-0
lines changed

9 files changed

+337
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#-------------------------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
4+
#-------------------------------------------------------------------------------------------------------------
5+
6+
# Pick any base image, but if you select node, skip installing node. 😊
7+
FROM debian:9
8+
9+
# Terraform and tflint versions
10+
ARG TERRAFORM_VERSION=0.12.16
11+
ARG TFLINT_VERSION=0.8.2
12+
13+
# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
14+
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
15+
# will be updated to match your local UID/GID (when using the dockerFile property).
16+
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
17+
ARG USERNAME=vscode
18+
ARG USER_UID=1000
19+
ARG USER_GID=$USER_UID
20+
21+
# Configure apt and install packages
22+
RUN apt-get update \
23+
&& export DEBIAN_FRONTEND=noninteractive \
24+
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
25+
#
26+
# install git iproute2, required tools installed
27+
&& apt-get install -y \
28+
git \
29+
openssh-client \
30+
less \
31+
curl \
32+
procps \
33+
unzip \
34+
apt-transport-https \
35+
ca-certificates \
36+
gnupg-agent \
37+
software-properties-common \
38+
lsb-release 2>&1 \
39+
#
40+
# [Optional] Install Node.js for Azure Cloud Shell support
41+
# Change the "lts/*" in the two lines below to pick a different version
42+
&& curl -so- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash 2>&1 \
43+
&& /bin/bash -c "source $HOME/.nvm/nvm.sh \
44+
&& nvm install lts/* \
45+
&& nvm alias default lts/*" 2>&1 \
46+
#
47+
# [Optional] For local testing instead of cloud shell
48+
# Install Docker CE CLI.
49+
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | apt-key add - 2>/dev/null \
50+
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" \
51+
&& apt-get update \
52+
&& apt-get install -y docker-ce-cli \
53+
#
54+
# [Optional] For local testing instead of cloud shell
55+
# Install the Azure CLI
56+
&& echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/azure-cli.list \
57+
&& curl -sL https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 2>/dev/null \
58+
&& apt-get update \
59+
&& apt-get install -y azure-cli \
60+
#
61+
# Install Terraform, tflint, and graphviz
62+
&& mkdir -p /tmp/docker-downloads \
63+
&& curl -sSL -o /tmp/docker-downloads/terraform.zip https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
64+
&& unzip /tmp/docker-downloads/terraform.zip \
65+
&& mv terraform /usr/local/bin \
66+
&& curl -sSL -o /tmp/docker-downloads/tflint.zip https://github.com/wata727/tflint/releases/download/v${TFLINT_VERSION}/tflint_linux_amd64.zip \
67+
&& unzip /tmp/docker-downloads/tflint.zip \
68+
&& mv tflint /usr/local/bin \
69+
&& cd ~ \
70+
&& rm -rf /tmp/docker-downloads \
71+
&& apt-get install -y graphviz \
72+
#
73+
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
74+
&& groupadd --gid $USER_GID $USERNAME \
75+
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
76+
# [Optional] Add sudo support for the non-root user
77+
&& apt-get install -y sudo \
78+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
79+
&& chmod 0440 /etc/sudoers.d/$USERNAME \
80+
#
81+
# Clean up
82+
&& apt-get autoremove -y \
83+
&& apt-get clean -y \
84+
&& rm -rf /var/lib/apt/lists/*

.devcontainer/devcontainer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.128.0/containers/azure-terraform-0.12
3+
{
4+
"name": "Azure Terraform 0.12",
5+
"dockerFile": "Dockerfile",
6+
"mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
7+
8+
// Set *default* container specific settings.json values on container create.
9+
"settings": {
10+
"terminal.integrated.shell.linux": "/bin/bash"
11+
},
12+
13+
// Add the IDs of extensions you want installed when the container is created.
14+
"extensions": [
15+
"hashicorp.terraform",
16+
"ms-azuretools.vscode-azureterraform",
17+
"ms-vscode.azurecli",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
21+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
22+
// "forwardPorts": [],
23+
24+
// Use 'postCreateCommand' to run commands after the container is created.
25+
// "postCreateCommand": "terraform --version",
26+
27+
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
28+
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
29+
30+
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
31+
// "remoteUser": "vscode"
32+
}

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
11+
# Exclude all .tfvars files, which are likely to contain sentitive data, such as
12+
# password, private keys, and other secrets. These should not be part of version
13+
# control as they are data points which are potentially sensitive and subject
14+
# to change depending on the environment.
15+
#
16+
*.tfvars
17+
18+
# Ignore override files as they are usually used to override resources locally and so
19+
# are not checked in
20+
override.tf
21+
override.tf.json
22+
*_override.tf
23+
*_override.tf.json
24+
25+
# Include override files you do wish to add to version control using negated pattern
26+
#
27+
# !example_override.tf
28+
29+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30+
# example: *tfplan*
31+
32+
# Ignore CLI configuration files
33+
.terraformrc
34+
terraform.rc
35+
36+
# Special credential files
37+
# Exclude all files with credentials stored in
38+
# Defined credentil files are: [somename]cred.conf and files with [somename].cred
39+
*creds.conf
40+
*cred.conf
41+
*.creds
42+
*.cred

.images/logo.png

217 KB
Loading

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![Contributors][contributors-shield]][contributors-url]
2+
[![Forks][forks-shield]][forks-url]
3+
[![Stargazers][stars-shield]][stars-url]
4+
[![Issues][issues-shield]][issues-url]
5+
[![MIT License][license-shield]][license-url]
6+
[![LinkedIn][linkedin-shield]][linkedin-url]
7+
8+
<!-- PROJECT LOGO -->
9+
<br />
10+
<p align="center">
11+
<a href="https://github.com/adminph-de/repo">
12+
<img src=".images/logo.png" alt="Logo">
13+
</a>
14+
<h3 align="center">Deploy a Remote Backend to Azure with Terraform</h3>
15+
</p>
16+
17+
<!-- TABLE OF CONTENTS -->
18+
## Table of Contents
19+
20+
* [About the Project](#about-the-project)
21+
* [Deployment](#deployment)
22+
* [Contact](#contact)
23+
* [Referenzes](#referenzes)
24+
25+
## About The Project
26+
27+
Using Terraform to deploy a StorageAccount in Azure to use it as a [Terraform Remote Backend](https://www.terraform.io/docs/backends/types/remote.html) to store your future tfstate files. Part of the reposotory is a folder called ```.devcontainer``` it contains the Docker container configuration to use it with [Microsoft Visial Studio Code](https://code.visualstudio.com/download) and the [Remote-Container](https://code.visualstudio.com/docs/remote/containers) technologie. [Terraform](https://www.terraform.io/downloads.html) and [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) is installed, if you run the container. You need apart of [Microsoft Visial Studio Code](https://code.visualstudio.com/download) [Docker Desktop](https://www.docker.com/get-started) installed on your PC, MAC or Linux to use [Remote-Container](https://code.visualstudio.com/docs/remote/containers). This is only optional and not requited to run the Terraform scripts.
28+
29+
30+
### Deployment
31+
32+
1. Clone the repo
33+
```sh
34+
git clone https://github.com/adminph-de/tf-azure-backend.git
35+
```
36+
2. Azure Credential file:
37+
Create an addinal file in your cloned repo, called: ```azurecert.conf``` with your spezific ```TENANT_ID```, ```SUBSCRIPTION_ID``` and a ```Azure Service Principal (SPN)``` ```CLIENT_ID``` and ```CLIENT_SECRET``` variable. Find the HowToBuild a SPN in the referenzes below.
38+
39+
Content of the file:
40+
```
41+
ARM_TENANT_ID="0000000-0000-0000-0000-000000000000"
42+
ARM_SUBSCRIPTION_ID="0000000-0000-0000-0000-000000000000"
43+
ARM_CLIENT_ID="0000000-0000-0000-0000-000000000000"
44+
ARM_CLIENT_SECRET="0000000-0000-0000-0000-000000000000"
45+
```
46+
47+
3. Terraform Variables file:
48+
Create a ```terraform.tfvars``` file in your cloned repo with the variable values like this:
49+
```
50+
project = "Remote Backend for tfstate files"
51+
environment = "PROD"
52+
location = "westus2"
53+
```
54+
55+
4. Run Terraform Init:
56+
```
57+
terraform init -backend-config=azurecreds.conf
58+
```
59+
60+
5. Run Terraform Plan:
61+
```
62+
terraform plan
63+
```
64+
65+
6. Run Terraform Apply:
66+
```
67+
terraform apply -auto-approve
68+
```
69+
70+
7. Check your result in your Azure Subscription
71+
72+
<!-- CONTACT -->
73+
## Contact
74+
75+
[LinkedIN](https://www.linkedin.com/in/patrickhayo/?locale=en_US) - [Twitter](https://twitter.com/N00ky2010) - [Email](patrick.hayo@flsmidth.com)
76+
77+
Project Link: [https://github.com/adminph-de/repo](https://github.com/adminph-de/repo)
78+
79+
<!-- REFERENZES -->
80+
## Referenzes
81+
82+
Author of the scrpit source is [Guillermo Musumeci](https://medium.com/@gmusumeci). Find detaild docmentation and explainations in his article:
83+
* [How to Create an Azure Remote Backend for Terraform](https://medium.com/@gmusumeci/how-to-create-an-azure-remote-backend-for-terraform-67cce5da1520)
84+
85+
Addinaly to this article, check:
86+
* [How to manage Terraform state in Azure Blob Storage](https://medium.com/developingnodes/how-to-manage-terraform-state-in-azure-blob-storage-870a80917450)
87+
* [Terraform CLI (azurerm)](https://www.terraform.io/docs/backends/types/azurerm.html)
88+
89+
<!-- MARKDOWN LINKS & IMAGES -->
90+
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
91+
[contributors-shield]: https://img.shields.io/github/contributors/adminph-de/tf-azure-backend.svg?style=flat-square
92+
[contributors-url]: https://github.com/adminph-de/tf-azure-backend/graphs/contributors
93+
[forks-shield]: https://img.shields.io/github/forks/adminph-de/tf-azure-backend.svg?style=flat-square
94+
[forks-url]: https://github.com/adminph-de/tf-azure-backend/network/members
95+
[stars-shield]: https://img.shields.io/github/stars/adminph-de/tf-azure-backend?style=flat-square
96+
[stars-url]: https://github.com/adminph-de/tf-azure-backend/stargazers
97+
[issues-shield]: https://img.shields.io/github/issues/adminph-de/tf-azure-backend.svg?style=flat-square
98+
[issues-url]: https://github.com/adminph-de/tf-azure-backend/issues
99+
[license-shield]: https://img.shields.io/github/license/adminph-de/tf-azure-backend.svg?style=flat-square
100+
[license-url]: https://github.com/adminph-de/tf-azure-backend/blob/master/LICENSE.txt
101+
[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square&logo=linkedin&colorB=555
102+
[linkedin-url]: https://www.linkedin.com/in/patrickhayo/?locale=en_US
103+
[product-screenshot]: images/screenshot.png

az-remote-backend-main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Generate a random storage name
2+
resource "random_string" "tf-name" {
3+
length = 8
4+
upper = false
5+
number = true
6+
lower = false
7+
special = false
8+
}
9+
# Create a Resource Group for the Terraform State File
10+
resource "azurerm_resource_group" "state-rg" {
11+
name = "terraform-tfstate-${lower(var.location)}"
12+
location = var.location
13+
lifecycle {
14+
prevent_destroy = true
15+
}
16+
tags = {
17+
Environment = var.environment
18+
Description = "Created by Terraform"
19+
}
20+
}
21+
# Create a Storage Account for the Terraform State File
22+
resource "azurerm_storage_account" "state-sta" {
23+
depends_on = [azurerm_resource_group.state-rg]
24+
#name = "${lower(var.company)}tf${random_string.tf-name.result}"
25+
name = "tfstate${random_string.tf-name.result}"
26+
resource_group_name = azurerm_resource_group.state-rg.name
27+
location = azurerm_resource_group.state-rg.location
28+
account_kind = "StorageV2"
29+
account_tier = "Standard"
30+
access_tier = "Hot"
31+
account_replication_type = "LRS"
32+
enable_https_traffic_only = true
33+
lifecycle {
34+
prevent_destroy = true
35+
}
36+
tags = {
37+
Project = var.project
38+
Environment = var.environment
39+
Description = "Created by Terraform"
40+
}
41+
}
42+
# Create a Storage Container for the Core State File
43+
resource "azurerm_storage_container" "core-container" {
44+
depends_on = [azurerm_storage_account.state-sta]
45+
name = "core-tfstate"
46+
storage_account_name = azurerm_storage_account.state-sta.name
47+
}

az-remote-backend-output.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "terraform_state_resource_group_name" {
2+
value = azurerm_resource_group.state-rg.name
3+
}
4+
output "terraform_state_storage_account" {
5+
value = azurerm_storage_account.state-sta.name
6+
}
7+
output "terraform_state_storage_container_core" {
8+
value = azurerm_storage_container.core-container.name
9+
}

az-remote-backend-variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# environment
2+
variable "environment" {
3+
type = string
4+
description = "This variable defines the environment to be built"
5+
}
6+
# azure region
7+
variable "location" {
8+
type = string
9+
description = "Azure region where the resource group will be created"
10+
default = "north europe"
11+
}
12+
variable "project" {
13+
type = string
14+
description = "Name of the Terraform Project"
15+
}

provider-main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Configure the Azure provider
2+
provider "azurerm" {
3+
version = "~>2.00"
4+
features {}
5+
}

0 commit comments

Comments
 (0)