Skip to content

Commit ca3e89d

Browse files
authored
Additional languages for the Directly Executed Modules blog: C#, YAML, Java (#15397)
Following up on #15395 the PR adds example translations to cover C#, YAML and Java experience. None of these languages currently support terraformConfig() method and the examples for this method are omitted for these languages. The feature is tracked in pulumi/pulumi-terraform-bridge#3131
1 parent 223629c commit ca3e89d

File tree

1 file changed

+229
-25
lines changed
  • content/blog/announcing-direct-tf-modules

1 file changed

+229
-25
lines changed

content/blog/announcing-direct-tf-modules/index.md

Lines changed: 229 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ First, [install the latest version of the Pulumi CLI](/docs/install/) (v3.178.0
9292

9393
Next, add a Terraform module to your Pulumi project:
9494

95-
{{% chooser language "typescript,python,go" %}}
95+
{{% chooser language "typescript,python,go,csharp,java,yaml" %}}
9696

9797
{{% choosable language typescript %}}
9898

@@ -127,21 +127,86 @@ You can then import the SDK in your Python code with:
127127
{{% /choosable %}}
128128

129129
{{% choosable language go %}}
130+
```bash
131+
$ pulumi package add terraform-module terraform-aws-modules/vpc/aws 6.0.0 vpcmod
132+
130133
Using Terraform CLI for schema inference
131134
Successfully generated a Go SDK for the vpcmod package at /workdir/sdks/vpcmod
132135
Go mod file updated to use local sdk for vpcmod
133136
To use this package, import github.com/pulumi/pulumi-terraform-module/sdks/go/vpcmod/v6/vpcmod
134137
Added package "vpcmod" to Pulumi.yaml
138+
```
139+
{{% /choosable %}}
140+
141+
{{% choosable language csharp %}}
142+
```bash
143+
$ pulumi package add terraform-module terraform-aws-modules/vpc/aws 6.0.0 vpcmod
144+
145+
Using Terraform CLI for schema inference
146+
Successfully generated a .NET SDK for the vpcmod package at /workdir/sdks/vpcmod
147+
148+
Reference `sdks\vpcmod\Pulumi.Vpcmod.csproj` added to the project.
149+
You also need to add the following to your .csproj file of the program:
150+
151+
<DefaultItemExcludes>$(DefaultItemExcludes);sdks/**/*.cs</DefaultItemExcludes>
152+
153+
You can then use the SDK in your .NET code with:
154+
155+
using Pulumi.Vpcmod;
156+
157+
Added package "vpcmod" to Pulumi.yaml
158+
```
159+
{{% /choosable %}}
160+
161+
{{% choosable language java %}}
162+
163+
```bash
164+
$ pulumi package add terraform-module terraform-aws-modules/vpc/aws 6.0.0 vpcmod
165+
166+
Using Terraform CLI for schema inference
167+
Successfully generated a Java SDK for the vpcmod package at /workdir/sdks/vpcmod
168+
169+
To use this SDK in your Java project, complete the following steps:
170+
171+
1. Copy the contents of the generated SDK to your Java project:
172+
cp -r /workdir/sdks/vpcmod/src/* /workdir/src
173+
174+
2. Add the SDK's dependencies to your Java project's build configuration.
175+
If you are using Maven, add the following dependencies to your pom.xml:
176+
177+
<dependencies>
178+
<dependency>
179+
<groupId>com.google.code.findbugs</groupId>
180+
<artifactId>jsr305</artifactId>
181+
<version>3.0.2</version>
182+
</dependency>
183+
<dependency>
184+
<groupId>com.google.code.gson</groupId>
185+
<artifactId>gson</artifactId>
186+
<version>2.8.9</version>
187+
</dependency>
188+
</dependencies>
189+
190+
Added package "vpcmod" to Pulumi.yaml
191+
```
192+
193+
{{% /choosable %}}
194+
195+
{{% choosable language yaml %}}
196+
```bash
197+
$ pulumi package add terraform-module terraform-aws-modules/vpc/aws 6.0.0 vpcmod
198+
199+
Added package "vpcmod" to Pulumi.yaml
200+
```
135201
{{% /choosable %}}
136202

137203
{{% /chooser %}}
138204

139205
Pulumi automatically generates a local SDK with full support for your language:
140206

141-
{{% chooser language "typescript,python,go" %}}
207+
{{% chooser language "typescript,python,go,csharp,java,yaml" %}}
142208

143209
{{% choosable language typescript %}}
144-
145210
```bash
146211
$ ls sdks/vpcmod
147212
README.md index.ts node_modules provider.ts tsconfig.json utilities.ts
@@ -150,32 +215,51 @@ bin module.ts package.json scripts types
150215
{{% /choosable %}}
151216

152217
{{% choosable language python %}}
218+
```bash
153219
$ ls sdks/vpcmod
154220
build pulumi_vpcmod pulumi_vpcmod.egg-info setup.py
221+
```
155222
{{% /choosable %}}
156223

157224
{{% choosable language go %}}
225+
```bash
158226
$ ls sdks/vpcmod
159227
go.mod vpcmod
228+
```
160229
{{% /choosable %}}
161230

162-
{{% /chooser %}}
231+
{{% choosable language csharp %}}
232+
```bash
233+
$ ls sdks/vpcmod
234+
Inputs Provider.cs README.md
235+
logo.png pulumi-plugin.json Utilities.cs
236+
Module.cs Pulumi.Vpcmod.csproj version.txt
237+
```
238+
{{% /choosable %}}
163239

164-
And links it into your project such as `package.json` when using TypeScript:
240+
{{% choosable language java %}}
241+
```bash
242+
$ ls sdks/vpcmod
243+
README.md src
244+
```
245+
{{% /choosable %}}
165246

166-
```json
167-
{
168-
"dependencies": {
169-
"@pulumi/vpcmod": "file:sdks/vpcmod"
170-
}
171-
}
247+
{{% choosable language yaml %}}
248+
```bash
249+
$ ls sdks/vpcmod
250+
vpcmod-6.0.0.yaml
172251
```
252+
{{% /choosable %}}
253+
254+
{{% /chooser %}}
255+
256+
Pulumi automatically links the generated SDK into your project.
173257

174258
### Using the Module in Your Code
175259

176260
Now you can use the module with full IntelliSense support:
177261

178-
{{% chooser language "typescript,python,go" %}}
262+
{{% chooser language "typescript,python,go,csharp,java,yaml" %}}
179263

180264
{{% choosable language typescript %}}
181265
```typescript
@@ -205,27 +289,33 @@ export const privateSubnets = vpc.private_subnets;
205289

206290
{{% choosable language python %}}
207291
```python
208-
import * as pulumi from "@pulumi/pulumi";
209-
import * as vpcmod from '@pulumi/vpcmod';
292+
import pulumi
293+
import pulumi_aws as aws
294+
import pulumi_vpcmod as vpcmod
210295

211-
const vpc = new vpcmod.Module("test-vpc", {
212-
azs: ["us-west-2a", "us-west-2b"],
213-
name: `test-vpc-${pulumi.getStack()}`,
214-
cidr: "10.0.0.0/16",
215-
public_subnets: [
296+
aws_provider = aws.Provider("awsprovider", region="us-east-1")
297+
298+
vpcmod_provider = vpcmod.Provider("vpcprovider", aws=aws_provider.terraform_config().result)
299+
300+
vpc = vpcmod.Module(
301+
"test-vpc",
302+
azs=["us-west-2a", "us-west-2b"],
303+
name=f"test-vpc{pulumi.get_stack()}",
304+
cidr="10.0.0.0/16",
305+
public_subnets=[
216306
"10.0.1.0/24",
217307
"10.0.2.0/24",
218308
],
219-
private_subnets: [
309+
private_subnets=[
220310
"10.0.3.0/24",
221311
"10.0.4.0/24",
222312
],
223-
enable_nat_gateway: true,
224-
single_nat_gateway: true,
225-
});
313+
enable_nat_gateway=True,
314+
single_nat_gateway=True,
315+
)
226316

227-
export const publicSubnets = vpc.public_subnets;
228-
export const privateSubnets = vpc.private_subnets;
317+
pulumi.export("publicSubnets", vpc.public_subnets)
318+
pulumi.export("privateSubnets", vpc.private_subnets)
229319
```
230320
{{% /choosable %}}
231321

@@ -279,6 +369,119 @@ func main() {
279369

280370
{{% /choosable %}}
281371

372+
{{% choosable language csharp %}}
373+
374+
```csharp
375+
using System.Collections.Generic;
376+
using Pulumi;
377+
using Vpc = Pulumi.Vpcmod;
378+
379+
return await Deployment.RunAsync(() =>
380+
{
381+
var vpc = new Vpc.Module("test-vpc", new Vpc.ModuleArgs
382+
{
383+
Azs = new string [] {
384+
"us-west-2a",
385+
"us-west-2b",
386+
},
387+
Cidr = "10.0.0.0/16",
388+
Public_subnets = new string[] {
389+
"10.0.1.0/24",
390+
"10.0.2.0/24"
391+
},
392+
Private_subnets = new string[] {
393+
"10.0.1.0/24",
394+
"10.0.2.0/24"
395+
},
396+
Enable_nat_gateway=true,
397+
Single_nat_gateway=true
398+
});
399+
400+
return new Dictionary<string, object?>
401+
{
402+
["publicSubnets"] = vpc.Public_subnets,
403+
["privateSubnets"] = vpc.Private_subnets
404+
};
405+
});
406+
```
407+
{{% /choosable %}}
408+
409+
{{% choosable language java %}}
410+
```java
411+
package myproject;
412+
413+
import com.pulumi.Context;
414+
import com.pulumi.Pulumi;
415+
import com.pulumi.core.Output;
416+
import com.pulumi.deployment.Deployment;
417+
import com.pulumi.vpcmod.Module;
418+
import com.pulumi.vpcmod.ModuleArgs;
419+
420+
public class App {
421+
422+
public static void main(String[] args) {
423+
Pulumi.run(ctx -> {
424+
final var stackName = Deployment.getInstance().getStackName();
425+
426+
final var vpc = new Module("test-vpc", ModuleArgs.builder()
427+
.name("test-vpc-"+stackName)
428+
.azs("us-west-2a", "us-west-2b")
429+
.cidr("10.0.0.0/16")
430+
.public_subnets("10.0.1.0/24", "10.0.2.0/24")
431+
.private_subnets("10.0.3.0/24", "10.0.4.0/24")
432+
.enable_nat_gateway(true)
433+
.single_nat_gateway(true)
434+
.build());
435+
436+
ctx.export("publicSubnets", vpc.public_subnets());
437+
ctx.export("privateSubnets", vpc.private_subnets());
438+
});
439+
}
440+
}
441+
```
442+
{{% /choosable %}}
443+
444+
{{% choosable language yaml %}}
445+
446+
``` yaml
447+
name: my-program
448+
449+
runtime: yaml
450+
451+
resources:
452+
vpc:
453+
type: vpcmod:index:Module
454+
properties:
455+
azs:
456+
- us-west-2a
457+
- us-west-2b
458+
name: test-vpc-${pulumi.stack}
459+
cidr: 10.0.0.0/16
460+
public_subnets:
461+
- 10.0.1.0/24
462+
- 10.0.2.0/24
463+
private_subnets:
464+
- 10.0.3.0/24
465+
- 10.0.4.0/24
466+
enable_nat_gateway: true
467+
single_nat_gateway: true
468+
469+
outputs:
470+
publicSubnets: ${vpc.public_subnets}
471+
privateSubnets: ${vpc.private_subnets}
472+
473+
packages:
474+
vpcmod:
475+
source: terraform-module
476+
version: 0.1.7
477+
parameters:
478+
- terraform-aws-modules/vpc/aws
479+
- 6.0.0
480+
- vpcmod
481+
```
482+
483+
{{% /choosable %}}
484+
282485
{{% /chooser %}}
283486
284487
@@ -432,6 +635,7 @@ func run(ctx *pulumi.Context) error {
432635

433636
{{% /choosable %}}
434637

638+
435639
{{% /chooser %}}
436640

437641
This demonstrates how Terraform modules integrate seamlessly with existing Pulumi programs, allowing you to compose infrastructure components naturally.

0 commit comments

Comments
 (0)