Welcome to Rust Happy "AWS Dev Hour: Building Modern Applications" with the backend Lambda function implemented in Rust
The following project follows AWS Dev Hour: Building Modern Applications We build out our cloud application infrastructure using CDK. Our backend code will be implemented in Rust. We include the rust code in this project as a mono-repo to make building and deploying ergonomic. For a real world project we would likely separate these concerns.
Follow directions to install the following for your host platform.
If we would like to package our lambdas on the Windows side, use this utility to create the zip file correctly.
go.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip
TODO: install OpenSSL and build the Rust OpenSSL crate.
TODO: Explore using Visual Studio Code Standard Rust Container
Build an image for building our lambdas based on the official Rust template for Alpine Linux. To this we add build-base, openssl-dev, git and zip.
docker build -t happyspace/rust-happy .
docker run --rm -it -v "$(pwd):/usr/src/app" happyspace/rust-happy
cargo build --bin rekognition --release --target x86_64-unknown-linux-musl
For custom AWS lambda runtimes, we follow the convention described in the docs:
- Include a runtime 'in the form of an executable file named bootstrap'
- A lambda function, in our case compiled into our runtime bootstrap
We will package our lambda functions in .zip archives for deployment through AWS CDK.
cp ./target/x86_64-unknown-linux-musl/release/rekognition ./bootstrap && zip rekognition.zip bootstrap && rm bootstrap && mv -f rekognition.zip ./lambda/
Our bootstrap file must be executable, which will not be preserved creating the '.zip' file in Windows context. Use the 'build-lambda-zip utility to create a zip file that does preserved the executable status of our bootstrap file.
cp ./target/x86_64-unknown-linux-musl/release/rekognition ./bootstrap ~\Go\Bin\build-lambda-zip.exe -output rekognition.zip bootstrap mv -force rekognition.zip ./lambda/ && rm bootstrap
Once we have created our build context in Docker, built and packaged our lambda functions: we are ready to deploy the code through AWS CDK.
cdk synth
cdk deploy
'runtime: Runtime.PROVIDED_AL2,' tells cloud formation that we are providing the runtime and that this runtime should run on Amazon Linux 2 (the 'AL2' suffix).
const rekognitionFunction = new lambda.Function(this, "rekognitionFunction", {
code: lambda.Code.fromAsset("lambda//rekognition.zip"),
runtime: Runtime.PROVIDED_AL2,
handler: "doesnt.matter",
timeout: Duration.seconds(30),
memorySize: 1024,
environment: {
"TABLE": this.tableImage.tableName,
"BUCKET": this.bucketImage.bucketName,
RUST_BACKTRACE: "1",
},
});
The cdk.json
file tells the CDK Toolkit how to execute your app.
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template
For our Rusoto clients it is important to enable the use of 'rustls' which removes a dependence on openssl for crypto functions.
rusoto_core = {version = "0.46.0", default_features = false, features=["rustls"]}
rusoto_s3 = {version = "0.46.0", default_features = false, features=["rustls"]}
rusoto_sqs = {version = "0.46.0", default_features = false, features=["rustls"]}
rusoto_dynamodb = {version = "0.46.0", default_features = false, features=["rustls"]}