Skip to content

02. Короче, Склифософский!

svs1370 edited this page Nov 8, 2019 · 8 revisions

«Короче, Склифосовский!»

Данный раздел предназначен для тех, кто хорошо знаком с системой Terraform, а также имеет представление об основных понятиях и способах авторизации в облачной платформе DECS.

Ниже приведён подробно откомментированный пример, показывающий, как создать виртуальную машину в облачной платформе DECS с помощью соответствующего Terraform провайдера.

Идентификаторы виртуального облачного пространства (aka Resource Group), в котором будет создана виртуальная машина, и образ операционной системы, на базе которого она должна быть создана, считываются из облачной платформы с помощью Source data функций decs_resgroup и decs_osimage соответственно. Виртуальная машина создаётся посредством Resource функции decs_vm.

Только авторизованные в контроллере облачной платформы пользователи могут управлять облачными ресурсами. Подробнее о способах авторизации см. Обзор облачной платформы DECS.

# 
# 1) Setup DECS Terraform provider and authenticate to DECS cloud controller
# Only properly authenticated user can communicate with resource management
# engine of the cloud platform.  
#
# Make sure that environment variables DECS_APP_ID and DECS_APP_SECRET are
# properly set prior to calling Terraform. Otherwise you may specify them here
# as "app_id" and "app_secret" parameters, but it is not adviseable from security 
# perspective.
#
provider "decs" {
   authenticator = "oauth2"
   controller_url = "https://mr4.digitalenergy.online"
   oauth2_url = "https://sso.digitalenergy.online"
}

#
# 2) Obtain info about the resource group, in which we will create a new VM.
# Resource group ID is one of mandatory parameters for new VM provisioning.
#
# Here we get info about _existing_ group named "test" owned by account 
# (aka tenant) "MyTenant".
# To create a new resource group, use: 
# resource "decs_resgroup" "name_of_the_new_group" {
#  ... }
#
data "decs_resgroup" "target_rg" {
  name = "test"
  tenant = "MyTenant" # this is the account name, where resgroup named "test" is found
}
# Once the above directive completes, resource group ID will be accessible 
# as "${data.decs_resgroup.target_rg.id}"

# 
# 3) Obtain OS Image information. Image ID is required to create a new VM.
#
# Note that:
#  - image name is specified verbatim with all spaces and upper/lowercase 
#    letters (e.g.  "Ubuntu 16.04 v1.2.3").
#
data "decs_image" "ubuntu_img" {
  name = "Ubuntu 16.04 v1.2.3"
}
# once the above directive completes, image ID will be accessible 
# as "${data.decs_image.ubuntu_img.id}"

#
# Now that we have all necessary prerequisites (the Resource Group ID, OS image ID)
# we can create the VM
#

#
# 4) Provision a VM in resource group identified by resource variable "target_rg" 
# with the following specs:
#  - name "name_of_the_vm_in_the_cloud_platform"
#  - vCPU count - see specs
#  - RAM size in MB - see specs
#  - boot disk of name "boot" and size in GB - see specs
# -  port forwarding rules to set up external SSH access to the VM
#  - description "human readable and useful description of this VM"
# 

###########################
#
# My Fist Terraform-managed VM
#
resource "decs_vm" "tf-managed-vm-01" {
   name = "tf-managed-vm-01"
   rgid = "${data.decs_resgroup.target_rg.id}"
   cpu = 2       # CPU count
   ram = 4096    # RAM size in MB
   boot_disk {
      label = "boot"
      size = 20  # Boot disk size in GB
   }
   port_forwards { # set up port forwards to allow SSH access
      ext_port = 11122
      int_port = 22
      proto = "tcp"
   }
   ssh_keys {      # authorize SSH key to user root
      user="root"
      public_key="<paste you SSH public key here>"
   }
   image_id = "${data.decs_image.ubuntu_img.id}"
   description = "Test VM 01 managed by Terraform"
}