diff --git a/modules/.gitattributes b/.gitattributes similarity index 100% rename from modules/.gitattributes rename to .gitattributes diff --git a/.github/workflows/terraform-ci.yml b/.github/workflows/terraform-ci.yml new file mode 100644 index 0000000..8f26c93 --- /dev/null +++ b/.github/workflows/terraform-ci.yml @@ -0,0 +1,66 @@ +name: Validate Terraform + +on: + push: + branches: + - master + - main + - develop + + pull_request: + branches: + - master + - main + - develop + + workflow_dispatch: + +# schedule: +# - cron: '0 0 * * 0' + +jobs: + build-webapp: + name: Validate Terraform + runs-on: ${{ matrix.environment }} + strategy: + matrix: + environment: + - ubuntu-latest + + env: + DOTNET_NOLOGO: 1 + DOTNET_CLI_TELEMETRY_OPTOUT: 1 + ACTIONS_RUNNER_DEBUG: true + TF_LOG: INFO + ARM_CLIENT_ID: "" + ARM_CLIENT_SECRET: ${{ secrets.TF_ARM_CLIENT_SECRET }} + ARM_SUBSCRIPTION_ID: "" + ARM_TENANT_ID: "" + WORKING_DIR: "./" + + steps: + - name: Fetch Sources + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v2 + with: + terraform_version: latest + + - name: Terraform Version + run: terraform -v + working-directory: ${{ env.WORKING_DIR }} + + - name: Terraform Init + run: terraform init -backend=false + working-directory: ${{ env.WORKING_DIR }} + + - name: Terraform Validate + run: terraform validate + working-directory: ${{ env.WORKING_DIR }} + + - name: Terraform Format + run: terraform fmt -check + working-directory: ${{ env.WORKING_DIR }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index e69de29..28af64a 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,8 @@ +.idea +.ssh +*.conf +.terraform +*.tfplan +*.tfstate +*.lock.info +*.tfstate.backup \ No newline at end of file diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..42fad84 --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,21 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/azurerm" { + version = "3.71.0" + hashes = [ + "h1:xySu+5dS0H9KYVsQoFp61uc5XLRKif9FrFs//OPNDrM=", + "zh:06f0d225b1711dfad256ff33134f878acc8f84624d9da66b075b075cc4d75892", + "zh:09ff74056818babe02ea5a633bffe2b8223eaf79916dc1db169651ef7725c22f", + "zh:27687e0f8458e6d88ebea94352eb523f56e8f5cdc468268af8f38dc4a4265bf4", + "zh:2d81bfab3c6a9b897fa8fbb5256c9e5a944e6ecbf7f73a2a3e2b53a2c4fbcfc5", + "zh:4cfc744cfc37aeeeecd82800c70e2591b38447af9e3c51bcbf06a5efe842ed65", + "zh:734fbb81508b264f772a076338ddf1c7b25534d2007a1738a7d55587478ed258", + "zh:9a5502c364f58073599fff8cdd8adc32e7f7bcd00a4d9b57d2fff678fd8a8319", + "zh:9bc528f7e78dbfd106f94b741b68dedd3dd3d31c3defcddcc1972c8e52a6b7db", + "zh:c30db03d877f9a7ae0c19d3fd338bbf95cdddbf6df1023709dbfa99689abac14", + "zh:c51d4065145b8f4ca45fc9a0f3ca7f2d933bc0302af2eead74f3ce64a9221ae8", + "zh:e23029fc7f81723795d7da770131adb1ce6f4d32f0a57eb75d47e036a0a19833", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} diff --git a/modules/README.INFO.md b/README.INFO.md similarity index 100% rename from modules/README.INFO.md rename to README.INFO.md diff --git a/azure-pipelines/azdo-terraform-ci.yml b/azure-pipelines/azdo-terraform-ci.yml new file mode 100644 index 0000000..eae824f --- /dev/null +++ b/azure-pipelines/azdo-terraform-ci.yml @@ -0,0 +1,52 @@ +trigger: + batch: true + branches: + include: + - master + paths: + include: + - '*' + +pr: + branches: + include: + - '*' + +variables: + - name: TF_LOG + value: INFO + - name: WORKING_DIR + value: '$(System.DefaultWorkingDirectory)' + +stages: + - stage: "Terraform_CI" + displayName: "Terraform Validate" + jobs: + - job: Terraform_CI + displayName: "Terraform Validate" + pool: + vmImage: "ubuntu-latest" + steps: + - checkout: self + fetchDepth: 0 + + - task: TerraformInstaller@0 + displayName: 'Terraform Installer' + inputs: + terraformVersion: 'latest' + + - script: terraform -v + displayName: 'Terraform Version' + workingDirectory: '$(WORKING_DIR)' + + - script: terraform init -backend=false + displayName: 'Terraform Init' + workingDirectory: '$(WORKING_DIR)' + + - script: terraform validate + displayName: 'Terraform Validate' + workingDirectory: '$(WORKING_DIR)' + + - script: terraform fmt -check + displayName: 'Terraform Format' + workingDirectory: '$(WORKING_DIR)' \ No newline at end of file diff --git a/locals.tf b/locals.tf new file mode 100644 index 0000000..3420a03 --- /dev/null +++ b/locals.tf @@ -0,0 +1,3 @@ +locals { + rg_name = "${var.resource_group_name}-${var.prefix}" +} \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..93c25ac --- /dev/null +++ b/main.tf @@ -0,0 +1,12 @@ +data "azurerm_client_config" "current" {} + +resource "azurerm_resource_group" "public" { + location = var.resource_group_location + name = local.rg_name +} + +module "resource_group" { + source = "./modules/example_submodule" + resource_group_location = "northeurope" + resource_group_name = "rg-from-module" +} \ No newline at end of file diff --git a/modules/example_submodule/main.tf b/modules/example_submodule/main.tf new file mode 100644 index 0000000..2c19d15 --- /dev/null +++ b/modules/example_submodule/main.tf @@ -0,0 +1,8 @@ +locals { + rg_name = var.resource_group_name +} + +resource "azurerm_resource_group" "public" { + location = var.resource_group_location + name = local.rg_name +} \ No newline at end of file diff --git a/modules/example_submodule/output.tf b/modules/example_submodule/output.tf new file mode 100644 index 0000000..e5e8c4b --- /dev/null +++ b/modules/example_submodule/output.tf @@ -0,0 +1,3 @@ +output "rg_id" { + value = azurerm_resource_group.public.id +} \ No newline at end of file diff --git a/modules/variables.tf b/modules/example_submodule/variables.tf similarity index 64% rename from modules/variables.tf rename to modules/example_submodule/variables.tf index 05f4b80..c944ef3 100644 --- a/modules/variables.tf +++ b/modules/example_submodule/variables.tf @@ -1,9 +1,9 @@ -variable "resourece_group_name" { +variable "resource_group_name" { type = string description = "Resource group name" } -variable "resourece_group_location" { +variable "resource_group_location" { type = string description = "Resource group location" } diff --git a/modules/main.tf b/modules/main.tf deleted file mode 100644 index e69de29..0000000 diff --git a/modules/output.tf b/output.tf similarity index 100% rename from modules/output.tf rename to output.tf diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..d042e32 --- /dev/null +++ b/provider.tf @@ -0,0 +1,7 @@ +provider "azurerm" { + features { + resource_group { + prevent_deletion_if_contains_resources = false + } + } +} \ No newline at end of file diff --git a/terraform.auto.tfvars.json b/terraform.auto.tfvars.json new file mode 100644 index 0000000..ec9d27a --- /dev/null +++ b/terraform.auto.tfvars.json @@ -0,0 +1,5 @@ +{ + "resource_group_name": "rg-terraform-template", + "resource_group_location": "northeurope", + "prefix": "tf01" +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..b86663a --- /dev/null +++ b/variables.tf @@ -0,0 +1,14 @@ +variable "resource_group_name" { + type = string + description = "Resource group name" +} + +variable "resource_group_location" { + type = string + description = "Resource group location" +} + +variable "prefix" { + type = string + description = "Prefix for all resources" +} diff --git a/modules/versions.tf b/versions.tf similarity index 100% rename from modules/versions.tf rename to versions.tf