-
Notifications
You must be signed in to change notification settings - Fork 880
/
main.go
76 lines (70 loc) · 2.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/compute"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
computeNetwork, err := compute.NewNetwork(ctx, "network",
&compute.NetworkArgs{
AutoCreateSubnetworks: pulumi.Bool(true),
},
)
if err != nil {
return err
}
computeFirewall, err := compute.NewFirewall(ctx, "firewall",
&compute.FirewallArgs{
Network: computeNetwork.SelfLink,
Allows: &compute.FirewallAllowArray{
&compute.FirewallAllowArgs{
Protocol: pulumi.String("tcp"),
Ports: pulumi.StringArray{
pulumi.String("22"),
pulumi.String("80"),
},
},
},
},
)
if err != nil {
return err
}
// (optional) create a simple web server using the startup script for the instance
startupScript := `#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`
computeInstance, err := compute.NewInstance(ctx, "instance",
&compute.InstanceArgs{
MachineType: pulumi.String("f1-micro"),
MetadataStartupScript: pulumi.String(startupScript),
BootDisk: &compute.InstanceBootDiskArgs{
InitializeParams: &compute.InstanceBootDiskInitializeParamsArgs{
Image: pulumi.String("debian-cloud/debian-9-stretch-v20181210"),
},
},
NetworkInterfaces: compute.InstanceNetworkInterfaceArray{
&compute.InstanceNetworkInterfaceArgs{
Network: computeNetwork.ID(),
// Must be empty to request an ephemeral IP
AccessConfigs: &compute.InstanceNetworkInterfaceAccessConfigArray{
&compute.InstanceNetworkInterfaceAccessConfigArgs{},
},
},
},
ServiceAccount: &compute.InstanceServiceAccountArgs{
Scopes: pulumi.StringArray{
pulumi.String("https://www.googleapis.com/auth/cloud-platform"),
},
},
},
pulumi.DependsOn([]pulumi.Resource{computeFirewall}),
)
if err != nil {
return err
}
ctx.Export("instanceName", computeInstance.Name)
ctx.Export("instanceIP", computeInstance.NetworkInterfaces.Index(pulumi.Int(0)).AccessConfigs().Index(pulumi.Int(0)).NatIp())
return nil
})
}