Skip to content

Commit 7d11bf5

Browse files
committed
more details in example's readme
1 parent d70f081 commit 7d11bf5

File tree

2 files changed

+138
-7
lines changed

2 files changed

+138
-7
lines changed

Examples/APIGateway/README.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,124 @@
1-
TO BE DONE
1+
# API Gateway
2+
3+
This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway.
4+
5+
## Code
6+
7+
The Lambda function takes all HTTP headers it receives as input and returns them as output.
8+
9+
The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when the API Gateway receives an HTTP request.
10+
11+
The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments:
12+
- the event argument is a `APIGatewayV2Request`. It is the parameter passed by the API Gateway. It contains all data passed in the HTTP request and some meta data.
13+
- the context argument is a `Lambda Context`. It is a description of the runtime context.
14+
15+
The function must return a `APIGatewayV2Response`.
16+
17+
`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library.
18+
19+
## Build & Package
20+
21+
To build the package, type the following commands.
22+
23+
```bash
24+
swift build
25+
swift package archive --disable-sandbox
26+
```
27+
28+
If there is no error, there is a ZIP file ready to deploy.
29+
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip`
30+
31+
## Deploy
32+
33+
The deployment must include the Lambda function and the API Gateway. We use the [Serverless Application Model (SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) to deploy the infrastructure.
34+
35+
**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
36+
37+
The example directory contains a file named `template.yaml` that describes the deployment.
38+
39+
To actually deploy your Lambda function and create the infrastructure, type the following `sam` command.
40+
41+
```bash
42+
sam deploy \
43+
----resolve-s3 \
44+
--template-file template.yaml \
45+
--stack-name MyLambda \
46+
--capabilities CAPABILITY_IAM
47+
```
48+
49+
At the end of the deployment, the script lists the API Gateway endpoint.
50+
The output is similar to this one.
51+
52+
```
53+
-----------------------------------------------------------------------------------------------------------------------------
54+
Outputs
55+
-----------------------------------------------------------------------------------------------------------------------------
56+
Key APIGAtewayEndpoint
57+
Description API Gateway endpoint UR"
58+
Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
59+
-----------------------------------------------------------------------------------------------------------------------------
60+
```
61+
62+
## Invoke your Lambda function
63+
64+
To invoke the Lambda function, use this `curl` command line.
65+
66+
```bash
67+
curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
68+
```
69+
70+
Be sure to replace the URL with the API Gateway endpoint returned in the previous step.
71+
72+
This should print a JSON similar to
73+
74+
```bash
75+
{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"}
76+
```
77+
78+
If you have `jq` installed, you can use it to pretty print the output.
79+
80+
```bash
81+
curl -s https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | jq
82+
{
83+
"version": "2.0",
84+
"rawPath": "/",
85+
"requestContext": {
86+
"domainPrefix": "a5q74es3k2",
87+
"stage": "$default",
88+
"timeEpoch": 1727726558220,
89+
"http": {
90+
"protocol": "HTTP/1.1",
91+
"method": "GET",
92+
"userAgent": "curl/8.7.1",
93+
"path": "/",
94+
"sourceIp": "81.0.0.43"
95+
},
96+
"apiId": "a5q74es3k2",
97+
"accountId": "012345678901",
98+
"requestId": "e72KxgsRoAMEMSA=",
99+
"domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
100+
"time": "30/Sep/2024:20:02:38 +0000"
101+
},
102+
"rawQueryString": "",
103+
"routeKey": "$default",
104+
"headers": {
105+
"x-forwarded-for": "81.0.0.43",
106+
"user-agent": "curl/8.7.1",
107+
"host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
108+
"accept": "*/*",
109+
"x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb",
110+
"content-length": "0",
111+
"x-forwarded-proto": "https",
112+
"x-forwarded-port": "443"
113+
},
114+
"isBase64Encoded": false
115+
}
116+
```
117+
118+
## Undeploy
119+
120+
When done testing, you can delete the infrastructure with this command.
121+
122+
```bash
123+
sam delete
124+
```

Examples/HelloWorld/README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Hello World
22

3-
This is a simple example of an AWS Lambda function that takes a String as input parameter and returns a String as response.
3+
This is a simple example of an AWS Lambda function that takes a `String` as input parameter and returns a `String` as response.
44

55
## Code
66

7-
The code creates a `LambdaRuntime` struct. In it's simplest form, it takes a function as argument. The function is the Lambda handler that will be invoked when an event triggers the Lambda function.
7+
The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when an event triggers the Lambda function.
88

9-
The handler is `(event: String, context: LambdaContext) -> String`. The function takes two arguments:
9+
The handler is `(event: String, context: LambdaContext)`. The function takes two arguments:
1010
- the event argument is a `String`. It is the parameter passed when invoking the function.
1111
- the context argument is a `Lambda Context`. It is a description of the runtime context.
1212

13-
The function must return a String.
13+
The function return value will be encoded as your Lambda function response.
1414

1515
## Build & Package
1616

@@ -53,9 +53,9 @@ aws lambda invoke \
5353
out.txt && cat out.txt && rm out.txt
5454
```
5555

56-
Note that the payload is expected to be a valid JSON strings, hence the surroundings quotes (`"`).
56+
Note that the payload is expected to be a valid JSON string, hence the surroundings quotes (`"`).
5757

58-
This should print
58+
This should output the following result.
5959

6060
```
6161
{
@@ -64,3 +64,11 @@ This should print
6464
}
6565
"Hello Seb"
6666
```
67+
68+
## Undeploy
69+
70+
When done testing, you can delete the Lambda function with this command.
71+
72+
```bash
73+
aws lambda delete-function --function-name MyLambda
74+
```

0 commit comments

Comments
 (0)