Skip to content

Commit f0732dc

Browse files
authored
Update Python examples to use classes (pulumi#801)
1 parent c2571f4 commit f0732dc

File tree

50 files changed

+1137
-1028
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1137
-1028
lines changed

aws-py-appsync/__main__.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
table = dynamodb.Table(
88
"tenants",
99
hash_key="id",
10-
attributes=[{
11-
"name": "id",
12-
"type": "S"
13-
}],
10+
attributes=[dynamodb.TableAttributeArgs(
11+
name="id",
12+
type="S"
13+
)],
1414
read_capacity=1,
1515
write_capacity=1)
1616

@@ -44,7 +44,7 @@
4444

4545
attachment = iam.RolePolicyAttachment(
4646
"iam-policy-attachment",
47-
role=role,
47+
role=role.name,
4848
policy_arn=policy.arn)
4949

5050
## GraphQL Schema
@@ -82,8 +82,8 @@
8282
random_string = random.RandomString(
8383
"random-datasource-name",
8484
length=15,
85-
special="false",
86-
number="false",
85+
special=False,
86+
number=False,
8787
)
8888

8989
## Link a data source to the Dynamo DB Table
@@ -92,9 +92,9 @@
9292
name=random_string.result,
9393
api_id=api.id,
9494
type="AMAZON_DYNAMODB",
95-
dynamodb_config={
96-
"table_name": table.name
97-
},
95+
dynamodb_config=appsync.DataSourceDynamodbConfigArgs(
96+
table_name=table.name,
97+
),
9898
service_role_arn=role.arn)
9999

100100
## A resolver for the [getTenantById] query

aws-py-ec2-provisioners/__main__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ def decode_key(key):
2525
secgrp = aws.ec2.SecurityGroup('secgrp',
2626
description='Foo',
2727
ingress=[
28-
{ 'protocol': 'tcp', 'from_port': 22, 'to_port': 22, 'cidr_blocks': ['0.0.0.0/0'] },
29-
{ 'protocol': 'tcp', 'from_port': 80, 'to_port': 80, 'cidr_blocks': ['0.0.0.0/0'] },
28+
aws.ec2.SecurityGroupIngressArgs(protocol='tcp', from_port=22, to_port=22, cidr_blocks=['0.0.0.0/0']),
29+
aws.ec2.SecurityGroupIngressArgs(protocol='tcp', from_port=80, to_port=80, cidr_blocks=['0.0.0.0/0']),
3030
],
3131
)
3232

3333
# Get the AMI
3434
ami = aws.get_ami(
3535
owners=['amazon'],
3636
most_recent=True,
37-
filters=[{
38-
'name': 'name',
39-
'values': ['amzn2-ami-hvm-2.0.????????-x86_64-gp2'],
40-
}],
37+
filters=[aws.GetAmiFilterArgs(
38+
name='name',
39+
values=['amzn2-ami-hvm-2.0.????????-x86_64-gp2'],
40+
)],
4141
)
4242

4343
# Create an EC2 server that we'll then provision stuff onto.

aws-py-eks/__main__.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
eks_cluster = eks.Cluster(
99
'eks-cluster',
1010
role_arn=iam.eks_role.arn,
11-
tags= {'Name':'pulumi-eks-cluster'},
12-
vpc_config = {
13-
'publicAccessCidrs': ['0.0.0.0/0'],
14-
'security_group_ids': [vpc.eks_security_group.id],
15-
'subnet_ids': vpc.subnet_ids,
16-
}
11+
tags={
12+
'Name': 'pulumi-eks-cluster',
13+
},
14+
vpc_config=eks.ClusterVpcConfigArgs(
15+
public_access_cidrs=['0.0.0.0/0'],
16+
security_group_ids=[vpc.eks_security_group.id],
17+
subnet_ids=vpc.subnet_ids,
18+
),
1719
)
1820

1921
eks_node_group = eks.NodeGroup(
@@ -23,13 +25,13 @@
2325
node_role_arn=iam.ec2_role.arn,
2426
subnet_ids=vpc.subnet_ids,
2527
tags={
26-
'Name' : 'pulumi-cluster-nodeGroup'
28+
'Name': 'pulumi-cluster-nodeGroup',
2729
},
28-
scaling_config = {
29-
'desired_size': 2,
30-
'max_size': 2,
31-
'min_size': 1,
32-
},
30+
scaling_config=eks.NodeGroupScalingConfigArgs(
31+
desired_size=2,
32+
max_size=2,
33+
min_size=1,
34+
),
3335
)
3436

3537
pulumi.export('cluster-name', eks_cluster.name)

aws-py-eks/iam.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
eks_role = iam.Role(
77
'eks-iam-role',
8-
98
assume_role_policy=json.dumps({
109
'Version': '2012-10-17',
1110
'Statement': [
@@ -17,21 +16,21 @@
1716
'Effect': 'Allow',
1817
'Sid': ''
1918
}
20-
]
21-
})
19+
],
20+
}),
2221
)
2322

2423
iam.RolePolicyAttachment(
2524
'eks-service-policy-attachment',
2625
role=eks_role.id,
27-
policy_arn='arn:aws:iam::aws:policy/AmazonEKSServicePolicy'
28-
)
26+
policy_arn='arn:aws:iam::aws:policy/AmazonEKSServicePolicy',
27+
)
2928

3029

3130
iam.RolePolicyAttachment(
3231
'eks-cluster-policy-attachment',
3332
role=eks_role.id,
34-
policy_arn='arn:aws:iam::aws:policy/AmazonEKSClusterPolicy'
33+
policy_arn='arn:aws:iam::aws:policy/AmazonEKSClusterPolicy',
3534
)
3635

3736
## Ec2 NodeGroup Role
@@ -49,25 +48,25 @@
4948
'Effect': 'Allow',
5049
'Sid': ''
5150
}
52-
]
53-
})
51+
],
52+
}),
5453
)
5554

5655
iam.RolePolicyAttachment(
5756
'eks-workernode-policy-attachment',
5857
role=ec2_role.id,
59-
policy_arn='arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy'
58+
policy_arn='arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy',
6059
)
6160

6261

6362
iam.RolePolicyAttachment(
6463
'eks-cni-policy-attachment',
6564
role=ec2_role.id,
66-
policy_arn='arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy'
65+
policy_arn='arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy',
6766
)
6867

6968
iam.RolePolicyAttachment(
7069
'ec2-container-ro-policy-attachment',
7170
role=ec2_role.id,
72-
policy_arn='arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly'
71+
policy_arn='arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly',
7372
)

aws-py-eks/vpc.py

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
1-
from pulumi_aws import config, ec2, get_availability_zones
1+
from pulumi_aws import ec2, get_availability_zones
22

33
## VPC
44

55
vpc = ec2.Vpc(
6-
'eks-vpc',
6+
'eks-vpc',
77
cidr_block='10.100.0.0/16',
88
instance_tenancy='default',
99
enable_dns_hostnames=True,
1010
enable_dns_support=True,
1111
tags={
12-
'Name' : 'pulumi-eks-vpc'
13-
}
12+
'Name': 'pulumi-eks-vpc',
13+
},
1414
)
1515

1616
igw = ec2.InternetGateway(
1717
'vpc-ig',
1818
vpc_id=vpc.id,
1919
tags={
20-
'Name' : 'pulumi-vpc-ig'
21-
}
20+
'Name': 'pulumi-vpc-ig',
21+
},
2222
)
2323

2424
eks_route_table = ec2.RouteTable(
2525
'vpc-route-table',
2626
vpc_id=vpc.id,
27-
routes=[{
28-
'cidr_block' : '0.0.0.0/0',
29-
'gateway_id' : igw.id
30-
}
31-
],
27+
routes=[ec2.RouteTableRouteArgs(
28+
cidr_block='0.0.0.0/0',
29+
gateway_id=igw.id,
30+
)],
3231
tags={
33-
'Name' : 'pulumi-vpc-rt'
34-
}
32+
'Name': 'pulumi-vpc-rt',
33+
},
3534
)
3635

3736
## Subnets, one for each AZ in a region
@@ -41,15 +40,15 @@
4140

4241
for zone in zones.names:
4342
vpc_subnet = ec2.Subnet(
44-
f'vpc-subnet-{zone}' ,
43+
f'vpc-subnet-{zone}',
4544
assign_ipv6_address_on_creation=False,
4645
vpc_id=vpc.id,
4746
map_public_ip_on_launch=True,
4847
cidr_block=f'10.100.{len(subnet_ids)}.0/24',
49-
availability_zone= zone,
48+
availability_zone=zone,
5049
tags={
51-
'Name' : f'pulumi-sn-{zone}'
52-
}
50+
'Name': f'pulumi-sn-{zone}',
51+
},
5352
)
5453
ec2.RouteTableAssociation(
5554
f'vpc-route-table-assoc-{zone}',
@@ -65,21 +64,22 @@
6564
vpc_id=vpc.id,
6665
description='Allow all HTTP(s) traffic to EKS Cluster',
6766
tags={
68-
'Name' : 'pulumi-cluster-sg'
67+
'Name': 'pulumi-cluster-sg',
6968
},
70-
ingress=[{
71-
'cidr_blocks' : ['0.0.0.0/0'],
72-
'from_port' : '443',
73-
'to_port' : '443',
74-
'protocol' : 'tcp',
75-
'description' : 'Allow pods to communicate with the cluster API Server.'
76-
},
77-
{
78-
'cidr_blocks' : ['0.0.0.0/0'],
79-
'from_port' : '80',
80-
'to_port' : '80',
81-
'protocol' : 'tcp',
82-
'description' : 'Allow internet access to pods'
83-
}
84-
]
69+
ingress=[
70+
ec2.SecurityGroupIngressArgs(
71+
cidr_blocks=['0.0.0.0/0'],
72+
from_port=443,
73+
to_port=443,
74+
protocol='tcp',
75+
description='Allow pods to communicate with the cluster API Server.'
76+
),
77+
ec2.SecurityGroupIngressArgs(
78+
cidr_blocks=['0.0.0.0/0'],
79+
from_port=80,
80+
to_port=80,
81+
protocol='tcp',
82+
description='Allow internet access to pods'
83+
),
84+
],
8585
)

aws-py-fargate/__main__.py

+31-31
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,47 @@
66
cluster = aws.ecs.Cluster('cluster')
77

88
# Read back the default VPC and public subnets, which we will use.
9-
default_vpc = aws.ec2.get_vpc(default='true')
9+
default_vpc = aws.ec2.get_vpc(default=True)
1010
default_vpc_subnets = aws.ec2.get_subnet_ids(vpc_id=default_vpc.id)
1111

1212
# Create a SecurityGroup that permits HTTP ingress and unrestricted egress.
1313
group = aws.ec2.SecurityGroup('web-secgrp',
1414
vpc_id=default_vpc.id,
1515
description='Enable HTTP access',
16-
ingress=[{
17-
'protocol': 'tcp',
18-
'from_port': 80,
19-
'to_port': 80,
20-
'cidr_blocks': ['0.0.0.0/0'],
21-
}],
22-
egress=[{
23-
'protocol': '-1',
24-
'from_port': 0,
25-
'to_port': 0,
26-
'cidr_blocks': ['0.0.0.0/0'],
27-
}]
16+
ingress=[aws.ec2.SecurityGroupIngressArgs(
17+
protocol='tcp',
18+
from_port=80,
19+
to_port=80,
20+
cidr_blocks=['0.0.0.0/0'],
21+
)],
22+
egress=[aws.ec2.SecurityGroupEgressArgs(
23+
protocol='-1',
24+
from_port=0,
25+
to_port=0,
26+
cidr_blocks=['0.0.0.0/0'],
27+
)],
2828
)
2929

3030
# Create a load balancer to listen for HTTP traffic on port 80.
3131
alb = aws.lb.LoadBalancer('app-lb',
3232
security_groups=[group.id],
33-
subnets=default_vpc_subnets.ids
33+
subnets=default_vpc_subnets.ids,
3434
)
3535

3636
atg = aws.lb.TargetGroup('app-tg',
3737
port=80,
3838
protocol='HTTP',
3939
target_type='ip',
40-
vpc_id=default_vpc.id
40+
vpc_id=default_vpc.id,
4141
)
4242

4343
wl = aws.lb.Listener('web',
4444
load_balancer_arn=alb.arn,
4545
port=80,
46-
default_actions=[{
47-
'type': 'forward',
48-
'target_group_arn': atg.arn
49-
}]
46+
default_actions=[aws.lb.ListenerDefaultActionArgs(
47+
type='forward',
48+
target_group_arn=atg.arn,
49+
)],
5050
)
5151

5252
# Create an IAM role that can be used by our service's task.
@@ -61,12 +61,12 @@
6161
},
6262
'Action': 'sts:AssumeRole',
6363
}]
64-
})
64+
}),
6565
)
6666

6767
rpa = aws.iam.RolePolicyAttachment('task-exec-policy',
6868
role=role.name,
69-
policy_arn='arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
69+
policy_arn='arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy',
7070
)
7171

7272
# Spin up a load balanced service running our container image.
@@ -93,16 +93,16 @@
9393
desired_count=3,
9494
launch_type='FARGATE',
9595
task_definition=task_definition.arn,
96-
network_configuration={
97-
'assign_public_ip': 'true',
98-
'subnets': default_vpc_subnets.ids,
99-
'security_groups': [group.id]
100-
},
101-
load_balancers=[{
102-
'target_group_arn': atg.arn,
103-
'container_name': 'my-app',
104-
'container_port': 80
105-
}],
96+
network_configuration=aws.ecs.ServiceNetworkConfigurationArgs(
97+
assign_public_ip=True,
98+
subnets=default_vpc_subnets.ids,
99+
security_groups=[group.id],
100+
),
101+
load_balancers=[aws.ecs.ServiceLoadBalancerArgs(
102+
target_group_arn=atg.arn,
103+
container_name='my-app',
104+
container_port=80,
105+
)],
106106
opts=ResourceOptions(depends_on=[wl]),
107107
)
108108

0 commit comments

Comments
 (0)