{Pulumi} EC2/Azure

https://www.pulumi.com/registry/packages/azure/api-docs/compute/linuxvirtualmachine/


「pulumi_azure_native」ではNIC作成が抽象化されていないため、
「pulumi_azure」を使用する

 

 


Python: 3.9.13

-- 1. macにPulumiインストール


brew install pulumi/tap/pulumi

 


-- 2. Azureログイン

az login --use-device-code
az account show

az version

az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults

 

az group list
az upgrade

 

 

 


-- 3. Create new project


mkdir test
cd test
pulumi new azure-python


Japan East
japaneast

↑locationはどちらの表記でもOK


az account list-locations --output table

pulumi config get azure-native:location
pulumi config set azure-native:location japaneast
pulumi config get azure-native:location


vi requirements.txt

pulumi_azure
を追加


venv/bin/pip install -r requirements.txt

 

vim __main__.py

import pulumi
import pulumi_azure as azure

rg9999999 = azure.core.ResourceGroup("rg9999999",
    location="Japan East",
    name="rg9999999")

vnet01 = azure.network.VirtualNetwork("vnet01",
    address_spaces=["10.2.0.0/16"],
    location=rg9999999.location,
    resource_group_name=rg9999999.name)
    
subnet01 = azure.network.Subnet("subnet01",
    resource_group_name=rg9999999.name,
    virtual_network_name=vnet01.name,
    address_prefixes=["10.2.1.0/24"])
    
pip01 = azure.network.PublicIp("pip01",
    resource_group_name=rg9999999.name,
    location=rg9999999.location,
    allocation_method="Static",
    tags={
        "environment": "dev",
    })

nsg01 = azure.network.NetworkSecurityGroup("nsg01",
    location=rg9999999.location,
    resource_group_name=rg9999999.name,
    security_rules=[azure.network.NetworkSecurityGroupSecurityRuleArgs(
        name="nsgr01",
        priority=100,
        direction="Inbound",
        access="Allow",
        protocol="Tcp",
        source_port_range="*",
        destination_port_range="22",
        source_address_prefix="*",
        destination_address_prefix="*",
    )],
    tags={
        "environment": "dev",
    })


nic01 = azure.network.NetworkInterface("nic01",
    location=rg9999999.location,
    resource_group_name=rg9999999.name,
    ip_configurations=[azure.network.NetworkInterfaceIpConfigurationArgs(
        name="internal",
        subnet_id=subnet01.id,
        private_ip_address_allocation="Dynamic",
        public_ip_address_id=pip01.id
    )])
    

nsg01_nic01 = azure.network.NetworkInterfaceSecurityGroupAssociation("nsg01_nic01",
    network_interface_id=nic01.id,
    network_security_group_id=nsg01.id)
    


vm01 = azure.compute.LinuxVirtualMachine("vm01",
    resource_group_name=rg9999999.name,
    location=rg9999999.location,
    size="Standard_B1ls",
    admin_username="azureuser",
    network_interface_ids=[nic01.id],
    admin_ssh_keys=[azure.compute.LinuxVirtualMachineAdminSshKeyArgs(
        username="azureuser",
        public_key=(lambda path: open(path).read())("/Users/azureuser/.ssh/id_rsa.pub"),
    )],
    os_disk=azure.compute.LinuxVirtualMachineOsDiskArgs(
        caching="ReadWrite",
        storage_account_type="Standard_LRS",
    ),
    source_image_reference=azure.compute.LinuxVirtualMachineSourceImageReferenceArgs(
        publisher="Canonical",
        offer="0001-com-ubuntu-server-focal",
        sku="20_04-lts",
        version="latest",
    ) )


-- 4. pulumi 実行

pulumi up

 

az vm list \
--resource-group rg9999999

 

-- 5. Azureクリーンアップ


pulumi destroy

 

pulumi stack rm dev

 

az group delete \
--name NetworkWatcherRG \
--yes