Skip to content

Commit 17f6b46

Browse files
committed
added all changes suggested by Tim
1 parent a301109 commit 17f6b46

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

Sources/AWSLambdaRuntimeCore/Documentation.docc/Deployment.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ Here is the content of this guide:
5454
}
5555
```
5656

57+
3. A Swift Lambda function to deploy.
58+
59+
You need a Swift Lambda function to deploy. If you don't have one yet, you can use one of the examples in the [Examples](https://github.com/swift-server/swift-aws-lambda-runtime/tree/main/Examples) directory.
60+
61+
Compile and package the function using the following command
62+
63+
```sh
64+
swift package archive --allow-network-connections docker
65+
```
66+
67+
This command creates a ZIP file with the compiled Swift code. The ZIP file is located in the `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` folder.
68+
69+
The name of the ZIP file depends on the target name you entered in the `Package.swift` file.
70+
5771
## Choosing the AWS Region where to deploy
5872

5973
[AWS Global infrastructure](https://aws.amazon.com/about-aws/global-infrastructure/) spans over 34 geographic Regions (and continuously expanding). When you create a resource on AWS, such as a Lambda function, you have to select a geographic region where the resource will be created. The two main factors to consider to select a Region are the physical proximity with your users and geographical compliance.
@@ -70,6 +84,8 @@ When you create a Lambda function, you must specify an execution role. This role
7084

7185
## Deploy your Lambda function with the AWS Console
7286

87+
In this section, we deploy the HelloWorld example function using the AWS Console. The HelloWorld function is a simple function that takes a `String` as input and returns a `String`.
88+
7389
Authenticate on the AWS console using your IAM username and password. On the top right side, select the AWS Region where you want to deploy, then navigate to the Lambda section.
7490

7591
![Console - Select AWS Region](console-10-regions)
@@ -91,7 +107,7 @@ On the right side, select **Upload from** and select **.zip file**.
91107

92108
![Console - select zip file](console-40-select-zip-file)
93109

94-
Select the zip file created with the `swift package archive --allow-network-connections docker` command. This file is located in your project folder at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip`. The name of the ZIP file depends on the target name you entered in the `Package.swift` file.
110+
Select the zip file created with the `swift package archive --allow-network-connections docker` command as described in the [Prerequisites](#prerequisites) section.
95111

96112
Select **Save**
97113

@@ -101,21 +117,23 @@ You're now ready to test your function.
101117

102118
### Invoke the function
103119

104-
Select the **Test** tab in the console and prepare a payload to send to your Lambda function. In this example, you've deployed the [HelloWorld](Exmaples.HelloWorld/README.md) example function. The function expects a `String` as input parameter and returns a `String`.
120+
Select the **Test** tab in the console and prepare a payload to send to your Lambda function. In this example, you've deployed the [HelloWorld](Exmaples.HelloWorld/README.md) example function. As explained, the function takes a `String` as input and returns a `String`. we will therefore create a test event with a JSON payload that contains a `String`.
105121

106122
Select **Create new event**. Enter an **Event name**. Enter `"Swift on Lambda"` as **Event JSON**. Note that the payload must be a valid JSON document, hence we use surrounding double quotes (`"`).
107123

108124
Select **Test** on the upper right side of the screen.
109125

110126
![Console - prepare test event](console-60-prepare-test-event)
111127

112-
The response of the invocation and additional meta data appears in the green section of the page.
128+
The response of the invocation and additional meta data appear in the green section of the page.
113129

114-
I can see the response from the Swift code: `Hello Swift on Lambda`.
130+
You can see the response from the Swift code: `Hello Swift on Lambda`.
115131

116132
The function consumed 109.60ms of execution time, out of this 83.72ms where spent to initialize this new runtime. This initialization time is known as Lambda cold start time.
117133

118-
> Lambda cold start time refers to the initial delay that occurs when a Lambda function is invoked for the first time or after being idle for a while. Cold starts happen because AWS needs to provision and initialize a new container, load your code, and start your runtime environment (in this case, the Swift runtime). This delay is particularly noticeable for the first invocation, but subsequent invocations (known as "warm starts") are typically much faster because the container and runtime are already initialized and ready to process requests. Cold starts are an important consideration when architecting serverless applications, especially for latency-sensitive workloads.
134+
> Lambda cold start time refers to the initial delay that occurs when a Lambda function is invoked for the first time or after being idle for a while. Cold starts happen because AWS needs to provision and initialize a new container, load your code, and start your runtime environment (in this case, the Swift runtime). This delay is particularly noticeable for the first invocation, but subsequent invocations (known as "warm starts") are typically much faster because the container and runtime are already initialized and ready to process requests. Cold starts are an important consideration when architecting serverless applications, especially for latency-sensitive workloads. Usually, compiled languages, such as Swift, Go, and Rust, have shorter cold start times compared to interpreted languages, such as Python, Java, Ruby, and Node.js.
135+
136+
```text
119137
120138
![Console - view invocation result](console-70-view-invocation-response)
121139
@@ -147,13 +165,14 @@ Select the `HelloWorld-role-xxxx` role and select **Delete**. Confirm the deleti
147165

148166
You can deploy your Lambda function using the AWS Command Line Interface (CLI). The CLI is a unified tool to manage your AWS services from the command line and automate your operations through scripts. The CLI is available for Windows, macOS, and Linux. Follow the [installation](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and [configuration](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html) instructions in the AWS CLI User Guide.
149167

168+
In this example, we're building the HelloWorld example from the [Examples](https://github.com/swift-server/swift-aws-lambda-runtime/tree/main/Examples) directory.
169+
150170
### Create the function
151171

152172
To create a function, you must first create the function execution role and define the permission. Then, you create the function with the `create-function` command.
153173

154-
The command assumes you've already created the ZIP file with the `swift package archive --allow-network-connections docker` command. The name and the path of the ZIP file depends on the executable target name you entered in the `Package.swift` file.
174+
The command assumes you've already created the ZIP file with the `swift package archive --allow-network-connections docker` command, as described in the [Prerequisites](#prerequisites) section.
155175

156-
157176
```sh
158177
# enter your AWS Account ID
159178
export AWS_ACCOUNT_ID=123456789012
@@ -215,7 +234,7 @@ aws lambda create-function \
215234

216235
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`.
217236

218-
To update the function, use the `update-function-code` command.
237+
To update the function, use the `update-function-code` command after you've recompiled and archived your code again with the `swift package archive` command.
219238

220239
```sh
221240
aws lambda update-function-code \
@@ -266,6 +285,8 @@ Use SAM when you want to deploy more than a Lambda function. SAM helps you to cr
266285

267286
### Create the function
268287

288+
We assume your Swift function is compiled and packaged, as described in the [Prerequisites](#prerequisites) section.
289+
269290
When using SAM, you describe the infrastructure you want to deploy in a YAML file. The file contains the definition of the Lambda function, the IAM role, and the permissions needed by the function. The SAM CLI uses this file to package and deploy your function.
270291

271292
You can create a SAM template to define a REST API implemented by AWS API Gateway and a Lambda function with the following command
@@ -302,10 +323,9 @@ Outputs:
302323
EOF
303324
```
304325

305-
In this example, the Lambda function must accept an APIGateway v2 JSON payload as input parameter and return a valid APIGAteway v2 JSON response. See the example code in the [APIGateway example README file](https://github.com/swift-server/swift-aws-lambda-runtime/blob/main/Examples/APIGateway/README.md).
306-
307-
To deploy the function with SAM, use the `sam deploy` command. The very first time you deploy a function, you must use the `--guided` flag to configure the deployment. The command will ask you a series of questions to configure the deployment.
326+
In this example, the Lambda function must accept an APIGateway v2 JSON payload as input parameter and return a valid APIGAteway v2 JSON response. See the example code in the [APIGateway example README file](https://github.com/swift-server/swift-aws-lambda-runtime/blob/main/Examples/APIGateway/README.md).
308327

328+
To deploy the function with SAM, use the `sam deploy` command. The very first time you deploy a function, you should use the `--guided` flag to configure the deployment. The command will ask you a series of questions to configure the deployment.
309329

310330
Here is the command to deploy the function with SAM:
311331

@@ -342,7 +362,7 @@ CloudFormation outputs from deployed stack
342362
Outputs
343363
--------------------------------------------------------------------------------
344364
Key APIGatewayEndpoint
345-
Description API Gateway endpoint UR"
365+
Description API Gateway endpoint URI"
346366
Value https://59i4uwbuj2.execute-api.us-east-1.amazonaws.com
347367
--------------------------------------------------------------------------------
348368
@@ -428,6 +448,8 @@ To use the CDK, you need to [install the CDK CLI](https://docs.aws.amazon.com/cd
428448
429449
Use the CDK when you want to define your infrastructure in code and manage the deployment of your Lambda function and other AWS services.
430450
451+
This example deploys the [APIGateway]((https://github.com/swift-server/swift-aws-lambda-runtime/blob/main/Examples/APIGateway/) example code. It comprises a Lambda function that implements a REST API and an API Gateway to expose the function over HTTPS.
452+
431453
### Create a CDK project
432454
433455
To create a new CDK project, use the `cdk init` command. The command creates a new directory with the project structure and the necessary files to define your infrastructure.
@@ -471,7 +493,7 @@ export class LambdaApiStack extends cdk.Stack {
471493
}
472494
}
473495
```
474-
The code assumes you already built the Swift Lambda function with the `swift package archive --allow-network-connections docker` command. The ZIP file is located in the `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` folder.
496+
The code assumes you already built and packaged the APIGateway Lambda function with the `swift package archive --allow-network-connections docker` command, as described in the [Prerequisites](#prerequisites) section.
475497
476498
You can write code to add an API Gateway to invoke your Lambda function. The following code creates an HTTP API Gateway that triggers the Lambda function.
477499

0 commit comments

Comments
 (0)