Skip to content

Commit 4f0954d

Browse files
garnaatrix0rrr
authored andcommitted
feat: add Python examples (aws-samples#25)
1 parent 7af4131 commit 4f0954d

File tree

47 files changed

+882
-0
lines changed

Some content is hidden

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

47 files changed

+882
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ node_modules
33
*.swp
44
cdk.context.json
55
package-lock.json
6+
__pycache__
7+
.env
8+
*~
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
from aws_cdk import (
3+
aws_autoscaling as autoscaling,
4+
aws_ec2 as ec2,
5+
aws_elasticloadbalancingv2 as elbv2,
6+
cdk,
7+
)
8+
9+
10+
class LoadBalancerStack(cdk.Stack):
11+
def __init__(self, app: cdk.App, id: str) -> None:
12+
super().__init__(app, id)
13+
14+
vpc = ec2.VpcNetwork(self, "VPC")
15+
16+
asg = autoscaling.AutoScalingGroup(
17+
self,
18+
"ASG",
19+
vpc=vpc,
20+
instance_type=ec2.InstanceTypePair(
21+
ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro
22+
),
23+
machine_image=ec2.AmazonLinuxImage(),
24+
)
25+
26+
lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
27+
28+
listener = lb.add_listener("Listener", port=80)
29+
listener.add_targets("Target", port=80, targets=[asg])
30+
listener.connections.allow_default_port_from_any_ipv4("Open to the world")
31+
32+
asg.scale_on_request_count("AModestLoad", target_requests_per_second=1)
33+
34+
35+
app = cdk.App()
36+
LoadBalancerStack(app, "LoadBalancerStack")
37+
app.run()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "python3 app.py"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
aws-cdk.cdk
2+
3+
aws-cdk.aws-autoscaling
4+
aws-cdk.aws-ec2
5+
aws-cdk.aws-elasticloadbalancingv2
6+
7+
# Work around for jsii#413
8+
aws-cdk.aws-autoscaling-common

python/chat-app/app.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from aws_cdk import cdk
2+
from aws_cdk import aws_lambda as lambda_, aws_s3 as s3
3+
4+
from cognito_chat_room_pool import CognitoChatRoomPool
5+
from dynamodb_posts_table import DynamoPostsTable
6+
7+
8+
class ChatAppFunction(lambda_.Function):
9+
def __init__(
10+
self, scope: cdk.Construct, id: str, *, bucket: s3.IBucket, zip_file: str
11+
):
12+
super().__init__(
13+
scope,
14+
id,
15+
code=lambda_.S3Code(bucket, zip_file),
16+
runtime=lambda_.Runtime.NODE_J_S610,
17+
handler="index.handler",
18+
)
19+
20+
21+
class MyStack(cdk.Stack):
22+
def __init__(self, scope: cdk.App, id: str, **kwargs):
23+
super().__init__(scope, id, **kwargs)
24+
25+
DynamoPostsTable(self, "Posts")
26+
CognitoChatRoomPool(self, "UserPool")
27+
28+
bucket = s3.Bucket.import_(self, "DougsBucket", bucket_name="dogs-chat-app")
29+
30+
ChatAppFunction(
31+
self,
32+
"StartAddBucket",
33+
bucket=bucket,
34+
zip_file="StartAddingPendingCognitoUser.zip",
35+
)
36+
37+
ChatAppFunction(
38+
self,
39+
"FinishAddBucket",
40+
bucket=bucket,
41+
zip_file="FinishAddingPendingCognitoUser.zip",
42+
)
43+
44+
ChatAppFunction(
45+
self,
46+
"SignInUserBucket",
47+
bucket=bucket,
48+
zip_file="SignInCognitoUser.zip",
49+
)
50+
51+
ChatAppFunction(
52+
self,
53+
"VerifyBucket",
54+
bucket=bucket,
55+
zip_file="VerifyCognitoSignIn.zip",
56+
)
57+
58+
ChatAppFunction(
59+
self,
60+
"StartChangeBucket",
61+
bucket=bucket,
62+
zip_file="StartChangingForgottenCognitoUserPassword.zip",
63+
)
64+
65+
ChatAppFunction(
66+
self,
67+
"FinishChangeBucket",
68+
bucket=bucket,
69+
zip_file="FinishChangingForgottenCognitoUserPassword.zip",
70+
)
71+
72+
ChatAppFunction(
73+
self,
74+
"GetPostsBucket",
75+
bucket=bucket,
76+
zip_file="GetPosts.zip",
77+
)
78+
79+
ChatAppFunction(
80+
self,
81+
"AddPostBucket",
82+
bucket=bucket,
83+
zip_file="AddPost.zip",
84+
)
85+
86+
ChatAppFunction(
87+
self,
88+
"DeletePostBucket",
89+
bucket=bucket,
90+
zip_file="DeletePost.zip",
91+
)
92+
93+
ChatAppFunction(
94+
self,
95+
"DeleteUserBucket",
96+
bucket=bucket,
97+
zip_file="DeleteCognitoUser.zip",
98+
)
99+
100+
101+
app = cdk.App()
102+
103+
# Add the stack to the app
104+
# (Apps can hot many stacks, for example, one for each region)
105+
MyStack(app, "ChatAppStack", env={"region": "us-west-2"})
106+
107+
app.run()

python/chat-app/cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "python3 app.py"
3+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from aws_cdk import aws_cognito as cognito, cdk
2+
3+
4+
class CognitoChatRoomPool(cdk.Construct):
5+
def __init__(self, scope: cdk.Construct, id: str) -> None:
6+
super().__init__(scope, id)
7+
8+
# Create chat room user pool
9+
chat_pool = cognito.CfnUserPool(
10+
self,
11+
"UserPool",
12+
admin_create_user_config={"allowAdminCreateUserOnly": False},
13+
policies={"passwordPolicy": {"minimumLength": 6, "requireNumbers": True}},
14+
schema=[{"attributeDataType": "String", "name": "email", "required": True}],
15+
auto_verified_attributes=["email"],
16+
)
17+
18+
# Now for the client
19+
cognito.CfnUserPoolClient(
20+
self,
21+
"UserPoolClient",
22+
client_name="Chat-Room",
23+
explicit_auth_flows=["ADMIN_NO_SRP_AUTH"],
24+
refresh_token_validity=30,
25+
user_pool_id=chat_pool.ref,
26+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from aws_cdk import cdk
2+
from aws_cdk import aws_dynamodb as dynanodb
3+
4+
5+
class DynamoPostsTable(cdk.Construct):
6+
def __init__(self, scope: cdk.Construct, id: str) -> None:
7+
super().__init__(scope, id)
8+
9+
dynanodb.Table(
10+
self,
11+
"Table",
12+
partition_key={"name": "Alias", "type": dynanodb.AttributeType.String},
13+
sort_key={"name": "Timestamp", "type": dynanodb.AttributeType.String},
14+
read_capacity=5,
15+
write_capacity=5,
16+
)

python/chat-app/requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
aws-cdk.cdk
2+
3+
aws-cdk.aws-cognito
4+
aws-cdk.aws-lambda
5+
aws-cdk.aws-dynamodb
6+
aws-cdk.aws-s3
7+
8+
# Work around for jsii#413
9+
aws-cdk.aws-autoscaling-common
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from aws_cdk import cdk
2+
from aws_cdk import (
3+
aws_autoscaling as autoscaling,
4+
aws_ec2 as ec2,
5+
aws_elasticloadbalancing as elb,
6+
)
7+
8+
9+
class LoadBalancerStack(cdk.Stack):
10+
def __init__(self, app: cdk.App, id: str, **kwargs) -> None:
11+
super().__init__(app, id, **kwargs)
12+
13+
vpc = ec2.VpcNetwork(self, "VPC")
14+
15+
asg = autoscaling.AutoScalingGroup(
16+
self,
17+
"ASG",
18+
vpc=vpc,
19+
instance_type=ec2.InstanceTypePair(
20+
ec2.InstanceClass.Burstable2, ec2.InstanceSize.Micro
21+
),
22+
machine_image=ec2.AmazonLinuxImage(),
23+
)
24+
25+
lb = elb.LoadBalancer(
26+
self, "LB", vpc=vpc, internet_facing=True, health_check={"port": 80}
27+
)
28+
lb.add_target(asg)
29+
30+
listener = lb.add_listener(external_port=80)
31+
listener.connections.allow_default_port_from_any_ipv4("Open to the world")
32+
33+
34+
app = cdk.App()
35+
LoadBalancerStack(app, "LoadBalancerStack")
36+
app.run()

0 commit comments

Comments
 (0)