Open8

EC2上に AWS SAM + Rust の開発環境を構築した際の雑多なメモ

Kenichi HayashiKenichi Hayashi

Installation (Amazon Linux 2)

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ rustup --version
rustup 1.25.1 (bb60b1e89 2022-07-12)
$ cargo --version
cargo 1.64.0 (387270bc7 2022-09-16)
Kenichi HayashiKenichi Hayashi

AWS SAM

Homebrew インストール (cargo-lambda用)

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/ec2-user/.bash_profile
$ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
$ sudo yum groupinstall 'Development Tools'

cargo-lambda インストール

$ brew tap cargo-lambda/cargo-lambda
$ brew install cargo-lambda

Lambda パッケージ作成

$ cargo lambda new aws-lambda-rust-sample

Build

$ cargo lambda build --release --arm64  

Deploy (初回のみ)

sam deploy用に template.yaml を用意

$ vi template.yaml

sam deploy用に 設定ファイルを用意

$ sam deploy --profile sandbox --guided
Stack Name [sam-app]: aws-lambda-rust-sample
	AWS Region [ap-northeast-1]:
	#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
	Confirm changes before deploy [y/N]:
	#SAM needs permission to be able to create roles to connect to the resources in your template
	Allow SAM CLI IAM role creation [Y/n]:
	#Preserves the state of previously provisioned resources when an operation fails
	Disable rollback [y/N]:
	Save arguments to configuration file [Y/n]:
	SAM configuration file [samconfig.toml]:
	SAM configuration environment [default]:

Deploy (2回目以降)

$ sam deploy --profile sandbox

実行 (FunctionNameはDeploy結果のものを使う)

$ aws lambda invoke --profile sandbox --cli-binary-format raw-in-base64-out --function-name aws-lambda-rust-sample-AwsLambdaRustSampleFunction-XXXXXXXXXXXX --payload '{"key": "value"}' output.json
---
{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}
---

結果確認

$ cat output.json
---
{"message":"Hello, TEST!"}
---

(削除する場合)

$ sam delete --profile sandbox
Kenichi HayashiKenichi Hayashi

[Usage]

$ cargo lambda new
$ cargo lambda build --release --arm64
$ cargo lambda watch
$ cargo lambda invoke --data-ascii '{"command": "hi"}'
http://localhost:9000/lambda-url/new-lambda-project
$ cargo lambda deploy  # sam deploy を使う
Kenichi HayashiKenichi Hayashi

SAM複数ランタイム

$ sam init
---
1 - AWS Quick Start Templates
1 - Hello World Example
13 - python3.9
2 - Image
Would you like to enable X-Ray tracing on the function(s) in your application?  [y/N]: N
Project name [sam-app]: aws-lambda-mix-sample
---

$ sam pipeline init --bootstrap
Kenichi HayashiKenichi Hayashi

Rust Test

help

$ cargo test [OPTIONS] [TESTNAME] [-- <ARGS>...]

テストのヘルプ

$ cargo test --help

テストのオプションのヘルプ

$ cargo test -- --help

cargo test オプション

$ cargo test --XXX

コンパイルされたテスト側オプション

$ cargo test -- --XXX

テスト内の println 出力

$ cargo test -- --nocapture

パッケージ内すべてをテスト

$ cargo test --all
$ cargo test --workspace # (--all の参照元)

前方一致

  • func_name_of_testが頭につくものをすべてテスト (func_name_of_test, func_name_of_test1, func_name_of_test2 ..)
$ cargo test --all func_name_of_test
$ cargo test --all -- --exact foo::tests::func_name_of_test
Kenichi HayashiKenichi Hayashi

参考情報

Tutorial

Playground

言語仕様系

Rust + AWS SAM

Rust + AWS SAM + Amazon API Gateway

AWS SAM Multiple Runtimes

Environment Values

Test