From ceb642105b2ca55103005533e3104d34420e1b83 Mon Sep 17 00:00:00 2001 From: Mike DuPont Date: Tue, 10 Dec 2024 20:39:42 -0500 Subject: [PATCH] Initial commit --- .../workflows/terraform-security-check.yml | 17 +++ .github/workflows/terraform-validate.yml | 31 +++++ .gitignore | 42 ++++++ README.md | 104 +++++++++++++++ .../demo-azure-vm/dev/east-us/README.md | 35 +++++ .../demo-azure-vm/dev/east-us/main.tf | 8 ++ .../demo-azure-vm/dev/east-us/output.tf | 15 +++ .../demo-azure-vm/dev/east-us/variables.tf | 29 ++++ .../production/east-us/README.md | 35 +++++ .../demo-azure-vm/production/east-us/main.tf | 8 ++ .../production/east-us/output.tf | 15 +++ .../production/east-us/variables.tf | 29 ++++ .../demo-azure-vm/staging/east-us/README.md | 35 +++++ .../demo-azure-vm/staging/east-us/main.tf | 8 ++ .../demo-azure-vm/staging/east-us/output.tf | 15 +++ .../staging/east-us/variables.tf | 29 ++++ .../staging/southeastasia/README.md | 35 +++++ .../staging/southeastasia/main.tf | 8 ++ .../staging/southeastasia/output.tf | 15 +++ .../staging/southeastasia/variables.tf | 29 ++++ .../demo-message/dev/east-us/backend.tf | 0 environments/demo-message/dev/east-us/main.tf | 4 + .../demo-message/dev/east-us/output.tf | 4 + .../demo-message/dev/east-us/variables.tf | 4 + .../production/east-us/backend.tf | 0 .../demo-message/production/east-us/main.tf | 4 + .../demo-message/production/east-us/output.tf | 4 + .../production/east-us/variables.tf | 4 + .../demo-message/staging/east-us/backend.tf | 0 .../demo-message/staging/east-us/main.tf | 4 + .../demo-message/staging/east-us/output.tf | 4 + .../demo-message/staging/east-us/variables.tf | 4 + modules/demo-azure-vm/README.md | 3 + modules/demo-azure-vm/main.tf | 124 ++++++++++++++++++ modules/demo-azure-vm/outputs.tf | 12 ++ modules/demo-azure-vm/providers.tf | 22 ++++ modules/demo-azure-vm/ssh.tf | 28 ++++ modules/demo-azure-vm/variables.tf | 29 ++++ modules/demo-message/main.tf | 5 + modules/demo-message/outputs.tf | 4 + modules/demo-message/variables.tf | 4 + utils/deploy-something/main.tf | 1 + 42 files changed, 810 insertions(+) create mode 100644 .github/workflows/terraform-security-check.yml create mode 100644 .github/workflows/terraform-validate.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 environments/demo-azure-vm/dev/east-us/README.md create mode 100644 environments/demo-azure-vm/dev/east-us/main.tf create mode 100644 environments/demo-azure-vm/dev/east-us/output.tf create mode 100644 environments/demo-azure-vm/dev/east-us/variables.tf create mode 100644 environments/demo-azure-vm/production/east-us/README.md create mode 100644 environments/demo-azure-vm/production/east-us/main.tf create mode 100644 environments/demo-azure-vm/production/east-us/output.tf create mode 100644 environments/demo-azure-vm/production/east-us/variables.tf create mode 100644 environments/demo-azure-vm/staging/east-us/README.md create mode 100644 environments/demo-azure-vm/staging/east-us/main.tf create mode 100644 environments/demo-azure-vm/staging/east-us/output.tf create mode 100644 environments/demo-azure-vm/staging/east-us/variables.tf create mode 100644 environments/demo-azure-vm/staging/southeastasia/README.md create mode 100644 environments/demo-azure-vm/staging/southeastasia/main.tf create mode 100644 environments/demo-azure-vm/staging/southeastasia/output.tf create mode 100644 environments/demo-azure-vm/staging/southeastasia/variables.tf create mode 100644 environments/demo-message/dev/east-us/backend.tf create mode 100644 environments/demo-message/dev/east-us/main.tf create mode 100644 environments/demo-message/dev/east-us/output.tf create mode 100644 environments/demo-message/dev/east-us/variables.tf create mode 100644 environments/demo-message/production/east-us/backend.tf create mode 100644 environments/demo-message/production/east-us/main.tf create mode 100644 environments/demo-message/production/east-us/output.tf create mode 100644 environments/demo-message/production/east-us/variables.tf create mode 100644 environments/demo-message/staging/east-us/backend.tf create mode 100644 environments/demo-message/staging/east-us/main.tf create mode 100644 environments/demo-message/staging/east-us/output.tf create mode 100644 environments/demo-message/staging/east-us/variables.tf create mode 100644 modules/demo-azure-vm/README.md create mode 100644 modules/demo-azure-vm/main.tf create mode 100644 modules/demo-azure-vm/outputs.tf create mode 100644 modules/demo-azure-vm/providers.tf create mode 100644 modules/demo-azure-vm/ssh.tf create mode 100644 modules/demo-azure-vm/variables.tf create mode 100644 modules/demo-message/main.tf create mode 100644 modules/demo-message/outputs.tf create mode 100644 modules/demo-message/variables.tf create mode 100644 utils/deploy-something/main.tf diff --git a/.github/workflows/terraform-security-check.yml b/.github/workflows/terraform-security-check.yml new file mode 100644 index 0000000..d607d38 --- /dev/null +++ b/.github/workflows/terraform-security-check.yml @@ -0,0 +1,17 @@ +name: 'Terraform Security' + +on: + workflow_dispatch: + # pull_request: + # push: + # branches: + # - main + # - develop + +jobs: + terraform: + name: 'Terraform Security Scan' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: triat/terraform-security-scan@v3.0.3 diff --git a/.github/workflows/terraform-validate.yml b/.github/workflows/terraform-validate.yml new file mode 100644 index 0000000..7d15cf2 --- /dev/null +++ b/.github/workflows/terraform-validate.yml @@ -0,0 +1,31 @@ +name: "Terraform Validation" + +on: + pull_request: + push: + branches: + - main + - develop + +jobs: + terraform: + name: "Terraform Syntax Check" + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v3.1.2 + with: + terraform_version: 1.1.7 + + - name: Terraform Format Check + run: terraform fmt -check -recursive + + - name: Terraform Init + run: terraform init + + - name: Terraform Validate + run: terraform validate diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff6d21d --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +# https://github.com/github/gitignore/blob/main/Terraform.gitignore + +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Ignore transient lock info files created by terraform apply +.terraform.tfstate.lock.info + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc + +# Lock +.terraform.lock.hcl \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..21310de --- /dev/null +++ b/README.md @@ -0,0 +1,104 @@ +# terraform-template + +Terraform project template for deploying infrastructure across multiple environments and regions, following best practices with modular structure and automated syntax checks (GitHub Actions) + +## Repo structure + +``` +terraform-template/ # Root directory of the Terraform template repository +├── README.md # Project documentation and overview +├── environments # Directory containing environment-specific configurations for different deployments +│ ├── demo-azure-vm # Environment folder for provisioning Azure VM resources using the demo-azure-vm module +│ │ ├── dev # Development environment configuration for the demo-azure-vm setup +│ │ │ └── east-us # Azure region-specific configuration for the dev environment (East US region) +│ │ │ ├── README.md # Documentation for the dev environment setup in East US region +│ │ │ ├── main.tf # Main Terraform configuration for provisioning resources in the dev environment +│ │ │ ├── output.tf # Outputs for the dev environment (e.g., VM IP address) +│ │ │ └── variables.tf # Variables specific to the dev environment (e.g., VM size, location) +│ │ └── staging # Staging environment folder for demo-azure-vm +│ │ ├── east-us # Staging environment configuration for East US region +│ │ │ ├── README.md # Documentation for the staging environment setup in East US region +│ │ │ ├── main.tf # Main Terraform configuration for staging resources in East US +│ │ │ ├── output.tf # Outputs for the staging environment +│ │ │ └── variables.tf # Variables specific to the staging environment (East US) +│ │ └── southeastasia # Staging environment configuration for Southeast Asia region +│ │ ├── README.md # Documentation for the staging environment setup in Southeast Asia region +│ │ ├── main.tf # Main Terraform configuration for staging resources in Southeast Asia +│ │ ├── output.tf # Outputs for the staging environment (Southeast Asia) +│ │ └── variables.tf # Variables specific to the staging environment (Southeast Asia) +│ └── production # Production environment folder for demo-azure-vm +│ └── east-us # Production environment configuration for East US region +│ └── demo-message # Environment folder for deploying resources using the demo-message module +│ ├── dev # Development environment configuration for demo-message setup +│ │ └── east-us # Region-specific configuration for the dev environment (East US region) +│ │ ├── backend.tf # Backend configuration for managing Terraform state +│ │ ├── main.tf # Main Terraform configuration for deploying demo-message resources +│ │ ├── output.tf # Outputs for the dev environment (e.g., message service IP address) +│ │ └── variables.tf # Variables specific to the dev environment (e.g., service names, ports) +│ ├── production # Production environment folder for demo-message +│ │ └── east-us # Production environment configuration for East US region +│ └── staging # Staging environment folder for demo-message +│ └── east-us # Staging environment configuration for East US region +├── modules # Directory containing reusable Terraform modules +│ ├── demo-azure-vm # Module for provisioning an Azure VM +│ │ ├── README.md # Documentation for the demo-azure-vm module +│ │ ├── main.tf # Main configuration for provisioning an Azure VM +│ │ ├── outputs.tf # Outputs from the demo-azure-vm module (e.g., public IP, VM ID) +│ │ ├── providers.tf # Provider configuration (Azure provider setup) +│ │ ├── ssh.tf # SSH-related configuration for Azure VM access (e.g., public keys) +│ │ └── variables.tf # Module-specific variables (e.g., VM size, admin credentials) +│ └── demo-message # Module for a simple message service deployment +│ ├── main.tf # Main configuration for deploying a demo message service +│ ├── outputs.tf # Outputs from the demo-message module (e.g., service endpoints) +│ └── variables.tf # Variables for configuring the demo-message module +└── utils # Utility scripts and configurations for managing infrastructure + └── deploy-something # Placeholder utility for deploying additional resources + └── main.tf # Main configuration for the utility (e.g., simple automation tasks) +``` + +## Demo content + +This demonstrates how modules, multiple environments, and shared resources work. In your real project, you can refer to this demo code and structure, and then delete the demo module when it is no longer needed. + +### Module + +These are the demo modules: + +- [modules/demo-message](./modules/demo-message/) +- [modules/demo-azure-vm](./modules/demo-azure-vm/) + +### Use demo-message module + +These demos use the same [modules/demo-message](./modules/demo-message/) above with difference variablesF + +- [environments/demo-message/dev/](./environments/demo-message/dev/) + ``` + cd environments/demo-message/dev/east-us + terraform init + terraform plan + terraform apply + ``` +- [environments/demo-message/staging/](./environments/demo-message/staging/) + ``` + cd environments/demo-message/staging/east-us + terraform init + terraform plan + terraform apply + ``` +- [environments/demo-message/production/](./environments/demo-message/production/) + ``` + cd environments/demo-message/production/east-us + terraform init + terraform plan + terraform apply + ``` + +### Use demo-azure-vm module + +- TODO: Add document + +## How to use the template? + +- Create a new repo from this template. +- Add the code for your appropriate use case. +- Optionally, delete the demo module/code when it is no longer needed to keep the repo clean. diff --git a/environments/demo-azure-vm/dev/east-us/README.md b/environments/demo-azure-vm/dev/east-us/README.md new file mode 100644 index 0000000..9f7d390 --- /dev/null +++ b/environments/demo-azure-vm/dev/east-us/README.md @@ -0,0 +1,35 @@ +## Deploy + +```bash +terraform init -upgrade + +terraform plan + +terraform apply +``` + +## Result + +- You can find the output information in the console + +``` +key_data = "xxxxxyyyyyzzzztttt generated-by-azure" +private_key_data = "zzzzzzzzzzzzzzzz" +public_ip_address = "x.y.z.t" +resource_group_name = "ENV-rg-some-thing" +``` + +## Connect to the VM + +```bash +# Save the private key *.pem +mkdir ssh_key +cd ssh_key +vi azure-vm-private-key.pem + +# It is required that your private key files are NOT accessible by others. +chmod 600 azure-vm-private-key.pem + +# Now SSH +ssh -i azure-vm-private-key.pem azureadmin@x.y.z.t +``` diff --git a/environments/demo-azure-vm/dev/east-us/main.tf b/environments/demo-azure-vm/dev/east-us/main.tf new file mode 100644 index 0000000..457b113 --- /dev/null +++ b/environments/demo-azure-vm/dev/east-us/main.tf @@ -0,0 +1,8 @@ +module "demo_azure_vm" { + source = "../../../../modules/demo-azure-vm" + resource_group_location = var.resource_group_location + vm_size = var.vm_size + resource_group_name_prefix = var.resource_group_name_prefix + username = var.username + vm_hostname = var.vm_hostname +} diff --git a/environments/demo-azure-vm/dev/east-us/output.tf b/environments/demo-azure-vm/dev/east-us/output.tf new file mode 100644 index 0000000..18d9569 --- /dev/null +++ b/environments/demo-azure-vm/dev/east-us/output.tf @@ -0,0 +1,15 @@ +output "resource_group_name" { + value = module.demo_azure_vm.resource_group_name +} + +output "public_ip_address" { + value = module.demo_azure_vm.public_ip_address +} + +output "key_data" { + value = module.demo_azure_vm.key_data +} + +output "private_key_data" { + value = module.demo_azure_vm.private_key_data +} diff --git a/environments/demo-azure-vm/dev/east-us/variables.tf b/environments/demo-azure-vm/dev/east-us/variables.tf new file mode 100644 index 0000000..2fef91c --- /dev/null +++ b/environments/demo-azure-vm/dev/east-us/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + type = string + default = "DEV-rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "username" { + type = string + description = "The username for the local account that will be created on the new VM." + default = "azureadmin" +} + +variable "vm_size" { + type = string + description = "vm_size" + default = "Standard_DS1_v2" +} + +variable "vm_hostname" { + type = string + description = "vm_hostname" + default = "dev-azurevm" +} \ No newline at end of file diff --git a/environments/demo-azure-vm/production/east-us/README.md b/environments/demo-azure-vm/production/east-us/README.md new file mode 100644 index 0000000..9f7d390 --- /dev/null +++ b/environments/demo-azure-vm/production/east-us/README.md @@ -0,0 +1,35 @@ +## Deploy + +```bash +terraform init -upgrade + +terraform plan + +terraform apply +``` + +## Result + +- You can find the output information in the console + +``` +key_data = "xxxxxyyyyyzzzztttt generated-by-azure" +private_key_data = "zzzzzzzzzzzzzzzz" +public_ip_address = "x.y.z.t" +resource_group_name = "ENV-rg-some-thing" +``` + +## Connect to the VM + +```bash +# Save the private key *.pem +mkdir ssh_key +cd ssh_key +vi azure-vm-private-key.pem + +# It is required that your private key files are NOT accessible by others. +chmod 600 azure-vm-private-key.pem + +# Now SSH +ssh -i azure-vm-private-key.pem azureadmin@x.y.z.t +``` diff --git a/environments/demo-azure-vm/production/east-us/main.tf b/environments/demo-azure-vm/production/east-us/main.tf new file mode 100644 index 0000000..457b113 --- /dev/null +++ b/environments/demo-azure-vm/production/east-us/main.tf @@ -0,0 +1,8 @@ +module "demo_azure_vm" { + source = "../../../../modules/demo-azure-vm" + resource_group_location = var.resource_group_location + vm_size = var.vm_size + resource_group_name_prefix = var.resource_group_name_prefix + username = var.username + vm_hostname = var.vm_hostname +} diff --git a/environments/demo-azure-vm/production/east-us/output.tf b/environments/demo-azure-vm/production/east-us/output.tf new file mode 100644 index 0000000..18d9569 --- /dev/null +++ b/environments/demo-azure-vm/production/east-us/output.tf @@ -0,0 +1,15 @@ +output "resource_group_name" { + value = module.demo_azure_vm.resource_group_name +} + +output "public_ip_address" { + value = module.demo_azure_vm.public_ip_address +} + +output "key_data" { + value = module.demo_azure_vm.key_data +} + +output "private_key_data" { + value = module.demo_azure_vm.private_key_data +} diff --git a/environments/demo-azure-vm/production/east-us/variables.tf b/environments/demo-azure-vm/production/east-us/variables.tf new file mode 100644 index 0000000..f38e5b1 --- /dev/null +++ b/environments/demo-azure-vm/production/east-us/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + type = string + default = "PROD-rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "username" { + type = string + description = "The username for the local account that will be created on the new VM." + default = "azureadmin" +} + +variable "vm_size" { + type = string + description = "vm_size" + default = "Standard_DS1_v2" +} + +variable "vm_hostname" { + type = string + description = "vm_hostname" + default = "prod-azurevm" +} \ No newline at end of file diff --git a/environments/demo-azure-vm/staging/east-us/README.md b/environments/demo-azure-vm/staging/east-us/README.md new file mode 100644 index 0000000..9f7d390 --- /dev/null +++ b/environments/demo-azure-vm/staging/east-us/README.md @@ -0,0 +1,35 @@ +## Deploy + +```bash +terraform init -upgrade + +terraform plan + +terraform apply +``` + +## Result + +- You can find the output information in the console + +``` +key_data = "xxxxxyyyyyzzzztttt generated-by-azure" +private_key_data = "zzzzzzzzzzzzzzzz" +public_ip_address = "x.y.z.t" +resource_group_name = "ENV-rg-some-thing" +``` + +## Connect to the VM + +```bash +# Save the private key *.pem +mkdir ssh_key +cd ssh_key +vi azure-vm-private-key.pem + +# It is required that your private key files are NOT accessible by others. +chmod 600 azure-vm-private-key.pem + +# Now SSH +ssh -i azure-vm-private-key.pem azureadmin@x.y.z.t +``` diff --git a/environments/demo-azure-vm/staging/east-us/main.tf b/environments/demo-azure-vm/staging/east-us/main.tf new file mode 100644 index 0000000..457b113 --- /dev/null +++ b/environments/demo-azure-vm/staging/east-us/main.tf @@ -0,0 +1,8 @@ +module "demo_azure_vm" { + source = "../../../../modules/demo-azure-vm" + resource_group_location = var.resource_group_location + vm_size = var.vm_size + resource_group_name_prefix = var.resource_group_name_prefix + username = var.username + vm_hostname = var.vm_hostname +} diff --git a/environments/demo-azure-vm/staging/east-us/output.tf b/environments/demo-azure-vm/staging/east-us/output.tf new file mode 100644 index 0000000..18d9569 --- /dev/null +++ b/environments/demo-azure-vm/staging/east-us/output.tf @@ -0,0 +1,15 @@ +output "resource_group_name" { + value = module.demo_azure_vm.resource_group_name +} + +output "public_ip_address" { + value = module.demo_azure_vm.public_ip_address +} + +output "key_data" { + value = module.demo_azure_vm.key_data +} + +output "private_key_data" { + value = module.demo_azure_vm.private_key_data +} diff --git a/environments/demo-azure-vm/staging/east-us/variables.tf b/environments/demo-azure-vm/staging/east-us/variables.tf new file mode 100644 index 0000000..99700f2 --- /dev/null +++ b/environments/demo-azure-vm/staging/east-us/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + type = string + default = "STAGING-rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "username" { + type = string + description = "The username for the local account that will be created on the new VM." + default = "azureadmin" +} + +variable "vm_size" { + type = string + description = "vm_size" + default = "Standard_DS1_v2" +} + +variable "vm_hostname" { + type = string + description = "vm_hostname" + default = "staging-azurevm" +} \ No newline at end of file diff --git a/environments/demo-azure-vm/staging/southeastasia/README.md b/environments/demo-azure-vm/staging/southeastasia/README.md new file mode 100644 index 0000000..9f7d390 --- /dev/null +++ b/environments/demo-azure-vm/staging/southeastasia/README.md @@ -0,0 +1,35 @@ +## Deploy + +```bash +terraform init -upgrade + +terraform plan + +terraform apply +``` + +## Result + +- You can find the output information in the console + +``` +key_data = "xxxxxyyyyyzzzztttt generated-by-azure" +private_key_data = "zzzzzzzzzzzzzzzz" +public_ip_address = "x.y.z.t" +resource_group_name = "ENV-rg-some-thing" +``` + +## Connect to the VM + +```bash +# Save the private key *.pem +mkdir ssh_key +cd ssh_key +vi azure-vm-private-key.pem + +# It is required that your private key files are NOT accessible by others. +chmod 600 azure-vm-private-key.pem + +# Now SSH +ssh -i azure-vm-private-key.pem azureadmin@x.y.z.t +``` diff --git a/environments/demo-azure-vm/staging/southeastasia/main.tf b/environments/demo-azure-vm/staging/southeastasia/main.tf new file mode 100644 index 0000000..457b113 --- /dev/null +++ b/environments/demo-azure-vm/staging/southeastasia/main.tf @@ -0,0 +1,8 @@ +module "demo_azure_vm" { + source = "../../../../modules/demo-azure-vm" + resource_group_location = var.resource_group_location + vm_size = var.vm_size + resource_group_name_prefix = var.resource_group_name_prefix + username = var.username + vm_hostname = var.vm_hostname +} diff --git a/environments/demo-azure-vm/staging/southeastasia/output.tf b/environments/demo-azure-vm/staging/southeastasia/output.tf new file mode 100644 index 0000000..18d9569 --- /dev/null +++ b/environments/demo-azure-vm/staging/southeastasia/output.tf @@ -0,0 +1,15 @@ +output "resource_group_name" { + value = module.demo_azure_vm.resource_group_name +} + +output "public_ip_address" { + value = module.demo_azure_vm.public_ip_address +} + +output "key_data" { + value = module.demo_azure_vm.key_data +} + +output "private_key_data" { + value = module.demo_azure_vm.private_key_data +} diff --git a/environments/demo-azure-vm/staging/southeastasia/variables.tf b/environments/demo-azure-vm/staging/southeastasia/variables.tf new file mode 100644 index 0000000..80572ea --- /dev/null +++ b/environments/demo-azure-vm/staging/southeastasia/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + default = "southeastasia" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + type = string + default = "STAGING-southeastasiastage-rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "username" { + type = string + description = "The username for the local account that will be created on the new VM." + default = "azureadmin" +} + +variable "vm_size" { + type = string + description = "vm_size" + default = "Standard_DS1_v2" +} + +variable "vm_hostname" { + type = string + description = "vm_hostname" + default = "staging-southeastasia-azurevm" +} diff --git a/environments/demo-message/dev/east-us/backend.tf b/environments/demo-message/dev/east-us/backend.tf new file mode 100644 index 0000000..e69de29 diff --git a/environments/demo-message/dev/east-us/main.tf b/environments/demo-message/dev/east-us/main.tf new file mode 100644 index 0000000..76fa07b --- /dev/null +++ b/environments/demo-message/dev/east-us/main.tf @@ -0,0 +1,4 @@ +module "demo" { + source = "../../../../modules/demo-message" + message = var.demo_message +} diff --git a/environments/demo-message/dev/east-us/output.tf b/environments/demo-message/dev/east-us/output.tf new file mode 100644 index 0000000..300be72 --- /dev/null +++ b/environments/demo-message/dev/east-us/output.tf @@ -0,0 +1,4 @@ +output "demo_message_output" { + description = "Output from the demo module in the Dev environment" + value = module.demo.demo_output_message +} diff --git a/environments/demo-message/dev/east-us/variables.tf b/environments/demo-message/dev/east-us/variables.tf new file mode 100644 index 0000000..d74f45b --- /dev/null +++ b/environments/demo-message/dev/east-us/variables.tf @@ -0,0 +1,4 @@ +variable "demo_message" { + description = "Message for the demo module in the Dev environment" + default = "Hello from Dev Environment!" +} diff --git a/environments/demo-message/production/east-us/backend.tf b/environments/demo-message/production/east-us/backend.tf new file mode 100644 index 0000000..e69de29 diff --git a/environments/demo-message/production/east-us/main.tf b/environments/demo-message/production/east-us/main.tf new file mode 100644 index 0000000..76fa07b --- /dev/null +++ b/environments/demo-message/production/east-us/main.tf @@ -0,0 +1,4 @@ +module "demo" { + source = "../../../../modules/demo-message" + message = var.demo_message +} diff --git a/environments/demo-message/production/east-us/output.tf b/environments/demo-message/production/east-us/output.tf new file mode 100644 index 0000000..2a64dc0 --- /dev/null +++ b/environments/demo-message/production/east-us/output.tf @@ -0,0 +1,4 @@ +output "demo_message_output" { + description = "Output from the demo module in the Production environment" + value = module.demo.demo_output_message +} diff --git a/environments/demo-message/production/east-us/variables.tf b/environments/demo-message/production/east-us/variables.tf new file mode 100644 index 0000000..cc66698 --- /dev/null +++ b/environments/demo-message/production/east-us/variables.tf @@ -0,0 +1,4 @@ +variable "demo_message" { + description = "Message for the demo module in the Production environment" + default = "Hello from Production Environment!" +} diff --git a/environments/demo-message/staging/east-us/backend.tf b/environments/demo-message/staging/east-us/backend.tf new file mode 100644 index 0000000..e69de29 diff --git a/environments/demo-message/staging/east-us/main.tf b/environments/demo-message/staging/east-us/main.tf new file mode 100644 index 0000000..76fa07b --- /dev/null +++ b/environments/demo-message/staging/east-us/main.tf @@ -0,0 +1,4 @@ +module "demo" { + source = "../../../../modules/demo-message" + message = var.demo_message +} diff --git a/environments/demo-message/staging/east-us/output.tf b/environments/demo-message/staging/east-us/output.tf new file mode 100644 index 0000000..6e1564d --- /dev/null +++ b/environments/demo-message/staging/east-us/output.tf @@ -0,0 +1,4 @@ +output "demo_message_output" { + description = "Output from the demo module in the Staging environment" + value = module.demo.demo_output_message +} diff --git a/environments/demo-message/staging/east-us/variables.tf b/environments/demo-message/staging/east-us/variables.tf new file mode 100644 index 0000000..3df72be --- /dev/null +++ b/environments/demo-message/staging/east-us/variables.tf @@ -0,0 +1,4 @@ +variable "demo_message" { + description = "Message for the demo module in the Staging environment" + default = "Hello from Staging Environment!" +} diff --git a/modules/demo-azure-vm/README.md b/modules/demo-azure-vm/README.md new file mode 100644 index 0000000..4d708b3 --- /dev/null +++ b/modules/demo-azure-vm/README.md @@ -0,0 +1,3 @@ +# Module to deploy VM on Azure + +- Reference: https://learn.microsoft.com/en-us/azure/virtual-machines/linux/quick-create-terraform?tabs=azure-cli diff --git a/modules/demo-azure-vm/main.tf b/modules/demo-azure-vm/main.tf new file mode 100644 index 0000000..d4830d1 --- /dev/null +++ b/modules/demo-azure-vm/main.tf @@ -0,0 +1,124 @@ +resource "random_pet" "rg_name" { + prefix = var.resource_group_name_prefix +} + +resource "azurerm_resource_group" "rg" { + location = var.resource_group_location + name = random_pet.rg_name.id +} + +# Create virtual network +resource "azurerm_virtual_network" "my_terraform_network" { + name = "myVnet" + address_space = ["10.0.0.0/16"] + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name +} + +# Create subnet +resource "azurerm_subnet" "my_terraform_subnet" { + name = "mySubnet" + resource_group_name = azurerm_resource_group.rg.name + virtual_network_name = azurerm_virtual_network.my_terraform_network.name + address_prefixes = ["10.0.1.0/24"] +} + +# Create public IPs +resource "azurerm_public_ip" "my_terraform_public_ip" { + name = "myPublicIP" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + allocation_method = "Dynamic" +} + +# Create Network Security Group and rule +resource "azurerm_network_security_group" "my_terraform_nsg" { + name = "myNetworkSecurityGroup" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + + security_rule { + name = "SSH" + priority = 1001 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_range = "22" + source_address_prefix = "*" + destination_address_prefix = "*" + } +} + +# Create network interface +resource "azurerm_network_interface" "my_terraform_nic" { + name = "myNIC" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + + ip_configuration { + name = "my_nic_configuration" + subnet_id = azurerm_subnet.my_terraform_subnet.id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.my_terraform_public_ip.id + } +} + +# Connect the security group to the network interface +resource "azurerm_network_interface_security_group_association" "example" { + network_interface_id = azurerm_network_interface.my_terraform_nic.id + network_security_group_id = azurerm_network_security_group.my_terraform_nsg.id +} + +# Generate random text for a unique storage account name +resource "random_id" "random_id" { + keepers = { + # Generate a new ID only when a new resource group is defined + resource_group = azurerm_resource_group.rg.name + } + + byte_length = 8 +} + +# Create storage account for boot diagnostics +resource "azurerm_storage_account" "my_storage_account" { + name = "diag${random_id.random_id.hex}" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + account_tier = "Standard" + account_replication_type = "LRS" +} + +# Create virtual machine +resource "azurerm_linux_virtual_machine" "my_terraform_vm" { + name = "myVM" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + network_interface_ids = [azurerm_network_interface.my_terraform_nic.id] + size = var.vm_size + + os_disk { + name = "myOsDisk" + caching = "ReadWrite" + storage_account_type = "Premium_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "0001-com-ubuntu-server-jammy" + sku = "22_04-lts-gen2" + version = "latest" + } + + computer_name = var.vm_hostname + admin_username = var.username + + admin_ssh_key { + username = var.username + public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey + } + + boot_diagnostics { + storage_account_uri = azurerm_storage_account.my_storage_account.primary_blob_endpoint + } +} diff --git a/modules/demo-azure-vm/outputs.tf b/modules/demo-azure-vm/outputs.tf new file mode 100644 index 0000000..13d7de3 --- /dev/null +++ b/modules/demo-azure-vm/outputs.tf @@ -0,0 +1,12 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "public_ip_address" { + value = azurerm_linux_virtual_machine.my_terraform_vm.public_ip_address +} + +## Existed in ssh.tf. Check and migrate if needed +# output "key_data" { +# value = azapi_resource_action.ssh_public_key_gen.output.publicKey +# } diff --git a/modules/demo-azure-vm/providers.tf b/modules/demo-azure-vm/providers.tf new file mode 100644 index 0000000..1e65dda --- /dev/null +++ b/modules/demo-azure-vm/providers.tf @@ -0,0 +1,22 @@ +terraform { + required_version = ">=0.12" + + required_providers { + azapi = { + source = "azure/azapi" + version = "~>1.5" + } + azurerm = { + source = "hashicorp/azurerm" + version = "~>2.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} + +provider "azurerm" { + features {} +} diff --git a/modules/demo-azure-vm/ssh.tf b/modules/demo-azure-vm/ssh.tf new file mode 100644 index 0000000..cef9c61 --- /dev/null +++ b/modules/demo-azure-vm/ssh.tf @@ -0,0 +1,28 @@ +resource "random_pet" "ssh_key_name" { + prefix = "ssh" + separator = "" +} + +resource "azapi_resource_action" "ssh_public_key_gen" { + type = "Microsoft.Compute/sshPublicKeys@2022-11-01" + resource_id = azapi_resource.ssh_public_key.id + action = "generateKeyPair" + method = "POST" + + response_export_values = ["publicKey", "privateKey"] +} + +resource "azapi_resource" "ssh_public_key" { + type = "Microsoft.Compute/sshPublicKeys@2022-11-01" + name = random_pet.ssh_key_name.id + location = azurerm_resource_group.rg.location + parent_id = azurerm_resource_group.rg.id +} + +output "key_data" { + value = azapi_resource_action.ssh_public_key_gen.output.publicKey +} + +output "private_key_data" { + value = azapi_resource_action.ssh_public_key_gen.output.privateKey +} diff --git a/modules/demo-azure-vm/variables.tf b/modules/demo-azure-vm/variables.tf new file mode 100644 index 0000000..927bd66 --- /dev/null +++ b/modules/demo-azure-vm/variables.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + type = string + default = "rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "username" { + type = string + description = "The username for the local account that will be created on the new VM." + default = "azureadmin" +} + +variable "vm_size" { + type = string + description = "vm_size" + default = "Standard_DS1_v2" +} + +variable "vm_hostname" { + type = string + description = "vm_hostname" + default = "azurevm" +} diff --git a/modules/demo-message/main.tf b/modules/demo-message/main.tf new file mode 100644 index 0000000..8b75625 --- /dev/null +++ b/modules/demo-message/main.tf @@ -0,0 +1,5 @@ +resource "null_resource" "demo_message" { + provisioner "local-exec" { + command = "echo ${var.message}" + } +} diff --git a/modules/demo-message/outputs.tf b/modules/demo-message/outputs.tf new file mode 100644 index 0000000..98dbdcd --- /dev/null +++ b/modules/demo-message/outputs.tf @@ -0,0 +1,4 @@ +output "demo_output_message" { + description = "The message passed to the demo module" + value = var.message +} diff --git a/modules/demo-message/variables.tf b/modules/demo-message/variables.tf new file mode 100644 index 0000000..3d322ea --- /dev/null +++ b/modules/demo-message/variables.tf @@ -0,0 +1,4 @@ +variable "message" { + description = "Message to display from the demo module" + type = string +} diff --git a/utils/deploy-something/main.tf b/utils/deploy-something/main.tf new file mode 100644 index 0000000..9a793ad --- /dev/null +++ b/utils/deploy-something/main.tf @@ -0,0 +1 @@ +# Free resource provisioning without structuring