Skip to content

Commit 36da3d3

Browse files
committed
adding files to code sample
1 parent a92ed17 commit 36da3d3

17 files changed

+253
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.swp
2+
package-lock.json
3+
__pycache__
4+
.pytest_cache
5+
.venv
6+
*.egg-info
7+
dist/
8+
9+
# CDK asset staging directory
10+
.cdk.staging
11+
cdk.out

.gitlab-ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
image: public.ecr.aws/docker/library/python:latest
2+
3+
stages:
4+
- test
5+
- build
6+
- publish
7+
8+
test:
9+
script:
10+
- apt-get update -y
11+
- apt-get install npm -y
12+
- npm install -g aws-cdk
13+
- python -m ensurepip --upgrade
14+
- python -m pip install --upgrade pip
15+
- python -m pip install --upgrade virtualenv
16+
- python -m venv .venv
17+
- source .venv/bin/activate
18+
- pip install -r requirements.txt
19+
# - cdk synth --json
20+
publish:
21+
script:
22+
- pip install build twine
23+
- python -m build
24+
- TWINE_PASSWORD=${TWINE_PASSWORD} TWINE_USERNAME=${TWINE_USERNAME} python -m twine upload --repository-url https://${CODE_ARTIFACT_DOMAIN}-${AWS_ACCOUNT_ID}.d.codeartifact.${AWS_REGION}.amazonaws.com/pypi/${CODE_ARTIFACT_REPO_NAME}/ dist/*
25+
needs: ["test"]

app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
3+
import aws_cdk as cdk
4+
5+
from cdk_python_module.cdk_workshop_stack import CdkWorkshopStack
6+
7+
8+
app = cdk.App()
9+
CdkWorkshopStack(app, "cdk-workshop")
10+
11+
app.synth()
12+

cdk.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"app": "python3 app.py",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"requirements*.txt",
11+
"source.bat",
12+
"**/__init__.py",
13+
"python/__pycache__",
14+
"tests"
15+
]
16+
},
17+
"context": {
18+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
19+
"@aws-cdk/core:checkSecretUsage": true,
20+
"@aws-cdk/core:target-partitions": [
21+
"aws",
22+
"aws-cn"
23+
],
24+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
25+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
26+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
27+
"@aws-cdk/aws-iam:minimizePolicies": true,
28+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
29+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
30+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
31+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
32+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
33+
"@aws-cdk/core:enablePartitionLiterals": true,
34+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
35+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
36+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true
37+
}
38+
}

cdk_python_module/__init__.py

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from constructs import Construct
2+
from aws_cdk import (
3+
Stack,
4+
aws_lambda as _lambda,
5+
aws_apigateway as apigw,
6+
)
7+
8+
from cdk_dynamo_table_view import TableViewer
9+
from .hitcounter import HitCounter
10+
11+
12+
class CdkWorkshopStack(Stack):
13+
14+
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
15+
super().__init__(scope, id, **kwargs)
16+
17+
# Defines an AWS Lambda resource
18+
hello = _lambda.Function(
19+
self, 'HelloHandler',
20+
runtime=_lambda.Runtime.PYTHON_3_7,
21+
code=_lambda.Code.from_asset('lambda'),
22+
handler='hello.handler',
23+
)
24+
25+
hello_with_counter = HitCounter(
26+
self, 'HelloHitCounter',
27+
downstream=hello,
28+
)
29+
30+
apigw.LambdaRestApi(
31+
self, 'Endpoint',
32+
handler=hello_with_counter._handler,
33+
)
34+
35+
TableViewer(
36+
self, 'ViewHitCounter',
37+
title='Hello Hits',
38+
table=hello_with_counter.table,
39+
)
40+

cdk_python_module/hello.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import json
2+
3+
def handler(event, context):
4+
print('request: {}'.format(json.dumps(event)))
5+
return {
6+
'statusCode': 200,
7+
'headers': {
8+
'Content-Type': 'text/plain'
9+
},
10+
'body': 'Good Night, CDK! You have hit {}\n'.format(event['path'])
11+
}
12+

cdk_python_module/hitcount.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
4+
import boto3
5+
6+
ddb = boto3.resource('dynamodb')
7+
table = ddb.Table(os.environ['HITS_TABLE_NAME'])
8+
_lambda = boto3.client('lambda')
9+
10+
11+
def handler(event, context):
12+
print('request: {}'.format(json.dumps(event)))
13+
table.update_item(
14+
Key={'path': event['path']},
15+
UpdateExpression='ADD hits :incr',
16+
ExpressionAttributeValues={':incr': 1}
17+
)
18+
19+
resp = _lambda.invoke(
20+
FunctionName=os.environ['DOWNSTREAM_FUNCTION_NAME'],
21+
Payload=json.dumps(event),
22+
)
23+
24+
body = resp['Payload'].read()
25+
26+
print('downstream response: {}'.format(body))
27+
return json.loads(body)
28+

cdk_python_module/hitcounter.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from constructs import Construct
2+
from aws_cdk import (
3+
aws_lambda as _lambda,
4+
aws_dynamodb as ddb,
5+
)
6+
7+
8+
class HitCounter(Construct):
9+
10+
@property
11+
def handler(self):
12+
return self._handler
13+
14+
@property
15+
def table(self):
16+
return self._table
17+
18+
def __init__(self, scope: Construct, id: str, downstream: _lambda.IFunction, **kwargs):
19+
super().__init__(scope, id, **kwargs)
20+
21+
self._table = ddb.Table(
22+
self, 'Hits',
23+
partition_key={'name': 'path', 'type': ddb.AttributeType.STRING}
24+
)
25+
26+
self._handler = _lambda.Function(
27+
self, 'HitCountHandler',
28+
runtime=_lambda.Runtime.PYTHON_3_7,
29+
handler='hitcount.handler',
30+
code=_lambda.Code.from_asset('lambda'),
31+
environment={
32+
'DOWNSTREAM_FUNCTION_NAME': downstream.function_name,
33+
'HITS_TABLE_NAME': self._table.table_name,
34+
}
35+
)
36+
37+
self._table.grant_read_write_data(self.handler)
38+
downstream.grant_invoke(self.handler)
39+

0 commit comments

Comments
 (0)