You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/AWSLambdaRuntimeCore/Documentation.docc/Deployment.md
+35-13Lines changed: 35 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,20 @@ Here is the content of this guide:
54
54
}
55
55
```
56
56
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
+
57
71
## Choosing the AWS Region where to deploy
58
72
59
73
[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
70
84
71
85
## Deploy your Lambda function with the AWS Console
72
86
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
+
73
89
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.
@@ -91,7 +107,7 @@ On the right side, select **Upload from** and select **.zip file**.
91
107
92
108

93
109
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.
95
111
96
112
Select **Save**
97
113
@@ -101,21 +117,23 @@ You're now ready to test your function.
101
117
102
118
### Invoke the function
103
119
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`.
105
121
106
122
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 (`"`).
107
123
108
124
Select **Test** on the upper right side of the screen.
109
125
110
126

111
127
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.
113
129
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`.
115
131
116
132
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.
117
133
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.
@@ -147,13 +165,14 @@ Select the `HelloWorld-role-xxxx` role and select **Delete**. Confirm the deleti
147
165
148
166
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.
149
167
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
+
150
170
### Create the function
151
171
152
172
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.
153
173
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.
155
175
156
-
157
176
```sh
158
177
# enter your AWS Account ID
159
178
export AWS_ACCOUNT_ID=123456789012
@@ -215,7 +234,7 @@ aws lambda create-function \
215
234
216
235
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`.
217
236
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.
219
238
220
239
```sh
221
240
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
266
285
267
286
### Create the function
268
287
288
+
We assume your Swift function is compiled and packaged, as described in the [Prerequisites](#prerequisites) section.
289
+
269
290
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.
270
291
271
292
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:
302
323
EOF
303
324
```
304
325
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).
308
327
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.
309
329
310
330
Here is the command to deploy the function with SAM:
311
331
@@ -342,7 +362,7 @@ CloudFormation outputs from deployed stack
@@ -428,6 +448,8 @@ To use the CDK, you need to [install the CDK CLI](https://docs.aws.amazon.com/cd
428
448
429
449
Use the CDK when you want to define your infrastructure in code and manage the deployment of your Lambda functionand other AWS services.
430
450
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
+
431
453
### Create a CDK project
432
454
433
455
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 {
471
493
}
472
494
}
473
495
```
474
-
The code assumes you already built the Swift Lambda functionwith 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.
475
497
476
498
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.
0 commit comments