Pulumi
-
Infrastructure as code (IAC)
-
Infrastructure state model like Terraform
-
Looks imperative but IS actually declarative.
-
Various language bindings: TypeScript, Python, Go, C#, Java ...
| Pulumi / Java | Terraform |
|---|---|
import com.pulumi.hcloud.Server; ... var server = new Server ( "helloServer", ServerArgs.builder() .name("hello") .image("debian-13") .serverType("cx23") .location("nbg1") .build() ); |
resource "hcloud_server" "helloServer" { name = "hello" image = "debian-13" server_type = "cx23" location = "nbg1" } |
public static void stack(Context ctx) { Server node1 = new Server ("Server1", ServerArgs.builder() .name("node1") ... .build() ); Output<String> ipv4Node1 = node1.ipv4Address(); ctx.export("Node1 ipv4", ipv4Node1); Server node2 = new Server ("Server2", ServerArgs.builder() .name("node2") ... .build() ); ctx.export("Node2 ipv4", node2.ipv4Address()); }
$ pulumi up
Previewing update (java):
Type Name Plan
+ pulumi:pulumi:Stack Hello-java create
+ ├─ hcloud:index:Server Server2 create
+ └─ hcloud:index:Server Server1 create
Outputs:
Node1 ipv4: [unknown]
Node2 ipv4: [unknown]
Resources:
+ 3 to create
Do you want to perform this update? [Use arrows to move, type to filter]
> yes
no
details
Updating (java):
Type Name Status
+ pulumi:pulumi:Stack Hello-java created (10s)
+ ├─ hcloud:index:Server Server2 created (8s)
+ └─ hcloud:index:Server Server1 created (9s)
Outputs:
Node1 ipv4: "88.99.224.53"
Node2 ipv4: "91.99.143.208"
Resources:
+ 3 created
Duration: 12s No. 30
Hello server creation
|
Q: |
|
static private final String cloudInitCode = """
#cloud-config
packages:
- nginx
runcmd:
- systemctl enable nginx
- rm /var/www/html/*
- echo I am Nginx @ $(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com)
created $(date -u) >> /var/www/html/index.html""";
var server = new Server ("helloServer", ServerArgs.builder()
.name("hello")
...
.userData(cloudInitCode)
.build()
);static private final String sshPublicKeyIdentifier = "userSshPublicKey"; ... final String content = Files.readString(Path.of("/home/goik/.ssh/id_ed25519.pub")); new SshKey(sshPublicKeyIdentifier, SshKeyArgs.builder() .name(sshPublicKeyIdentifier).publicKey(content).build() ); ... var server = new Server ("helloServer", ServerArgs.builder() .name("hello") ... .sshKeys(sshPublicKeyIdentifier) .build() );
No. 31
ssh keys and cloud-init script
|
Q: |
Extend your previous application by adding:
|
