Skip to content

Commit e83b7cc

Browse files
committed
Added Build and Deploy section for vanilla bash approach
* added an overview how to build and deploy without using `cargo lambda` * fixed a typo
1 parent f8cc32d commit e83b7cc

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

README.md

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,65 @@ async fn func(event: LambdaEvent<Value>) -> Result<Value, Error> {
7373

7474
## Building and deploying your Lambda functions
7575

76-
If you already have Cargo Lambda installed in your machine, run the next command to build your function:
76+
### Overview
77+
78+
AWS Lambda packages are _.zip_ archives containing an executable called `bootstrap` built for a Linux sandbox.
79+
The binary has to be compiled for the target architecture and the runtime of the Lambda container.
80+
81+
- Runtimes: _Amazon Linux 2_ and _Amazon Linux 2023_
82+
- Architectures: _x86_64_ and _arm64_
83+
84+
Recommended `cargo build` targets:
85+
- Amazon Linux 2 / x86_64: `x86_64-unknown-linux-musl`
86+
- Amazon Linux 2 / arm64: `aarch64-unknown-linux-musl`
87+
- Amazon Linux 2023 / x86_64: `x86_64-unknown-linux-gnu`, `x86_64-unknown-linux-musl`
88+
- Amazon Linux 2023 / arm64: `aarch64-unknown-linux-gnu`, `aarch64-unknown-linux-musl`
89+
90+
The following script builds and deploys a Rust executable to an existing Lambda function.
91+
Replace the variable parts of the script with your values before running it from the project root.
92+
93+
```bash
94+
target=x86_64-unknown-linux-gnu # the architecture of the lambda function on AWS
95+
region=us-east-1 # the region of the lambda function
96+
lambda=my-lambda # the name of the lambda function on AWS
97+
crate=basic-lambda # the name of the crate to build and deploy
98+
99+
cargo build --release --target $target
100+
cp ./target/$target/release/$crate ./bootstrap && zip lambda.zip bootstrap && rm bootstrap
101+
aws lambda update-function-code --region $region --function-name $lambda --zip-file fileb://lambda.zip
102+
```
103+
104+
AWS Lambda fails with `Runtime.InvalidEntrypoint` exception if the target does not match the selected platform, e.g. if deploying _x86_64_ to _arm64_ architecture.
105+
106+
See [Building a Custom Runtime Guide](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) for more detailed explanations of the build and initialization process.
107+
108+
#### Cross-compilation
109+
110+
Cross-compilation for the target architecture requires the target toolchain to be installed. You may need additional configuration depending on your setup.
111+
112+
For example, to build for _arm64_ on _Ubuntu x86_64_ you need to install the target via _rustup_:
113+
114+
```bash
115+
rustup target add aarch64-unknown-linux-gnu
116+
rustup target add aarch64-unknown-linux-musl
117+
```
118+
119+
and add linkers to _/.cargo/config.toml_ file:
120+
121+
```toml
122+
[target.aarch64-unknown-linux-gnu]
123+
linker = "aarch64-linux-gnu-gcc"
124+
125+
[target.aarch64-unknown-linux-musl]
126+
linker = "aarch64-linux-gnu-gcc"
127+
```
128+
129+
See [Cross-compilation](https://rust-lang.github.io/rustup/cross-compilation.html) and [Cargo target](https://doc.rust-lang.org/cargo/reference/config.html#target) documentation for more info.
130+
131+
132+
### Using `cargo lambda` command
133+
134+
If you already have [Cargo Lambda](https://www.cargo-lambda.info) installed on your machine, run the next command to build your function:
77135

78136
```bash
79137
cargo lambda build --release
@@ -97,8 +155,7 @@ cargo lambda build --release --arm64
97155

98156
### 2. Deploying the binary to AWS Lambda
99157

100-
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.
101-
158+
Rename the generated executable to `bootstrap` and add it to a zip archive.
102159
You can find the `bootstrap` binary for your function under the `target/lambda` directory.
103160

104161
#### 2.1. Deploying with Cargo Lambda

0 commit comments

Comments
 (0)