Skip to content

Refactoring #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ jobs:
- name: Ensure code is formatted
run: terraform fmt -diff -check -recursive

- name: Terraform Init
run: terraform init -backend=false

- name: Terraform Validate
run: terraform validate

- uses: actions/cache@v4
name: Cache TFLint plugin dir
with:
Expand All @@ -51,6 +45,21 @@ jobs:
- name: Run TFLint
run: tflint

- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Terraform Init
run: terraform init -backend=false

- name: Terraform Plan
run: |
terraform plan -input=false -refresh=false

- name: Terraform Validate
run: terraform validate

- name: Pull Checkov image
run: docker pull bridgecrew/checkov:latest

Expand Down
86 changes: 50 additions & 36 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
locals {
ubuntu_vm_network_security_rules = [
{
name = "Allow-HTTP-From-Internet-To-VM"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = 80
source_address_prefix = "Internet"
destination_address_prefix = "10.0.0.10/32"
},
{
name = "Allow-SSH-From-Internet-To-VM"
priority = 200
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = 22
source_address_prefix = "Internet"
destination_address_prefix = "10.0.0.10/32"
}
]

tags = {
creator = "terraform"
environment = "playground"
Expand Down Expand Up @@ -28,7 +53,7 @@ resource "azurerm_subnet" "this" {
address_prefixes = ["10.0.0.0/28"]
}

resource "azurerm_public_ip" "vm_ubuntu" {
resource "azurerm_public_ip" "ubuntu_vm" {
name = "pip-ubuntu"
resource_group_name = azurerm_resource_group.playground.name
location = azurerm_resource_group.playground.location
Expand All @@ -40,7 +65,7 @@ resource "azurerm_public_ip" "vm_ubuntu" {
tags = local.tags
}

resource "azurerm_network_interface" "vm_ubuntu" {
resource "azurerm_network_interface" "ubuntu_vm" {
name = "nic-ubuntu"
location = azurerm_virtual_network.this.location
resource_group_name = azurerm_subnet.this.resource_group_name
Expand All @@ -51,61 +76,50 @@ resource "azurerm_network_interface" "vm_ubuntu" {
private_ip_address_version = "IPv4"
private_ip_address_allocation = "Static"
private_ip_address = "10.0.0.10"
public_ip_address_id = azurerm_public_ip.vm_ubuntu.id
public_ip_address_id = azurerm_public_ip.ubuntu_vm.id
}

tags = local.tags
}

module "vm_ubuntu" {
module "ubuntu_vm" {
source = "./modules/linux_virtual_machine/"

name = "vm-ubuntu"
resource_group_name = azurerm_network_interface.vm_ubuntu.resource_group_name
location = azurerm_network_interface.vm_ubuntu.location
resource_group_name = azurerm_network_interface.ubuntu_vm.resource_group_name
location = azurerm_network_interface.ubuntu_vm.location
size = "Standard_B2s"
admin_username = "wozorio"
network_interface_ids = [azurerm_network_interface.vm_ubuntu.id]
network_interface_ids = [azurerm_network_interface.ubuntu_vm.id]

tags = local.tags
}

module "nsg_vm_ubuntu" {
source = "./modules/network_security_group/"

resource "azurerm_network_security_group" "ubuntu_vm" {
name = "nsg-ubuntu"
location = azurerm_resource_group.playground.location
resource_group_name = azurerm_resource_group.playground.name

security_rules = [
{
name = "Allow-HTTP-From-Internet-To-VM"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = 80
source_address_prefix = "Internet"
destination_address_prefix = "10.0.0.10/32"
},
{
name = "Allow-SSH-From-Internet-To-VM"
priority = 200
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = 22
source_address_prefix = "Internet"
destination_address_prefix = "10.0.0.10/32"
dynamic "security_rule" {
for_each = local.ubuntu_vm_network_security_rules

content {
name = security_rule.value.name
priority = security_rule.value.priority
direction = security_rule.value.direction
access = security_rule.value.access
protocol = security_rule.value.protocol
source_port_range = security_rule.value.source_port_range
destination_port_range = security_rule.value.destination_port_range
source_address_prefix = security_rule.value.source_address_prefix
destination_address_prefix = security_rule.value.destination_address_prefix
}
]
}

tags = local.tags
}

resource "azurerm_network_interface_security_group_association" "vm_ubuntu" {
network_interface_id = azurerm_network_interface.vm_ubuntu.id
network_security_group_id = module.nsg_vm_ubuntu.id
resource "azurerm_network_interface_security_group_association" "ubuntu_vm" {
network_interface_id = azurerm_network_interface.ubuntu_vm.id
network_security_group_id = azurerm_network_security_group.ubuntu_vm.id
}
6 changes: 1 addition & 5 deletions providers.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
terraform {
backend "azurerm" {
storage_account_name = "stterraformwozorio"
container_name = "tfstate"
key = "playground.tfstate"
}
backend "azurerm" {}
}

provider "azurerm" {
Expand Down
Loading