Skip to content

Commit 7500b2b

Browse files
authored
Refresh readmes (#883)
- Remove old tooling information that we don't test or use. - Update to recommend AL2023 - Remove any mention to AL1 - Cleanup code examples - Move lambda_http specific docs to its own readme Signed-off-by: David Calavera <[email protected]>
1 parent ee78eaa commit 7500b2b

File tree

2 files changed

+28
-112
lines changed

2 files changed

+28
-112
lines changed

README.md

Lines changed: 6 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,14 @@ By default, Cargo Lambda builds your functions to run on x86_64 architectures. I
8787

8888
#### 1.2. Build your Lambda functions
8989

90-
__Amazon Linux 2__
90+
__Amazon Linux 2023__
9191

92-
We recommend you to use Amazon Linux 2 runtimes (such as `provided.al2`) as much as possible for building Lambda functions in Rust. To build your Lambda functions for Amazon Linux 2 runtimes, run:
92+
We recommend you to use the Amazon Linux 2023 (such as `provided.al2023`) because it includes a newer version of GLIC, which many Rust programs depend on. To build your Lambda functions for Amazon Linux 2023 runtimes, run:
9393

9494
```bash
9595
cargo lambda build --release --arm64
9696
```
9797

98-
__Amazon Linux 1__
99-
100-
Amazon Linux 1 uses glibc version 2.17, while Rust binaries need glibc version 2.18 or later by default. However, with Cargo Lambda, you can specify a different version of glibc.
101-
102-
If you are building for Amazon Linux 1, or you want to support both Amazon Linux 2 and 1, run:
103-
104-
```bash
105-
cargo lambda build --release --target aarch64-unknown-linux-gnu.2.17
106-
```
107-
> **Note**
108-
> Replace "aarch64" with "x86_64" if you are building for x86_64
10998
### 2. Deploying the binary to AWS Lambda
11099

111100
For [a custom runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html), AWS Lambda looks for an executable called `bootstrap` in the deployment package zip. Rename the generated executable to `bootstrap` and add it to a zip archive.
@@ -117,8 +106,7 @@ You can find the `bootstrap` binary for your function under the `target/lambda`
117106
Once you've built your code with one of the options described earlier, use the `deploy` subcommand to upload your function to AWS:
118107

119108
```bash
120-
cargo lambda deploy \
121-
--iam-role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role
109+
cargo lambda deploy
122110
```
123111

124112
> **Warning**
@@ -128,9 +116,7 @@ This command will create a Lambda function with the same name of your rust packa
128116
of the function by adding the argument at the end of the command:
129117

130118
```bash
131-
cargo lambda deploy \
132-
--iam-role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role \
133-
my-first-lambda-function
119+
cargo lambda deploy my-first-lambda-function
134120
```
135121

136122
> **Note**
@@ -162,7 +148,7 @@ You can find the resulting zip file in `target/lambda/YOUR_PACKAGE/bootstrap.zip
162148
$ aws lambda create-function --function-name rustTest \
163149
--handler bootstrap \
164150
--zip-file fileb://./target/lambda/basic/bootstrap.zip \
165-
--runtime provided.al2023 \ # Change this to provided.al2 if you would like to use Amazon Linux 2 (or to provided.al for Amazon Linux 1).
151+
--runtime provided.al2023 \ # Change this to provided.al2 if you would like to use Amazon Linux 2
166152
--role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role \
167153
--environment Variables={RUST_BACKTRACE=1} \
168154
--tracing-config Mode=Active
@@ -229,74 +215,6 @@ $ aws lambda invoke
229215
$ cat output.json # Prints: {"msg": "Command Say Hi! executed."}
230216
```
231217

232-
#### 2.4. Serverless Framework
233-
234-
Alternatively, you can build a Rust-based Lambda function declaratively using the [Serverless framework Rust plugin](https://github.com/softprops/serverless-rust).
235-
236-
A number of getting started Serverless application templates exist to get you up and running quickly:
237-
238-
- a minimal [echo function](https://github.com/softprops/serverless-aws-rust) to demonstrate what the smallest Rust function setup looks like
239-
- a minimal [http function](https://github.com/softprops/serverless-aws-rust-http) to demonstrate how to interface with API Gateway using Rust's native [http](https://crates.io/crates/http) crate (note this will be a git dependency until 0.2 is published)
240-
- a combination [multi function service](https://github.com/softprops/serverless-aws-rust-multi) to demonstrate how to set up a services with multiple independent functions
241-
242-
Assuming your host machine has a relatively recent version of node, you [won't need to install any host-wide serverless dependencies](https://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner). To get started, run the following commands to create a new lambda Rust application
243-
and install project level dependencies.
244-
245-
```bash
246-
$ npx serverless install \
247-
--url https://github.com/softprops/serverless-aws-rust \
248-
--name my-new-app \
249-
&& cd my-new-app \
250-
&& npm install --silent
251-
```
252-
253-
Deploy it using the standard serverless workflow:
254-
255-
```bash
256-
# build, package, and deploy service to aws lambda
257-
$ npx serverless deploy
258-
```
259-
260-
Invoke it using serverless framework or a configured AWS integrated trigger source:
261-
262-
```bash
263-
npx serverless invoke -f hello -d '{"foo":"bar"}'
264-
```
265-
266-
#### 2.5. Docker
267-
268-
Alternatively, you can build a Rust-based Lambda function in a [docker mirror of the AWS Lambda provided runtime with the Rust toolchain preinstalled](https://github.com/rust-serverless/lambda-rust).
269-
270-
Running the following command will start an ephemeral docker container, which will build your Rust application and produce a zip file containing its binary auto-renamed to `bootstrap` to meet the AWS Lambda's expectations for binaries under `target/lambda_runtime/release/{your-binary-name}.zip`. Typically, this is just the name of your crate if you are using the cargo default binary (i.e. `main.rs`):
271-
272-
```bash
273-
# build and package deploy-ready artifact
274-
$ docker run --rm \
275-
-v ${PWD}:/code \
276-
-v ${HOME}/.cargo/registry:/root/.cargo/registry \
277-
-v ${HOME}/.cargo/git:/root/.cargo/git \
278-
rustserverless/lambda-rust
279-
```
280-
281-
With your application built and packaged, it's ready to ship to production. You can also invoke it locally to verify is behavior using the [lambci :provided docker container](https://hub.docker.com/r/lambci/lambda/), which is also a mirror of the AWS Lambda provided runtime with build dependencies omitted:
282-
283-
```bash
284-
# start a docker container replicating the "provided" lambda runtime
285-
# awaiting an event to be provided via stdin
286-
$ unzip -o \
287-
target/lambda/release/{your-binary-name}.zip \
288-
-d /tmp/lambda && \
289-
docker run \
290-
-i -e DOCKER_LAMBDA_USE_STDIN=1 \
291-
--rm \
292-
-v /tmp/lambda:/var/task \
293-
lambci/lambda:provided
294-
295-
# provide an event payload via stdin (typically a json blob)
296-
297-
# Ctrl-D to yield control back to your function
298-
```
299-
300218
## Local development and testing
301219

302220
### Testing your code with unit and integration tests
@@ -332,7 +250,7 @@ fn test_my_lambda_handler() {
332250
}
333251
```
334252

335-
### Cargo Lambda
253+
### Local dev server with Cargo Lambda
336254

337255
[Cargo Lambda](https://www.cargo-lambda.info) provides a local server that emulates the AWS Lambda control plane. This server works on Windows, Linux, and MacOS. In the root of your Lambda project. You can run the following subcommand to compile your function(s) and start the server.
338256

@@ -432,30 +350,6 @@ fn main() -> Result<(), Box<Error>> {
432350
}
433351
```
434352

435-
## Feature flags in lambda_http
436-
437-
`lambda_http` is a wrapper for HTTP events coming from three different services, Amazon Load Balancer (ALB), Amazon Api Gateway (APIGW), and AWS Lambda Function URLs. Amazon Api Gateway can also send events from three different endpoints, REST APIs, HTTP APIs, and WebSockets. `lambda_http` transforms events from all these sources into native `http::Request` objects, so you can incorporate Rust HTTP semantics into your Lambda functions.
438-
439-
By default, `lambda_http` compiles your function to support any of those services. This increases the compile time of your function because we have to generate code for all the sources. In reality, you'll usually put a Lambda function only behind one of those sources. You can choose which source to generate code for with feature flags.
440-
441-
The available features flags for `lambda_http` are the following:
442-
443-
- `alb`: for events coming from [Amazon Elastic Load Balancer](https://aws.amazon.com/elasticloadbalancing/).
444-
- `apigw_rest`: for events coming from [Amazon API Gateway Rest APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html).
445-
- `apigw_http`: for events coming from [Amazon API Gateway HTTP APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api.html) and [AWS Lambda Function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html).
446-
- `apigw_websockets`: for events coming from [Amazon API Gateway WebSockets](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html).
447-
448-
If you only want to support one of these sources, you can disable the default features, and enable only the source that you care about in your package's `Cargo.toml` file. Substitute the dependency line for `lambda_http` for the snippet below, changing the feature that you want to enable:
449-
450-
```toml
451-
[dependencies.lambda_http]
452-
version = "0.5.3"
453-
default-features = false
454-
features = ["apigw_rest"]
455-
```
456-
457-
This will make your function compile much faster.
458-
459353
## Supported Rust Versions (MSRV)
460354

461355
The AWS Lambda Rust Runtime requires a minimum of Rust 1.70, and is not guaranteed to build on compiler versions earlier than that.

lambda-http/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,25 @@ pub async fn function_handler(dynamodb_client: &aws_sdk_dynamodb::Client, event:
235235
When you integrate HTTP Lambda functions with API Gateway stages, the path received in the request will include the stage as the first segment, for example `/production/api/v1`, where `production` is the API Gateway stage.
236236

237237
If you don't want to receive the stage as part of the path, you can set the environment variable `AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH` to `true`, either in your Lambda function configuration, or inside the `main` Rust function. Following the previous example, when this environment variable is present, the path that the function receives is `/api/v1`, eliminating the stage from the first segment.
238+
239+
## Feature flags
240+
241+
`lambda_http` is a wrapper for HTTP events coming from three different services, Amazon Load Balancer (ALB), Amazon Api Gateway (APIGW), and AWS Lambda Function URLs. Amazon Api Gateway can also send events from three different endpoints, REST APIs, HTTP APIs, and WebSockets. `lambda_http` transforms events from all these sources into native `http::Request` objects, so you can incorporate Rust HTTP semantics into your Lambda functions.
242+
243+
By default, `lambda_http` compiles your function to support any of those services. This increases the compile time of your function because we have to generate code for all the sources. In reality, you'll usually put a Lambda function only behind one of those sources. You can choose which source to generate code for with feature flags.
244+
245+
The available features flags for `lambda_http` are the following:
246+
247+
- `alb`: for events coming from [Amazon Elastic Load Balancer](https://aws.amazon.com/elasticloadbalancing/).
248+
- `apigw_rest`: for events coming from [Amazon API Gateway Rest APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html).
249+
- `apigw_http`: for events coming from [Amazon API Gateway HTTP APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api.html) and [AWS Lambda Function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html).
250+
- `apigw_websockets`: for events coming from [Amazon API Gateway WebSockets](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html).
251+
252+
If you only want to support one of these sources, you can disable the default features, and enable only the source that you care about in your package's `Cargo.toml` file. Substitute the dependency line for `lambda_http` for the snippet below, changing the feature that you want to enable:
253+
254+
```toml
255+
[dependencies.lambda_http]
256+
version = "0.5.3"
257+
default-features = false
258+
features = ["apigw_rest"]
259+
```

0 commit comments

Comments
 (0)