Skip to content

Commit fe123f8

Browse files
committed
Fix readme
1 parent 7a89c19 commit fe123f8

File tree

4 files changed

+143
-12
lines changed

4 files changed

+143
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ Package.resolved
1212
.vscode
1313
Makefile
1414
.devcontainer
15-
.amazonq
15+
.amazonq
16+
samconfig.toml

Examples/StreamingFromEvent/README.md

Lines changed: 138 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Streaming Codable Lambda function
22

3-
This example demonstrates how to use the new `StreamingLambdaHandlerWithEvent` protocol to create Lambda functions that:
3+
This example demonstrates how to use the `StreamingLambdaHandlerWithEvent` protocol to create Lambda functions that:
44

55
1. **Receive JSON input**: Automatically decode JSON events into Swift structs
66
2. **Stream responses**: Send data incrementally as it becomes available
77
3. **Execute background work**: Perform additional processing after the response is sent
88

9-
The example uses the new streaming codable interface that combines the benefits of:
9+
The example uses the streaming codable interface that combines the benefits of:
1010
- Type-safe JSON input decoding (like regular `LambdaHandler`)
1111
- Response streaming capabilities (like `StreamingLambdaHandler`)
1212
- Background work execution after response completion
@@ -68,7 +68,7 @@ The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPa
6868
You can test the function locally before deploying:
6969

7070
```bash
71-
swift run &
71+
swift run
7272

7373
# In another terminal, test with curl:
7474
curl -v \
@@ -95,19 +95,70 @@ aws lambda create-function \
9595
--role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution
9696
```
9797

98+
> [!IMPORTANT]
99+
> The timeout value must be bigger than the time it takes for your function to stream its output. Otherwise, the Lambda control plane will terminate the execution environment before your code has a chance to finish writing the stream. Here, the sample function stream responses during 10 seconds and we set the timeout for 15 seconds.
100+
98101
The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`.
99102

100103
Be sure to set `AWS_ACCOUNT_ID` with your actual AWS account ID (for example: 012345678901).
101104

102-
### Invoke your Lambda function
105+
### Step2: Give permission to invoke that function through an URL
106+
107+
Anyone with a valid signature from your AWS account will have permission to invoke the function through its URL.
108+
109+
```bash
110+
aws lambda add-permission \
111+
--function-name StreamingFromEvent \
112+
--action lambda:InvokeFunctionUrl \
113+
--principal ${AWS_ACCOUNT_ID} \
114+
--function-url-auth-type AWS_IAM \
115+
--statement-id allowURL
116+
```
117+
118+
### Step3: Create the URL
103119

104-
To invoke the Lambda function, use the AWS CLI:
120+
This creates [a URL with IAM authentication](https://docs.aws.amazon.com/lambda/latest/dg/urls-auth.html). Only calls with a valid signature will be authorized.
105121

106122
```bash
107-
aws lambda invoke \
123+
aws lambda create-function-url-config \
108124
--function-name StreamingFromEvent \
109-
--payload $(echo '{"count": 5, "message": "Streaming from AWS!", "delayMs": 500}' | base64) \
110-
response.txt && cat response.txt
125+
--auth-type AWS_IAM \
126+
--invoke-mode RESPONSE_STREAM
127+
```
128+
This calls return various information, including the URL to invoke your function.
129+
130+
```json
131+
{
132+
"FunctionUrl": "https://ul3nf4dogmgyr7ffl5r5rs22640fwocc.lambda-url.us-east-1.on.aws/",
133+
"FunctionArn": "arn:aws:lambda:us-east-1:012345678901:function:StreamingFromEvent",
134+
"AuthType": "AWS_IAM",
135+
"CreationTime": "2024-10-22T07:57:23.112599Z",
136+
"InvokeMode": "RESPONSE_STREAM"
137+
}
138+
```
139+
140+
### Invoke your Lambda function
141+
142+
To invoke the Lambda function, use `curl` with the AWS Sigv4 option to generate the signature.
143+
144+
Read the [AWS Credentials and Signature](../README.md/#AWS-Credentials-and-Signature) section for more details about the AWS Sigv4 protocol and how to obtain AWS credentials.
145+
146+
When you have the `aws` command line installed and configured, you will find the credentials in the `~/.aws/credentials` file.
147+
148+
```bash
149+
URL=https://ul3nf4dogmgyr7ffl5r5rs22640fwocc.lambda-url.us-east-1.on.aws/
150+
REGION=us-east-1
151+
ACCESS_KEY=AK...
152+
SECRET_KEY=...
153+
AWS_SESSION_TOKEN=...
154+
155+
curl --user "${ACCESS_KEY}":"${SECRET_KEY}" \
156+
--aws-sigv4 "aws:amz:${REGION}:lambda" \
157+
-H "x-amz-security-token: ${AWS_SESSION_TOKEN}" \
158+
--no-buffer \
159+
--header "Content-Type: application/json" \
160+
--data '{"count": 3, "message": "Hello World!", "delayMs": 1000}' \
161+
"$URL"
111162
```
112163

113164
This should output the following result, with configurable delays between each message:
@@ -126,3 +177,82 @@ When done testing, you can delete the Lambda function with this command.
126177
```bash
127178
aws lambda delete-function --function-name StreamingFromEvent
128179
```
180+
181+
## Deploy with AWS SAM
182+
183+
Alternatively, you can use [AWS SAM](https://aws.amazon.com/serverless/sam/) to deploy the Lambda function.
184+
185+
**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
186+
187+
### SAM Template
188+
189+
The template file is provided as part of the example in the `template.yaml` file. It defines a Lambda function based on the binary ZIP file. It creates the function url with IAM authentication and sets the function timeout to 15 seconds.
190+
191+
```yaml
192+
AWSTemplateFormatVersion: '2010-09-09'
193+
Transform: AWS::Serverless-2016-10-31
194+
Description: SAM Template for StreamingFromEvent Example
195+
196+
Resources:
197+
# Lambda function
198+
StreamingNumbers:
199+
Type: AWS::Serverless::Function
200+
Properties:
201+
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/StreamingFromEvent/StreamingFromEvent.zip
202+
Timeout: 15
203+
Handler: swift.bootstrap # ignored by the Swift runtime
204+
Runtime: provided.al2
205+
MemorySize: 128
206+
Architectures:
207+
- arm64
208+
FunctionUrlConfig:
209+
AuthType: AWS_IAM
210+
InvokeMode: RESPONSE_STREAM
211+
212+
Outputs:
213+
# print Lambda function URL
214+
LambdaURL:
215+
Description: Lambda URL
216+
Value: !GetAtt StreamingNumbersUrl.FunctionUrl
217+
```
218+
219+
### Deploy with SAM
220+
221+
```bash
222+
sam deploy \
223+
--resolve-s3 \
224+
--template-file template.yaml \
225+
--stack-name StreamingFromEvent \
226+
--capabilities CAPABILITY_IAM
227+
```
228+
229+
The URL of the function is provided as part of the output.
230+
231+
```
232+
CloudFormation outputs from deployed stack
233+
-----------------------------------------------------------------------------------------------------------------------------
234+
Outputs
235+
-----------------------------------------------------------------------------------------------------------------------------
236+
Key LambdaURL
237+
Description Lambda URL
238+
Value https://gaudpin2zjqizfujfnqxstnv6u0czrfu.lambda-url.us-east-1.on.aws/
239+
-----------------------------------------------------------------------------------------------------------------------------
240+
```
241+
242+
Once the function is deployed, you can invoke it with `curl`, similarly to what you did when deploying with the AWS CLI.
243+
244+
```bash
245+
curl "$URL" \
246+
--user "$ACCESS_KEY":"$SECRET_KEY" \
247+
--aws-sigv4 "aws:amz:${REGION}:lambda" \
248+
-H "x-amz-security-token: $AWS_SESSION_TOKEN" \
249+
--no-buffer
250+
```
251+
252+
### Undeploy with SAM
253+
254+
When done testing, you can delete the infrastructure with this command.
255+
256+
```bash
257+
sam delete
258+
```

Examples/StreamingFromEvent/template.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
3-
Description: SAM Template for Streaming Example
3+
Description: SAM Template for StreamingfromEvent Example
44

55
Resources:
66
# Lambda function
77
StreamingNumbers:
88
Type: AWS::Serverless::Function
99
Properties:
10-
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/StreamingNumbers/StreamingNumbers.zip
10+
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/StreamingFromEvent/StreamingFromEvent.zip
1111
Timeout: 15
1212
Handler: swift.bootstrap # ignored by the Swift runtime
1313
Runtime: provided.al2

Tests/AWSLambdaRuntimeTests/LambdaStreamingCodableTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftAWSLambdaRuntime open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
5+
// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information

0 commit comments

Comments
 (0)