1. Assigning a Primary IP to a Hetzner Cloud Server

    TypeScript

    When creating a cloud server on most cloud providers, including Hetzner Cloud, a primary IP is automatically assigned to the server. Pulumi enables you to define your cloud infrastructure as code, which means we can provision a Hetzner Cloud Server with an associated IP address.

    With Pulumi, we define resources like cloud servers and IP addresses by creating instances of classes that represent resource types. In the case of Hetzner Cloud (which at the time of this writing does not have a direct Pulumi provider), we can use Pulumi's Terraform bridge to leverage the Terraform Hetzner Cloud provider.

    To accomplish this, you will need to set up the following resources:

    • hcloud provider: To interact with Hetzner Cloud API using Pulumi.
    • Server: A cloud server which will be provisioned on Hetzner Cloud.
    • FloatingIp: A floating IP that can be assigned to a server.

    Here is an example program in TypeScript using Pulumi to create a server on Hetzner Cloud and assign it a primary IP address:

    import * as pulumi from "@pulumi/pulumi"; import * as hcloud from "@pulumi/hcloud"; // Create a new Hetzner Cloud Server. const server = new hcloud.Server("my-server", { serverType: "cx11", // The type of server (e.g., cx11, cx21, etc.) image: "ubuntu-20.04", // The image you want to use for the server (e.g., ubuntu-20.04) // Other configurations like the location or datacenter can be set here as well. }); // Allocate a new Floating IP to the server. const ip = new hcloud.FloatingIp("my-fip", { type: "ipv4", // The type of floating IP (ipv4 or ipv6) homeLocation: "fsn1", // The location where the IP should be created // The server ID to which the IP should be assigned in the format `hcloud_server.my-server.id` serverId: server.id, }); // Export the IP address and server ID as stack outputs. export const ipAddress = ip.ipAddress; export const serverId = server.id;

    Explanations:

    • hcloud.Server: This resource provisions a new server in Hetzner Cloud. You must specify the type of server and the operating system image you want to use.
    • hcloud.FloatingIp: This resource creates a floating IP address. The floating IP can be moved between servers, providing flexibility. Here it's assigned to the server we created. You must specify the IP type and the location.
    • ipAddress and serverId are exported as stack outputs. This allows you to access these values from the Pulumi CLI after the deployment is done. They provide useful information like the IP address of your server and its unique identifier within Hetzner Cloud.

    Please note that while working with cloud providers, you often need to set up account credentials or tokens to authenticate API requests. With Hetzner Cloud, you need to set up an API token and provide it to Pulumi so it can manage your resources. Ensure that you follow Hetzner Cloud's best practices for security and that the API token has the appropriate permissions for the operations you want to perform.

    To run this code:

    1. Install Pulumi CLI and set it up on your system.
    2. Set up Hetzner Cloud provider by providing an API token.
    3. Save the above code in a TypeScript file, such as index.ts.
    4. Run pulumi up in your terminal to create the resources defined in the code.

    Remember to review the associated costs and understand the billing mechanism for the resources you are deploying. Always clean up resources that you no longer need to avoid unnecessary cloud costs.