Skip to content

Commit 09dac84

Browse files
norbinshrix0rrr
authored andcommitted
feat: Python example for APIGW with lambda integration and CORS (#104)
1 parent ce8c08b commit 09dac84

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ $ cdk destroy
8181

8282
| Example | Description |
8383
|---------|-------------|
84+
| [api-cors-lambda](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/api-cors-lambda/) | Shows creation of Rest API (GW) with an /example GET endpoint, with CORS enabled |
8485
| [application-load-balancer](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/application-load-balancer/) | Using an AutoScalingGroup with an Application Load Balancer |
8586
| [classic-load-balancer](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/classic-load-balancer/) | Using an AutoScalingGroup with a Classic Load Balancer |
8687
| [custom-resource](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/custom-resource/) | Shows adding a Custom Resource to your CDK app |

python/api-cors-lambda/app.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from aws_cdk import (
2+
core,
3+
aws_lambda as _lambda,
4+
aws_apigateway as _apigw
5+
)
6+
7+
8+
class ApiCorsLambdaStack(core.Stack):
9+
10+
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
11+
super().__init__(scope, id, **kwargs)
12+
13+
base_lambda = _lambda.Function(self,'ApiCorsLambda',
14+
handler='lambda-handler.handler',
15+
runtime=_lambda.Runtime.PYTHON_3_7,
16+
code=_lambda.Code.asset('lambda'),
17+
)
18+
19+
base_api = _apigw.RestApi(self, 'ApiGatewayWithCors',
20+
rest_api_name='ApiGatewayWithCors')
21+
22+
example_entity = base_api.root.add_resource('example')
23+
example_entity_lambda_integration = _apigw.LambdaIntegration(base_lambda,proxy=False, integration_responses=[
24+
{
25+
'statusCode': '200',
26+
'responseParameters': {
27+
'method.response.header.Access-Control-Allow-Origin': "'*'",
28+
}
29+
}
30+
]
31+
)
32+
example_entity.add_method('GET', example_entity_lambda_integration,
33+
method_responses=[{
34+
'statusCode': '200',
35+
'responseParameters': {
36+
'method.response.header.Access-Control-Allow-Origin': True,
37+
}
38+
}
39+
]
40+
)
41+
42+
self.add_cors_options(example_entity)
43+
44+
45+
def add_cors_options(self, apigw_resource):
46+
apigw_resource.add_method('OPTIONS', _apigw.MockIntegration(
47+
integration_responses=[{
48+
'statusCode': '200',
49+
'responseParameters': {
50+
'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
51+
'method.response.header.Access-Control-Allow-Origin': "'*'",
52+
'method.response.header.Access-Control-Allow-Methods': "'GET,OPTIONS'"
53+
}
54+
}
55+
],
56+
passthrough_behavior=_apigw.PassthroughBehavior.WHEN_NO_MATCH,
57+
request_templates={"application/json":"{\"statusCode\":200}"}
58+
),
59+
method_responses=[{
60+
'statusCode': '200',
61+
'responseParameters': {
62+
'method.response.header.Access-Control-Allow-Headers': True,
63+
'method.response.header.Access-Control-Allow-Methods': True,
64+
'method.response.header.Access-Control-Allow-Origin': True,
65+
}
66+
}
67+
],
68+
)
69+
70+
app = core.App()
71+
ApiCorsLambdaStack(app, "ApiCorsLambdaStack")
72+
app.synth()

python/api-cors-lambda/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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def handler(event, context):
2+
return {
3+
'statusCode': 200,
4+
'body': 'Lambda was invoked successfully.'
5+
}
6+

0 commit comments

Comments
 (0)