Algorithmic Trading: Pulumi Deployment

Summery

Th

How to setup and use Pulumi

The Code Explained

Suggested text: When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam

CodeToHTML
package main

import (
    containerservice "github.com/pulumi/pulumi-azure-native-sdk/containerservice/v20230102preview"
    keyvault "github.com/pulumi/pulumi-azure-native-sdk/keyvault"
    network "github.com/pulumi/pulumi-azure-native-sdk/network"
    resources "github.com/pulumi/pulumi-azure-native-sdk/resources/v2/v20241101"
    "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

var resourceGroupName string = "rg1"
var resourceGroupLocation string = "westus"

func main() {
    //Run resource creation
    pulumi.Run(func(ctx *pulumi.Contexterror {

        //Call the resource group creation function
        rgerr := createResourceGroup(ctxresourceGroupNameresourceGroupLocation)
        if err != nil {
            return err
        }

        // You can now use rg.Name, rg.ID, etc. for other resources
        ctx.Export("resourceGroupName"rg.Name)

        vneterr := NewVNet(ctx"myVNet"pulumi.String(resourceGroupLocation), rg.Name)
        if err != nil {
            return err
        }

        subneterr := NewSubnet(ctx"subnet1"vnet"10.10.10.0/24")
        if err != nil {
            return err
        }
        ctx.Export("subnetID"subnet.ID())

        _err = createAKS(ctxsubnet.ID())
        if err != nil {
            return err
        }

        tenantID := pulumi.String("<your-tenant-id>")
        subnetID := subnet.ID() // from your existing subnet

        _err = createKeyVault(ctx"vault90348503485"resourceGroupNameresourceGroupLocationt
        enantIDsubnetID)
        if err != nil {
            return err
        }

        return nil
    })
}

// Function for Creating Resource Group
func createResourceGroup(ctx *pulumi.Contextname stringlocation string) (*resources.ResourceGrou
perror) {
    rgerr := resources.NewResourceGroup(ctxname&resources.ResourceGroupArgs{
        Location:          pulumi.String(location),
        ResourceGroupNamepulumi.String(name),
    })
    if err != nil {
        return nilerr
    }
    return rgnil
}

type VNet struct {
    pulumi.CustomResourceState
}

func NewVNet(ctx *pulumi.Contextname stringlocation pulumi.StringInputrgName pulumi.StringInpu
t) (*VNeterror) {
    var vnet VNet

    args := pulumi.Map{
        "resourceGroupName"rgName,
        "location":          location,
        "addressSpace"pulumi.Map{
            "addressPrefixes"pulumi.StringArray{
                pulumi.String("10.0.0.0/16"),
            },
        },
    }

    err := ctx.RegisterResource("Microsoft.Network/virtualNetworks"nameargs&vnet)
    if err != nil {
        return nilerr
    }

    return &vnetnil
}

type Subnet struct {
    pulumi.CustomResourceState
}

func NewSubnet(ctx *pulumi.Contextname stringvnet *VNetprefix string) (*Subneterror) {
    var subnet Subnet

    args := pulumi.Map{
        "addressPrefix"pulumi.String(prefix),
    }

    err := ctx.RegisterResource("Microsoft.Network/virtualNetworks/subnets"nameargs&subnetpu
    lumi.Parent(vnet))
    if err != nil {
        return nilerr
    }

    return &subnetnil
}

// createAKS provisions an Azure Kubernetes Service (AKS) cluster
func createAKS(ctx *pulumi.ContextsubnetID pulumi.StringInput) (*containerservice.ManagedCluster
error) {
    clustererr := containerservice.NewManagedCluster(ctx"managedCluster"&containerservice.Mana
    gedClusterArgs{
        AddonProfilescontainerservice.ManagedClusterAddonProfileMap{
            "azureKeyvaultSecretsProvider"&containerservice.ManagedClusterAddonProfileArgs{
                Configpulumi.StringMap{
                    "enableSecretRotation"pulumi.String("true"),
                    "rotationPollInterval"pulumi.String("2m"),
                },
                Enabledpulumi.Bool(true),
            },
        },

        // Enable Azure Disk CSI driver
        StorageProfile&containerservice.ManagedClusterStorageProfileArgs{
            DiskCSIDriver&containerservice.ManagedClusterStorageProfileDiskCSIDriverArgs{
                Enabledpulumi.Bool(true),
            },
        },

        AgentPoolProfilescontainerservice.ManagedClusterAgentPoolProfileArray{
            &containerservice.ManagedClusterAgentPoolProfileArgs{
                Count:              pulumi.Int(2),
                EnableNodePublicIPpulumi.Bool(false),
                Mode:               pulumi.String(containerservice.AgentPoolModeSystem),
                Name:               pulumi.String("nodepool1"),
                OsType:             pulumi.String(containerservice.OSTypeLinux),
                Type:               pulumi.String(containerservice.AgentPoolTypeVirtualMachineScaleS
                ets),
                VmSize:             pulumi.String("Standard_DS2_v2"),
                VnetSubnetID:       subnetID,
            },
        },
        AutoScalerProfile&containerservice.ManagedClusterPropertiesAutoScalerProfileArgs{
            ScaleDownDelayAfterAddpulumi.String("15m"),
            ScanInterval:           pulumi.String("20s"),
        },
        DiskEncryptionSetID:     pulumi.String("/subscriptions/00000000-0000-0000-0000-000000000000/
        resourcegroups/rg1/providers/Microsoft.Compute/diskEncryptionSets/des"),
        DnsPrefix:               pulumi.String("dnsprefix1"),
        EnablePodSecurityPolicypulumi.Bool(true),
        EnableRBAC:              pulumi.Bool(true),
        KubernetesVersion:       pulumi.String(""),
        LinuxProfile&containerservice.ContainerServiceLinuxProfileArgs{
            AdminUsernamepulumi.String("azureuser"),
            Ssh&containerservice.ContainerServiceSshConfigurationArgs{
                PublicKeyscontainerservice.ContainerServiceSshPublicKeyArray{
                    &containerservice.ContainerServiceSshPublicKeyArgs{
                        KeyDatapulumi.String("keydata"),
                    },
                },
            },
        },
        Locationpulumi.String(resourceGroupLocation),
        NetworkProfile&containerservice.ContainerServiceNetworkProfileArgs{
            LoadBalancerProfile&containerservice.ManagedClusterLoadBalancerProfileArgs{
                ManagedOutboundIPs&containerservice.ManagedClusterLoadBalancerProfileManagedOutbou
                ndIPsArgs{
                    Countpulumi.Int(2),
                },
            },
            LoadBalancerSkupulumi.String(containerservice.LoadBalancerSkuStandard),
            OutboundType:    pulumi.String(containerservice.OutboundTypeLoadBalancer),
        },
        ResourceGroupNamepulumi.String("rg1"),
        ResourceName:      pulumi.String("clustername1"),

        Identity&containerservice.ManagedClusterIdentityArgs{
            Typecontainerservice.ResourceIdentityTypeSystemAssigned,
        },

        Sku&containerservice.ManagedClusterSKUArgs{
            Namepulumi.String("Basic"),
            Tierpulumi.String(containerservice.ManagedClusterSKUTierFree),
        },
        Tagspulumi.StringMap{
            "archv2"pulumi.String(""),
            "tier":   pulumi.String("production"),
        },
        WindowsProfile&containerservice.ManagedClusterWindowsProfileArgs{
            AdminPasswordpulumi.String("replacePassword1234$"),
            AdminUsernamepulumi.String("azureuser"),
        },
    })
    if err != nil {
        return nilerr
    }

    return clusternil
}

func createKeyVault(ctx *pulumi.Context,
    vaultNameresourceGroupNamelocation string,
    tenantID pulumi.StringInputsubnetID pulumi.StringInput,
) (*keyvault.Vaulterror) {

    // Create Key Vault
    vaulterr := keyvault.NewVault(ctxvaultName&keyvault.VaultArgs{
        ResourceGroupNamepulumi.String(resourceGroupName),
        VaultName:         pulumi.String(vaultName),
        Location:          pulumi.String(location),
        Properties&keyvault.VaultPropertiesArgs{
            TenantIdtenantID,
            Sku&keyvault.SkuArgs{
                Name:   keyvault.SkuNameStandard,
                Familypulumi.String(keyvault.SkuFamilyA),
            },
            AccessPolicieskeyvault.AccessPolicyEntryArray{
                &keyvault.AccessPolicyEntryArgs{
                    TenantIdtenantID,
                    ObjectIdpulumi.String("<your-object-id>"),
                    Permissions&keyvault.PermissionsArgs{
                        Keyspulumi.StringArray{
                            pulumi.String("get"),
                            pulumi.String("list"),
                            pulumi.String("create"),
                            pulumi.String("delete"),
                        },
                        Secretspulumi.StringArray{
                            pulumi.String("get"),
                            pulumi.String("list"),
                            pulumi.String("set"),
                            pulumi.String("delete"),
                        },
                        Certificatespulumi.StringArray{
                            pulumi.String("get"),
                            pulumi.String("list"),
                            pulumi.String("create"),
                            pulumi.String("delete"),
                        },
                    },
                },
            },
            EnabledForDeployment:         pulumi.Bool(true),
            EnabledForTemplateDeploymentpulumi.Bool(true),
            EnabledForDiskEncryption:     pulumi.Bool(true),
            NetworkAcls&keyvault.NetworkRuleSetArgs{
                DefaultActionpulumi.String("Deny"),
                Bypass:        pulumi.String("AzureServices"),
            },
        },
    })
    if err != nil {
        return nilerr
    }

    // Create Private Endpoint Key Vault
    _err = network.NewPrivateEndpoint(ctxvaultName+"-pe"&network.PrivateEndpointArgs{
        ResourceGroupNamepulumi.String(resourceGroupName),
        Location:          pulumi.String(location),
        Subnet&network.SubnetTypeArgs{
            IdsubnetID,
        },

        PrivateLinkServiceConnectionsnetwork.PrivateLinkServiceConnectionArray{
            &network.PrivateLinkServiceConnectionArgs{
                Name:                 pulumi.String("kv-endpoint-connection"),
                PrivateLinkServiceIdvault.ID(),
                GroupIdspulumi.StringArray{
                    pulumi.String("vault"),
                },
            },
        },
    })
    if err != nil {
        return nilerr
    }

    return vaultnil
}

© 2023 All Rights Reserved.
Email: zacaryfettig@gmail.com
Write me a message
Write me a message

    * I promise the confidentiality of your personal information