Skip to content

Commit ff26a54

Browse files
committed
C# translation
1 parent c15d47c commit ff26a54

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

content/docs/iac/extending-pulumi/use-terraform-module.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ packages:
117117
- rdsmod
118118
```
119119
120-
{{% chooser language "typescript,python,yaml" %}}
120+
{{% chooser language "typescript,python,csharp,yaml" %}}
121121
122122
{{% choosable language typescript %}}
123123
@@ -422,6 +422,113 @@ func main() {
422422
```
423423
{{% /choosable %}}
424424

425+
{{% choosable language csharp %}}
426+
427+
Since this was a C# project, Pulumi generated a C# SDK for the modules, making those available to use as `Pulumi.Rdsmod` and `Pulumi.Vpcmod`. We can now use the Terraform modules directly in our code:
428+
429+
```csharp
430+
using System;
431+
using System.Linq;
432+
using System.Threading.Tasks;
433+
using System.Collections.Generic;
434+
using System.Collections.Immutable;
435+
using Pulumi;
436+
using Aws = Pulumi.Aws;
437+
using Rdsmod = Pulumi.Rdsmod;
438+
using Std = Pulumi.Std;
439+
using Vpcmod = Pulumi.Vpcmod;
440+
441+
return await Deployment.RunAsync(() =>
442+
{
443+
// Get available availability zones
444+
var azs = Aws.GetAvailabilityZones.Invoke(new Aws.GetAvailabilityZonesInvokeArgs
445+
{
446+
Filters =
447+
{
448+
new Aws.Inputs.GetAvailabilityZonesFilterInputArgs
449+
{
450+
Name = "opt-in-status",
451+
Values = { "opt-in-not-required" }
452+
}
453+
}
454+
}).Apply(result => result.Names.Take(3).ToArray());
455+
456+
var cidr = "10.0.0.0/16";
457+
458+
var config = new Pulumi.Config();
459+
var prefix = config.Get("prefix") ?? Deployment.Instance.StackName;
460+
461+
// Create a VPC using the terraform-aws-modules/vpc module
462+
var vpc = new Vpcmod.Module("test-vpc", new Vpcmod.ModuleArgs
463+
{
464+
Azs = azs,
465+
Name = Output.Format($"test-vpc-{prefix}"),
466+
Cidr = cidr,
467+
Public_subnets = Utils.Subnets(cidr, azs, 1),
468+
Private_subnets = Utils.Subnets(cidr, azs, 5),
469+
Database_subnets = Utils.Subnets(cidr, azs, 9),
470+
Create_database_subnet_group = true,
471+
});
472+
473+
// Create a security group for the RDS instance
474+
var rdsSecurityGroup = new Aws.Ec2.SecurityGroup("test-rds-sg", new Aws.Ec2.SecurityGroupArgs
475+
{
476+
VpcId = vpc.Vpc_id.Apply(id => id ?? string.Empty),
477+
});
478+
479+
_ = new Aws.Vpc.SecurityGroupIngressRule("test-rds-sg-ingress", new Aws.Vpc.SecurityGroupIngressRuleArgs
480+
{
481+
IpProtocol = "tcp",
482+
SecurityGroupId = rdsSecurityGroup.Id,
483+
CidrIpv4 = vpc.Vpc_cidr_block.Apply(x => x!),
484+
FromPort = 3306,
485+
ToPort = 3306,
486+
});
487+
488+
// Create an RDS instance using the terraform-aws-modules/rds module
489+
_ = new Rdsmod.Module("test-rds", new Rdsmod.ModuleArgs
490+
{
491+
Engine = "mysql",
492+
Identifier = Output.Format($"test-rds-{prefix}"),
493+
Manage_master_user_password = true,
494+
Publicly_accessible = false,
495+
Allocated_storage = 20,
496+
Max_allocated_storage = 100,
497+
Instance_class = "db.t4g.large",
498+
Engine_version = "8.0",
499+
Family = "mysql8.0",
500+
Db_name = "completeMysql",
501+
Username = "complete_mysql",
502+
Port = "3306",
503+
Multi_az = true,
504+
Db_subnet_group_name = vpc.Database_subnet_group_name,
505+
Vpc_security_group_ids = { rdsSecurityGroup.Id },
506+
Skip_final_snapshot = true,
507+
Deletion_protection = false,
508+
Create_db_option_group = false,
509+
Create_db_parameter_group = false,
510+
});
511+
});
512+
513+
// Utilities to calculate subnet CIDRs
514+
internal class Utils {
515+
public static Output<ImmutableArray<string>> Subnets(string cidr, Output<string[]> azs, int offset) {
516+
return azs.Apply(names => Pulumi.Output.All(names.Select((_, i) => Utils.GetCidrSubnet(cidr, i + 1))));
517+
}
518+
519+
public static Output<string> GetCidrSubnet(string cidr, int netnum)
520+
{
521+
return Std.Cidrsubnet.Invoke(new Std.CidrsubnetInvokeArgs
522+
{
523+
Input = cidr,
524+
Newbits = 8,
525+
Netnum = netnum
526+
}).Apply(result => result.Result);
527+
}
528+
}
529+
```
530+
{{% /choosable %}}
531+
425532
{{% choosable language yaml %}}
426533

427534
When authoring in YAML, there's no need for Pulumi to generate a SDK. In the YAML you can reference the Terraform module by its schema token, which takes the format `<module-name>:index:Module`:

0 commit comments

Comments
 (0)