GithubHelp home page GithubHelp logo

armankoradia / azure-getting-started-with-terraform Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 47.0 18 KB

Created new repo for Linux Academy guide on "A complete Azure environment with Terraform"

PHP 6.23% HCL 81.00% Shell 12.76%

azure-getting-started-with-terraform's People

Contributors

armankoradia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

azure-getting-started-with-terraform's Issues

How to reference values from imported resource in your terraform code?

Hi there,

I need some assistance on how to reference the values from the objects imported by "terraform import"

Let us start from the beginning of the life. You can reproduce this at your end too.

Input: Resource group, Virtual network and Virtual subnet created in Azure Portal.

Question to the instructor: Do I have to import each resource? or Just importing virtual network is sufficient? The reason I ask is when I import virtual network, I see resource group and subnet(s) so why to import them again? Or is there an explicit need to import each resource so that terraform destroy won't destroy them? Please clarify

Expected Outcome: Be able to spin up a Virtual machine with NIC card, storage account, managed disk, network security group.

Step 1. There exist no .terraform directory and no terraform.tfstate file, Let us examine the basic terraform state file.

[[email protected] /data/OnlyOnMyPC/terraform_iac/win_2016_datacentre_vm/aos-1.v3]ls -l
total 12
-rwxrwx---. 1 root vboxsf 1564 Oct 26 08:56 aos-1.tf
-rwxrwx---. 1 root vboxsf 1577 Oct 11 22:07 reuse_import
-rwxrwx---. 1 root vboxsf 642 Oct 21 23:46 terraform.tfvars
[[email protected] /data/OnlyOnMyPC/terraform_iac/win_2016_datacentre_vm/aos-1.v3]

##################################################################################

VARIABLES

##################################################################################
variable "ARM_SUBSCRIPTION_ID" {}

variable "ARM_CLIENT_ID" {}

variable "ARM_CLIENT_SECRET" {}

variable "ARM_TENANT_ID" {}

variable "Resource_group_name" {}

variable "Location" {}

variable "automation_account_name" {}

variable "virtual_network_name" {}

variable "address_space" {
type = "list"
}
variable "address_prefix" {
type = "list"
}

variable "virtual_network_subnet" {}

variable "virtual_network_subnet_name" {}

variable "virtual_network_nic" {

}
variable "virtual_machine_name" {}

variable "vm-hostname" {}

variable "vm-username" {}

variable "vm-password" {}

##################################################################################

PROVIDERS

##################################################################################

provider "azurerm" {
subscription_id="${var.ARM_SUBSCRIPTION_ID}"
client_id="${var.ARM_CLIENT_ID}"
client_secret="${var.ARM_CLIENT_SECRET}"
tenant_id="${var.ARM_TENANT_ID}"
}

##################################################################################

Creating Resource Group

##################################################################################
data "azurerm_resource_group" "tf_resource_group" {
name = "test_resource_group"
}

resource "azurerm_virtual_network" "test_virtual_network" {
}

  1. I run terraform import to now import the test_virtual_network resource by the following command:

terraform import azurerm_virtual_network.test_virtual_network /subscriptions/06c532b0-b91a-42da-9fb9-9d5a0f8efe20/resourceGroups/test_resource_group/providers/Microsoft.Network/virtualNetworks/test_virtual_network

azurerm_virtual_network.test_virtual_network: Import complete!
Imported azurerm_virtual_network (ID: /subscriptions/06c532b0-b91a-42da-9fb9-9d5a0f8efe20/resourceGroups/test_resource_group/providers/Microsoft.Network/virtualNetworks/test_virtual_network)
azurerm_virtual_network.test_virtual_network: Refreshing state... (ID: /subscriptions/06c532b0-b91a-42da-9fb9-...k/virtualNetworks/test_virtual_network)

Import Successful!

The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.

  1. I now run terraform plan, now, terraform is asking me for address_value! But address_value, resource_group, subnet, all of the required information is already in the terraform state file!

terraform plan
var.address_prefix
Enter a value: ^C
Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...

Error: Error asking for user input: Error asking for address_prefix: interrupted

[[email protected] /data/OnlyOnMyPC/terraform_iac/win_2016_datacentre_vm/aos-1.v3]

Considering that I might need to import 100+ resources, it is not practical to input their values all over again, so I'm wondering how can I reference the values from terraform state show

[[email protected] /data/OnlyOnMyPC/terraform_iac/win_2016_datacentre_vm/aos-1.v3]terraform state list
azurerm_virtual_network.test_virtual_network
[[email protected] /data/OnlyOnMyPC/terraform_iac/win_2016_datacentre_vm/aos-1.v3]terraform state show
id = /subscriptions/06c532b0-b91a-42da-9fb9-9d5a0f8efe20/resourceGroups/test_resource_group/providers/Microsoft.Network/virtualNetworks/test_virtual_network
address_space.# = 1
address_space.0 = 10.197.0.0/16
dns_servers.# = 0
location = australiasoutheast
name = test_virtual_network
resource_group_name = test_resource_group
subnet.# = 2
subnet.27872635.address_prefix = 10.197.2.0/24
subnet.27872635.id = /subscriptions/06c532b0-b91a-42da-9fb9-9d5a0f8efe20/resourceGroups/test_resource_group/providers/Microsoft.Network/virtualNetworks/test_virtual_network/subnets/test_subnet2
subnet.27872635.name = test_subnet2
subnet.27872635.security_group =
subnet.3408639592.address_prefix = 10.197.1.0/24
subnet.3408639592.id = /subscriptions/06c532b0-b91a-42da-9fb9-9d5a0f8efe20/resourceGroups/test_resource_group/providers/Microsoft.Network/virtualNetworks/test_virtual_network/subnets/test_subnet
subnet.3408639592.name = test_subnet
subnet.3408639592.security_group =
tags.% = 0

  1. I now want to spin up a VM in test_subnet and test_subnet2, how do I do that?

Example: https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html

variable "prefix" {
default = "tfvmex"
}

resource "azurerm_resource_group" "main" { I don't think I have to create as I terraform import has already imported the subnet, correct ?
name = "${var.prefix}-resources"
location = "West US 2"
}

resource "azurerm_virtual_network" "main" { I don't think I have to create as terraform import has imported the virtual network, correct?
name = "${var.prefix}-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
}

resource "azurerm_subnet" "internal" { I don't think I have to create as terraform import has already imported the subnet, correct?
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}

resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}" # How to refrence to my location which exist in terraform state file?
resource_group_name = "${azurerm_resource_group.main.name}"

ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}" # How to refrence to subnet which exist in my terraform state file?
private_ip_address_allocation = "dynamic"
}
}

resource "azurerm_virtual_machine" "main" {
name = "${var.prefix}-vm"
location = "${azurerm_resource_group.main.location}" How to refrence to the location? it exist in state file
resource_group_name = "${azurerm_resource_group.main.name}" How to refrence to the resource group name, it exist in state file
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS1_v2"

Uncomment this line to delete the OS disk automatically when deleting the VM

delete_os_disk_on_termination = true

Uncomment this line to delete the data disks automatically when deleting the VM

delete_data_disks_on_termination = true

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}

os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}

os_profile_linux_config {
disable_password_authentication = false
}

tags {
environment = "staging"
}
}

Would greatly appreciate your assistance and guidance.

Many thanks,
Ameya Agashe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.