1+ import pulumi
2+ import pulumi_yandex as yandex
3+
4+ # Read configuration (set via pulumi config)
5+ config = pulumi .Config ()
6+ cloud_id = config .require ("cloud_id" )
7+ folder_id = config .require ("folder_id" )
8+ zone = config .get ("zone" ) or "ru-central1-a"
9+ public_key_path = config .get ("public_key_path" ) or "~/.ssh/id_rsa.pub"
10+
11+ # Read SSH public key file
12+ with open (public_key_path , "r" ) as f :
13+ ssh_public_key = f .read ().strip ()
14+
15+ # Get Ubuntu image
16+ image = yandex .get_compute_image (family = "ubuntu-2404-lts-oslogin" )
17+
18+ # Create VPC network
19+ network = yandex .VpcNetwork ("lab-network" )
20+
21+ # Create subnet
22+ subnet = yandex .VpcSubnet ("lab-subnet" ,
23+ zone = zone ,
24+ network_id = network .id ,
25+ v4_cidr_blocks = ["192.168.10.0/24" ])
26+
27+ # Create security group
28+ security_group = yandex .VpcSecurityGroup ("lab-sg" ,
29+ network_id = network .id ,
30+ description = "Allow SSH, HTTP, and app port 5000" ,
31+ ingress = [
32+ yandex .VpcSecurityGroupIngressArgs (
33+ protocol = "TCP" ,
34+ description = "SSH" ,
35+ port = 22 ,
36+ v4_cidr_blocks = ["0.0.0.0/0" ],
37+ ),
38+ yandex .VpcSecurityGroupIngressArgs (
39+ protocol = "TCP" ,
40+ description = "HTTP" ,
41+ port = 80 ,
42+ v4_cidr_blocks = ["0.0.0.0/0" ],
43+ ),
44+ yandex .VpcSecurityGroupIngressArgs (
45+ protocol = "TCP" ,
46+ description = "App port 5000" ,
47+ port = 5000 ,
48+ v4_cidr_blocks = ["0.0.0.0/0" ],
49+ ),
50+ ],
51+ egress = [yandex .VpcSecurityGroupEgressArgs (
52+ protocol = "ANY" ,
53+ description = "Allow all outbound" ,
54+ v4_cidr_blocks = ["0.0.0.0/0" ],
55+ )])
56+
57+ # Create VM instance
58+ vm = yandex .ComputeInstance ("lab-vm" ,
59+ zone = zone ,
60+ platform_id = "standard-v2" ,
61+ resources = yandex .ComputeInstanceResourcesArgs (
62+ cores = 2 ,
63+ memory = 1 ,
64+ core_fraction = 20 ,
65+ ),
66+ boot_disk = yandex .ComputeInstanceBootDiskArgs (
67+ initialize_params = yandex .ComputeInstanceBootDiskInitializeParamsArgs (
68+ image_id = image .id ,
69+ size = 10 ,
70+ type = "network-hdd" ,
71+ ),
72+ ),
73+ network_interfaces = [yandex .ComputeInstanceNetworkInterfaceArgs (
74+ subnet_id = subnet .id ,
75+ security_group_ids = [security_group .id ],
76+ nat = True ,
77+ )],
78+ metadata = {
79+ "ssh-keys" : f"ubuntu:{ ssh_public_key } " ,
80+ })
81+
82+ # Export public IP
83+ pulumi .export ("vm_public_ip" , vm .network_interfaces [0 ].nat_ip_address )
84+ pulumi .export ("ssh_command" , pulumi .Output .concat ("ssh ubuntu@" , vm .network_interfaces [0 ].nat_ip_address ))
0 commit comments