How to create a VM via IaaS-API
In this guide, we’ll demonstrate how to create a virtual machine using the STACKIT IaaS API and CLI.
Our step-by-step process will cover the essential components needed to deploy a fully functional virtual machine.
Get an overview about the environment
Section titled “Get an overview about the environment”- Switch over to where you have installed the STACKIT CLI
- First use the cli to Authenticate to the API and set the project ID
$ stackit auth loginSuccessfully logged into STACKIT CLI.$ stackit config set --project-id xxx$ PROJECTID=xxxFor this tutorial we will use the Ubuntu 20.04 image so we are filtering for it
stackit curl https://iaas.api.eu01.stackit.cloud/v1/projects/{projectID}/images | jq '.items[]|select(.name =="Ubuntu 20.04")|{id,name,status,scope}'
{ "id": "7c3bdf4b-422e-4bad-b224-a3eea9a04185", "name": "Ubuntu 20.04", "status": "AVAILABLE", "scope": "public"}All available volume performance-classes can be found here:
All available machine-types can be found here:
Create VM with a random local IP
Section titled “Create VM with a random local IP”In this part of the guide you will create a VM that will have the next free local IP assigned to it.
Create a volume that will act as the boot volume
Section titled “Create a volume that will act as the boot volume”stackit volume create --availability-zone [AVAILABILITY_ZONE] --name [VOLUME_NAME] --source-id [IMAGE_ID] --source-type image --size [IMAGE_SIZE]| —availability-zone [AVAILABILITY_ZONE] | Specifies the availability zone where the volume will be created. This ensures the volume is provisioned in the desired data center location |
|---|---|
| —name [VOLUME_NAME] | Defines a custom name for the new volume, making it easier to identify and manage |
| —source-id [IMAGE_ID] | Indicates the ID of the source image from which the volume will be created. This allows you to create volumes based on existing images |
| —source-type image | Specifies that the source for the new volume is an image. This parameter works in conjunction with the —source-id flag |
| —size [IMAGE_SIZE] | Determines the size of the new volume. The size should be specified in an appropriate unit (e.g., GB). The maximum limit is 72GB. |
After Execution of this command you receive a Volume ID.
Create a Security group
Section titled “Create a Security group”To be able to access the VM, we need a Security Group that contains a Firewall rule that allows SSH.
For this we create a security group
stackit curl -X POST -H "Content-Type: application/json" --data '{"name": "[SECURITY_GROUP_NAME]"}' https://iaas.api.eu01.stackit.cloud/v1/projects/[PROJECT_ID]/security-groups- Replace [SECURITY_GROUP_NAME] with the name of the security group you want to create
- Replace [PROJECT_ID] with your STACKIT Project ID
After Execution of this command you receive a Security Group ID.
Create the security group rules
Section titled “Create the security group rules”Allow SSH
stackit curl -X POST -H "Content-Type: application/json" --data '{"direction": "ingress", "protocol": "tcp", "portRange":{"max": 22, "min": 22}}' https://iaas.api.eu01.stackit.cloud/v1/projects/[PROJECT_ID]/security-groups/[SECURITY_GROUP_ID]/rulesAllow ICMP
stackit curl -X POST -H "Content-Type: application/json" --data '{"direction": "ingress", "protocol": "icmp"}' https://iaas.api.eu01.stackit.cloud/v1/projects/[PROJECT_ID]/security-groups/[SECURITY_GROUP_ID]/rules- replace [PROJECT_ID] with your STACKIT Project ID
- replace [SECURITY_GROUP_ID] with the ID of the security group you created
Create a network
Section titled “Create a network”stackit network create --name [NETWORK_NAME] --ipv4-dns-name-servers "[DNS_SERVER_1],[DNS_SERVER_2]" --ipv4-prefix "[IPV4-PREFIX]"| —name [NETWORK_NAME] | Specifies the name for the new network |
|---|---|
| —ipv4-dns-name-servers “[DNS_SERVER_1],[DNS_SERVER_2]“ | Sets custom DNS servers for the network. Multiple DNS servers can be specified, separated by commas |
| —ipv4-prefix “[IPV4-PREFIX]“ | Defines the IPv4 address range for the network using CIDR notation |
After Execution of this command you receive a Network ID.
Create a Keypair
Section titled “Create a Keypair”Create an SSH key pair that will be used to authenticate your SSH session
stackit curl -X POST -H "Content-Type: application/json" --data '{"name":"[KEYPAIR_NAME]", "publicKey": "[PUBLIC_KEY]"}' https://iaas.api.eu01.stackit.cloud/v1/keypairsreplace **[KEYPAIR_NAME]**with the name you want to assign to the new key pair.replace **[PUBLIC_KEY]**with the public key string of your SSH key pair.
Create the virtual machine
Section titled “Create the virtual machine”stackit server create -n [SERVERNAME] --availability-zone [AVAILABILITY_ZONE] --machine-type [MACHINE_TYPE] --keypair-name [KEYPAIR_NAME] --network-id [NETWORK_ID] --boot-volume-source-id [VOLUME_ID] --boot-volume-source-type volume --security-groups [SECURITY_GROUP_ID]-n [SERVERNAME]: Specifies the name for the new server.--availability-zone [AVAILABILITY_ZONE]: Defines the availability zone where the server will be created.--machine-type [MACHINE_TYPE]: Sets the hardware configuration for the server.--keypair-name [KEYPAIR_NAME]: Assigns the SSH key pair for secure access to the server.--network-id [NETWORK_ID]: Connects the server to a specific network.--boot-volume-source-id [VOLUME_ID]: Specifies the ID of the volume to use as the boot volume.--boot-volume-source-type volume: Indicates that the boot volume source is an existing volume.--security-groups [SECURITY_GROUP_ID]: Associates the server with specified security group(s) for network access control.
After Execution of this command you receive a Server ID.
Add a public IP
Section titled “Add a public IP”To enable public access to your server, you’ll need to assign a public IP address to the virtual machine’s network interface.
To retrieve the network interface ID of a virtual machine, execute the following command
stackit server describe [SERVER_ID]Create the public IP and attach it to the server
stackit public-ip create --associated-resource-id [NETWORK_INTERFACE_ID]Read out the Public IP
stackit server describe [SERVER_ID]Now you can login via ssh.