Skip to content

Commit 8d2f40d

Browse files
committed
Add sample Lambda function and API Gateway configuration for Hello World example
1 parent 40a8467 commit 8d2f40d

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

sample/first_code/app.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
3+
# import requests
4+
5+
6+
def lambda_handler(event, context):
7+
"""Sample pure Lambda function
8+
9+
Parameters
10+
----------
11+
event: dict, required
12+
API Gateway Lambda Proxy Input Format
13+
14+
Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
15+
16+
context: object, required
17+
Lambda Context runtime methods and attributes
18+
19+
Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
20+
21+
Returns
22+
------
23+
API Gateway Lambda Proxy Output Format: dict
24+
25+
Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
26+
"""
27+
28+
# try:
29+
# ip = requests.get("http://checkip.amazonaws.com/")
30+
# except requests.RequestException as e:
31+
# # Send some context about this error to Lambda Logs
32+
# print(e)
33+
34+
# raise e
35+
36+
return {
37+
"statusCode": 200,
38+
"body": json.dumps({
39+
"message": "hello world",
40+
# "location": ip.text.replace("\n", "")
41+
}),
42+
}

sample/first_code/template.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
powertools-quickstart
5+
6+
Sample SAM Template for powertools-quickstart
7+
8+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
9+
Globals:
10+
Function:
11+
Timeout: 3
12+
13+
Resources:
14+
HelloWorldFunction:
15+
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
16+
Properties:
17+
CodeUri: hello_world/
18+
Handler: app.lambda_handler
19+
Runtime: python3.12
20+
Architectures:
21+
- x86_64
22+
Events:
23+
HelloWorld:
24+
Type: Api
25+
Properties:
26+
Path: /hello
27+
Method: get
28+
29+
Outputs:
30+
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
31+
# Find out more about other implicit resources you can reference within SAM
32+
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
33+
HelloWorldApi:
34+
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
35+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Prod/hello/"
36+
HelloWorldFunction:
37+
Description: "Hello World Lambda Function ARN"
38+
Value: !GetAtt HelloWorldFunction.Arn
39+
HelloWorldFunctionIamRole:
40+
Description: "Implicit IAM Role created for Hello World function"
41+
Value: !GetAtt HelloWorldFunctionRole.Arn

sample/self_router/app.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
3+
4+
def hello_name(event, **kargs):
5+
username = event["pathParameters"]["name"]
6+
return {"statusCode": 200, "body": json.dumps({"message": f"hello {username}!"})}
7+
8+
9+
def hello(**kargs):
10+
return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
11+
12+
13+
class Router:
14+
def __init__(self):
15+
self.routes = {}
16+
17+
def set(self, path, method, handler):
18+
self.routes[f"{path}-{method}"] = handler
19+
20+
def get(self, path, method):
21+
try:
22+
route = self.routes[f"{path}-{method}"]
23+
except KeyError:
24+
raise RuntimeError(f"Cannot route request to the correct method. path={path}, method={method}")
25+
return route
26+
27+
router = Router()
28+
router.set(path="/hello", method="GET", handler=hello)
29+
router.set(path="/hello/{name}", method="GET", handler=hello_name)
30+
31+
32+
def lambda_handler(event, context):
33+
path = event["resource"]
34+
http_method = event["httpMethod"]
35+
method = router.get(path=path, method=http_method)
36+
return method(event=event)

sample/self_router/template.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
powertools-quickstart
5+
6+
Sample SAM Template for powertools-quickstart
7+
8+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
9+
Globals:
10+
Function:
11+
Timeout: 3
12+
13+
Resources:
14+
HelloWorldFunction:
15+
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
16+
Properties:
17+
CodeUri: hello_world/
18+
Handler: app.lambda_handler
19+
Runtime: python3.12
20+
Architectures:
21+
- x86_64
22+
Events:
23+
HelloWorld:
24+
Type: Api
25+
Properties:
26+
Path: /hello
27+
Method: get
28+
HelloWorldName:
29+
Type: Api
30+
Properties:
31+
Path: /hello/{name}
32+
Method: get
33+
34+
Outputs:
35+
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
36+
# Find out more about other implicit resources you can reference within SAM
37+
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
38+
HelloWorldApi:
39+
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
40+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Prod/hello/"
41+
HelloWorldFunction:
42+
Description: "Hello World Lambda Function ARN"
43+
Value: !GetAtt HelloWorldFunction.Arn
44+
HelloWorldFunctionIamRole:
45+
Description: "Implicit IAM Role created for Hello World function"
46+
Value: !GetAtt HelloWorldFunctionRole.Arn

0 commit comments

Comments
 (0)