Terraform Manifest StructureBeta
The Terraform manifest describes the infrastructure configuration for deploying an instance of an image-based application in Marketplace. The description language is HashiCorp Configuration Language (HCL).
The manifest uses the following elements:
variables— input variables for resource configuration.resources— resources that allow managing infrastructure objects. For example, creating a VM. When describing resources, dependencies can be specified using thedepends onblock. A resource will not be created if at least one dependency is not met.data-sources— data sources that allow obtaining specific information from the Terraform provider, for example, getting available VM types or getting script execution results.outputs— output parameters, the results of the manifest execution.
Name | Description | Designation in the manifest |
|---|---|---|
VK CS | Contains resources and data sources for describing service infrastructure. The full list of resources and data sources is provided in the provider's official documentation. Examples of manifests describing resource creation are provided in the how-to guides for using Terraform |
|
VK CS Infra (iVK CS) | Contains resources and data sources that extend the capabilities of the VK CS provider, for example, allowing to track the state of a service instance, run scripts, and use their results during deployment. Bash and Python are supported for scripts. Resources and data sources are described in the VK CS Infra (iVK CS) provider reference |
|
Null | The provider's resources are used to launch external processes or actions not directly related to infrastructure resources |
|
Random | The provider's resources are used to generate passwords for accessing the service instance. For more details, see the provider's official documentation |
|
Time | The provider's resources are used to configure time-based interactions between other resources, for example, to create a pause or a timestamp |
|
The diagram of a Terraform manifest describing the steps of deploying a service on a VM in VK Cloud:
The specific steps of service deployment depend on the infrastructure required for your service and its settings.
The main components of a Terraform manifest:
- Description of input variables for resource configuration, including the special
instance_uuidvariable that contains the service deployment identifier. - Description of the service deployment infrastructure.
- Description of output parameters.
Optional components that are described using iVK CS provider resources:
-
- Executing scripts during service deployment or reinstallation.
- Using script execution results by other manifest resources.
- Retrieving script execution results.
-
Using monitoring during service deployment or reinstallation.
-
Using the DNS of the VK Cloud platform.
-
Creating VK Object Storage buckets. The created buckets will be displayed in the VK Cloud personal dashboard.
-
Rebooting a VM during service deployment.
Below is a manifest for deploying the Grafana service on a single VM using:
- external IP addresses;
- the cloud platform DNS;
- address availability monitoring.
# --------------------variables--------------------# Special variables# Service deployment identifiervariable "instance_uuid" {type = stringdefault = "4a57a965-3c83-436c-80e2-428e421538cc"}variable "email" {type = stringdefault = "user@example.com"}# External variables# Placement with an external or internal IP addressvariable "grafana_placement" {type = stringdefault = "internal"}# Backupvariable "backup_style" {type = stringdefault = "s3"}# Availability zonevariable "ds-az" {type = stringdefault = "ME1"}# VM typevariable "ds-flavor" {type = stringdefault = "a493b27d-170d-48eb-a24b-99e9b325f988"}# Subnet identifiervariable "ds-subnet" {type = stringdefault = "cd4224ac-0527-4291-a8e0-afae0cee02ed"}# Root disk typevariable "root_type" {type = stringdefault = "ceph-ssd"}# Root disk size, GBvariable "root_size" {type = numberdefault = 10}# Data storage disk typevariable "data_type" {type = stringdefault = "ceph-ssd"}# Data storage disk size, GBvariable "data_size" {type = numberdefault = 1}# Service image identifiervariable "image_uuid" {type = stringdefault = "8c7a6443-bb79-4f04-884a-14231f0ba6cb"description = "grafana image"}# Access ports for the security groupvariable "ports" {type = list(number)default = [22, 80, 443]description = "ports for secgroup rule. grafana [80, 443]"}locals {# Shortened version of instance_uuidshort_name = substr(var.instance_uuid, 0, 8)# Hostname generationhosts_name = "${local.short_name}-grafana"}# ---------------------data------------------------# Retrieving virtual network datadata "vkcs_networking_subnet" subnet {id = var.ds-subnet}# --------------------backup-----------------------# Creating S3 bucketsresource "ivkcs_s3" "s3_backup" {count = var.backup_style == "s3" ? 1 : 0name = "${local.hosts_name}-backup"}# --------------------security group---------------# Creating a security groupresource "vkcs_networking_secgroup" "secgroup" {name = "${local.short_name}-grafana"sdn = data.vkcs_networking_subnet.subnet.sdn}# Rules for the security groupresource "vkcs_networking_secgroup_rule" "rules" {count = length(var.ports)# Defining the direction of rule application — for incoming (ingress) or outgoing (egress) connectionsdirection = "ingress"# List of access portsport_range_max = element(var.ports, count.index)port_range_min = element(var.ports, count.index)# Access protocolprotocol = "tcp"# Remote network prefixremote_ip_prefix = "0.0.0.0/0"# Identifier of the security group for which the rules are createdsecurity_group_id = vkcs_networking_secgroup.secgroup.iddescription = "rule_tcp_${element(var.ports, count.index)}"}# --------------------network----------------------# Binding an IP address to a portresource "vkcs_networking_port" "ports" {name = "${local.short_name}-grafana"admin_state_up = "true"network_id = data.vkcs_networking_subnet.subnet.network_idsdn = data.vkcs_networking_subnet.subnet.sdnsecurity_group_ids = [vkcs_networking_secgroup.secgroup.id]fixed_ip {subnet_id = var.ds-subnet}}# --------------------keypair----------------------# Creating a key pairresource "ivkcs_ssh_keypair" "keypair" {}# --------------------------vm---------------------# Creating a cloud-config configuration. Retrieving data for initializing the agent on the hostresource "ivkcs_user_data" "user_data" {# Service deployment identifieruuid = var.instance_uuidhosts = [local.hosts_name]target_os = "almalinux9"# Keys for SSH accessssh_authorized_keys = [ivkcs_ssh_keypair.keypair.public_key,]}# Creating a VMresource "vkcs_compute_instance" "grafana" {name = local.hosts_nameflavor_id = var.ds-flavorsecurity_groups = [vkcs_networking_secgroup.secgroup.name]availability_zone = var.ds-azmetadata = { "sid" : "xaas", "product" : "grafana" }# Root diskblock_device {source_type = "volume"destination_type = "volume"boot_index = 0uuid = vkcs_blockstorage_volume.boot.id}# Applying the cloud-config configuration for VM setup. Installing the agentuser_data = ivkcs_user_data.user_data.user_data[0]# Attaching an IP address to the VMnetwork {port = vkcs_networking_port.ports.id}# Attempting to stop the VM before deletionstop_before_destroy = true# VM creation timeouttimeouts {create = "10m"}}# --------------------volume-----------------------# Creating the root diskresource "vkcs_blockstorage_volume" "boot" {name = "${local.short_name}-grafana-boot"# Metadatametadata = { "sid" : "xaas", "product" : "grafana" }# Service image identifierimage_id = var.image_uuidvolume_type = var.root_typesize = var.root_sizeavailability_zone = var.ds-az}# Creating the data diskresource "vkcs_blockstorage_volume" "grafana_data" {name = "${local.short_name}-grafana-data"# Metadatametadata = { "sid" : "xaas", "product" : "grafana" }size = var.data_sizeavailability_zone = var.ds-azvolume_type = var.data_type}# Attaching the data disk to the VMresource "vkcs_compute_volume_attach" "attached" {instance_id = vkcs_compute_instance.grafana.idvolume_id = vkcs_blockstorage_volume.grafana_data.id}# -----------------------external------------------# Retrieving the pool of external IP addressesresource "vkcs_networking_floatingip" "fips" {count = var.grafana_placement == "external" ? 1 : 0pool = data.vkcs_networking_subnet.subnet.sdn == "neutron" ? "ext-net" : "internet"}# Assigning an external IP address to the VMresource "vkcs_compute_floatingip_associate" "fip" {count = length(vkcs_networking_floatingip.fips)floating_ip = vkcs_networking_floatingip.fips[count.index].addressinstance_id = vkcs_compute_instance.grafana.id}# Creating an A record in the cloud platform DNSresource "ivkcs_dns" "grafana" {count = length(vkcs_networking_floatingip.fips)name = "grafana-${local.short_name}"domain = "xaas.msk.vkcs.cloud"ip = vkcs_networking_floatingip.fips[count.index].address}# --------------------agent-run--------------------locals {grafana_domain = var.grafana_placement == "external" ? "${ivkcs_dns.grafana[0].name}.${ivkcs_dns.grafana[0].domain}" : vkcs_compute_instance.grafana.access_ip_v4grafana_root_url = var.grafana_placement == "external" ? "https://${local.grafana_domain}" : "http://${local.grafana_domain}"# Startup script for the ivkcs_agent_exec.start resourcestart = <<-EOT#!/bin/bashansible-playbook start.yml \--extra-vars "lego_enabled=${var.grafana_placement == "external" ? "true" : "false"}" \--extra-vars "lego_email=${var.email}" \--extra-vars "backup_enabled=${var.backup_style == "s3" ? "true" : "false"}" \--extra-vars "backup_access_token=${try(ivkcs_s3.s3_backup[0].access, "n/a")}" \--extra-vars "backup_secret_token=${try(ivkcs_s3.s3_backup[0].secret, "n/a")}" \--extra-vars "backup_bucket_name=${try(ivkcs_s3.s3_backup[0].name, "n/a")}_bucket" \--extra-vars "grafana_domain=${local.grafana_domain}" \--extra-vars "grafana_root_url=${local.grafana_root_url}"EOT}resource "ivkcs_agent_exec" "start" {hosts = [local.hosts_name]name = "start_grafana"uuid = var.instance_uuidstep {index = 1type = "bash"content = local.startoptions {timeout = "20m"cwd = "/opt/playbooks"attempts = 1}}depends_on = [vkcs_compute_instance.grafana,vkcs_compute_volume_attach.attached,]}# --------------------health check-----------------# VM monitoringresource "ivkcs_agent_check" "health" {hosts = [local.hosts_name]uuid = var.instance_uuid# Monitoring by addresshttp_health {method = "GET"protocol = var.grafana_placement == "internal" ? "http" : "https"host = local.grafana_domainpath = "/api/health"http_codes = [200]period = "30s"}timeouts {create = "5m"}depends_on = [ivkcs_agent_exec.start,]}# --------------------outputs----------------------# Outputting the private SSH key for VM accessoutput "keypair" {value = ivkcs_ssh_keypair.keypair.private_key# The output parameter contains sensitive datasensitive = true}# Outputting the Grafana URLoutput "grafana_url" {value = local.grafana_root_url}# Outputting the S3 bucket identifieroutput "s3_backup" {value = try(ivkcs_s3.s3_backup[0].id, "n/a")}