Skip to content

Commit d81b4ca

Browse files
committed
Add Python sources
1 parent 0c75151 commit d81b4ca

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ packages:
117117
- rdsmod
118118
```
119119
120-
{{% chooser language "typescript,yaml" %}}
120+
{{% chooser language "typescript,python,yaml" %}}
121121
122122
{{% choosable language typescript %}}
123123
@@ -210,6 +210,88 @@ function getCidrSubnet(cidr: string, netnum: number): pulumi.Output<string> {
210210

211211
{{% /choosable %}}
212212

213+
{{% choosable language python %}}
214+
215+
Since this was a Python project, Pulumi generated a Python SDK for the modules, making those available to use as `pulumi_vpcmod` and `pulumi_rdsmod` respectively. We can now use the Terraform modules directly in our code:
216+
217+
```python
218+
import pulumi
219+
import pulumi_aws as aws
220+
import pulumi_vpcmod as vpcmod
221+
import pulumi_rdsmod as rdsmod
222+
import pulumi_std as std
223+
224+
# Get available availability zones
225+
azs = aws.get_availability_zones_output(
226+
filters=[{
227+
"name": "opt-in-status",
228+
"values": ["opt-in-not-required"],
229+
}]
230+
).names.apply(lambda names: names[:3])
231+
232+
cidr = "10.0.0.0/16"
233+
234+
cfg = pulumi.Config()
235+
prefix = cfg.get("prefix") or pulumi.get_stack()
236+
237+
# Utility function to calculate subnet CIDRs
238+
def get_cidr_subnet(cidr, netnum):
239+
return std.cidrsubnet_output(
240+
input=cidr,
241+
newbits=8,
242+
netnum=netnum
243+
).result
244+
245+
# Create a VPC using the terraform-aws-modules/vpc module
246+
vpc = vpcmod.Module("test-vpc",
247+
azs=azs,
248+
name=f"test-vpc-{prefix}",
249+
cidr=cidr,
250+
public_subnets=azs.apply(lambda azs: [get_cidr_subnet(cidr, i+1) for i in range(len(azs))]),
251+
private_subnets=azs.apply(lambda azs: [get_cidr_subnet(cidr, i+1+4) for i in range(len(azs))]),
252+
database_subnets=azs.apply(lambda azs: [get_cidr_subnet(cidr, i+1+8) for i in range(len(azs))]),
253+
create_database_subnet_group=True
254+
)
255+
256+
# Create a security group for the RDS instance
257+
rds_security_group = aws.ec2.SecurityGroup('test-rds-sg',
258+
vpc_id=vpc.vpc_id
259+
)
260+
261+
aws.vpc.SecurityGroupIngressRule('test-rds-sg-ingress',
262+
ip_protocol='tcp',
263+
security_group_id=rds_security_group.id,
264+
cidr_ipv4=vpc.vpc_cidr_block,
265+
from_port=3306,
266+
to_port=3306
267+
)
268+
269+
# Create an RDS instance using the terraform-aws-modules/rds module
270+
rdsmod.Module("test-rds",
271+
engine="mysql",
272+
identifier=f"test-rds-{prefix}",
273+
manage_master_user_password=True,
274+
publicly_accessible=False,
275+
allocated_storage=20,
276+
max_allocated_storage=100,
277+
instance_class="db.t4g.large",
278+
engine_version="8.0",
279+
family="mysql8.0",
280+
db_name="completeMysql",
281+
username="complete_mysql",
282+
port='3306',
283+
multi_az=True,
284+
db_subnet_group_name=vpc.database_subnet_group_name,
285+
vpc_security_group_ids=[rds_security_group.id],
286+
skip_final_snapshot=True,
287+
deletion_protection=False,
288+
create_db_option_group=False,
289+
create_db_parameter_group=False
290+
)
291+
```
292+
293+
{{% /choosable %}}
294+
213295
{{% choosable language yaml %}}
214296

215297
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)