Ops
Search…
Terraform
Terraform is a declarative way of setup your infrastructure, and may be used to deploy nanos instances with terraform-ops-provider on supported cloud providers.
Download and install terraform.

GCP

See below an explanation of the resources needed to upload a nanos image and to launch an instance to google cloud provider. You can see the entire configuration file at the end of this section. All files required to run this example is in ops terraform plugin repository.
  1. 1.
    Start importing the ops terraform plugin and set your google cloud settings.
    1
    terraform {
    2
    required_providers {
    3
    ops = {
    4
    source = "nanovms/ops"
    5
    }
    6
    }
    7
    }
    8
    9
    provider "google" {
    10
    project = "prod-1033"
    11
    region = "us-west2"
    12
    zone = "us-west2-a"
    13
    }
    14
    15
    provider "ops" {
    16
    17
    }
    Copied!
  2. 2.
    Create an image in your local machine with your application binary file. You have to specify your google cloud account settings in a configuration file and set the target cloud to gcp.
    1
    resource "ops_images" "walk_server_image" {
    2
    name = "walk-server"
    3
    elf = "./walk-server"
    4
    config = "./config.json"
    5
    targetcloud = "gcp"
    6
    }
    Copied!
  3. 3.
    Upload the nanos image to google storage. The resources below create a bucket in google storage and upload the nanos image.
    1
    resource "google_storage_bucket" "images_bucket" {
    2
    name = "terraform-images"
    3
    location = "us"
    4
    force_destroy = true
    5
    }
    6
    7
    resource "google_storage_bucket_object" "walk_server_raw_disk" {
    8
    name = "walk-server.tar.gz"
    9
    source = ops_images.walk_server_image.path
    10
    bucket = google_storage_bucket.images_bucket.name
    11
    }
    Copied!
  4. 4.
    Use the path of the object uploaded to create the image.
    1
    resource "google_compute_image" "walk_server_image" {
    2
    name = "walk-server-img"
    3
    4
    raw_disk {
    5
    source = google_storage_bucket_object.walk_server_raw_disk.self_link
    6
    }
    7
    8
    labels = {
    9
    "createdby" = "ops"
    10
    }
    11
    12
    }
    Copied!
  5. 5.
    Use the image as base to launch an instance. Make sure you add firewall rules to make your application publicly available.
    1
    resource "google_compute_instance" "walk_server_instance" {
    2
    name = "walk-server"
    3
    machine_type = "f1-micro"
    4
    5
    boot_disk {
    6
    initialize_params {
    7
    image = google_compute_image.walk_server_image.self_link
    8
    }
    9
    }
    10
    11
    labels = {
    12
    "createdby" = "ops"
    13
    }
    14
    15
    tags = ["walk-server"]
    16
    17
    network_interface {
    18
    # A default network is created for all GCP projects
    19
    network = "default"
    20
    access_config {
    21
    }
    22
    }
    23
    }
    24
    25
    resource "google_compute_firewall" "walk_server_firewall" {
    26
    name = "walk-server-firewall"
    27
    network = "default"
    28
    29
    allow {
    30
    protocol = "tcp"
    31
    ports = ["8080"]
    32
    }
    33
    34
    target_tags = ["walk-server"]
    35
    }
    Copied!
    After joining all pieces we get the next terraform configuration file.
    1
    terraform {
    2
    required_providers {
    3
    ops = {
    4
    source = "nanovms/ops"
    5
    }
    6
    }
    7
    }
    8
    9
    provider "google" {
    10
    project = "prod-1033"
    11
    region = "us-west2"
    12
    zone = "us-west2-a"
    13
    }
    14
    15
    provider "ops" {
    16
    17
    }
    18
    19
    resource "ops_images" "walk_server_image" {
    20
    name = "walk-server"
    21
    elf = "./walk-server"
    22
    config = "./config.json"
    23
    targetcloud = "gcp"
    24
    }
    25
    26
    resource "google_storage_bucket" "images_bucket" {
    27
    name = "terraform-images"
    28
    location = "us"
    29
    force_destroy = true
    30
    }
    31
    32
    resource "google_storage_bucket_object" "walk_server_raw_disk" {
    33
    name = "walk-server.tar.gz"
    34
    source = ops_images.walk_server_image.path
    35
    bucket = google_storage_bucket.images_bucket.name
    36
    }
    37
    38
    resource "google_compute_image" "walk_server_image" {
    39
    name = "walk-server-img"
    40
    41
    raw_disk {
    42
    source = google_storage_bucket_object.walk_server_raw_disk.self_link
    43
    }
    44
    45
    labels = {
    46
    "createdby" = "ops"
    47
    }
    48
    49
    }
    50
    51
    resource "google_compute_instance" "walk_server_instance" {
    52
    name = "walk-server"
    53
    machine_type = "f1-micro"
    54
    55
    boot_disk {
    56
    initialize_params {
    57
    image = google_compute_image.walk_server_image.self_link
    58
    }
    59
    }
    60
    61
    labels = {
    62
    "createdby" = "ops"
    63
    }
    64
    65
    tags = ["walk-server"]
    66
    67
    network_interface {
    68
    # A default network is created for all GCP projects
    69
    network = "default"
    70
    access_config {
    71
    }
    72
    }
    73
    }
    74
    75
    resource "google_compute_firewall" "walk_server_firewall" {
    76
    name = "walk-server-firewall"
    77
    network = "default"
    78
    79
    allow {
    80
    protocol = "tcp"
    81
    ports = ["8080"]
    82
    }
    83
    84
    target_tags = ["walk-server"]
    85
    }
    86
    87
    output "image_path" {
    88
    value = ops_images.walk_server_image.path
    89
    }
    90
    91
    output "instance_ip" {
    92
    value = google_compute_instance.walk_server_instance.network_interface[0].access_config[0].nat_ip
    93
    }
    Copied!
    After running this configuration you will see the path where the image file was created locally and the ip address of the launched instance.
Last modified 5mo ago
Copy link
Contents
GCP