3
1

More than 3 years have passed since last update.

Rust Runtime for AWS Lambdaの実行ファイルをWindows環境ビルドしたんだけど・・

Last updated at Posted at 2019-12-07

Rust Lambda のビルド

Rustを勉強するついでにあえてWLSを利用せずにWindows環境でRust実行環境の構築と
Lambda with Rustを構築してみたんだけども・・

やりたいこと

AWS Lambdaで実行可能なbootstrapファイルをビルドできる環境を構築したい。

できれば、WSL環境が許されない現場でもどうにかしてビルドできる環境できないか
確認したい。

今回の動作環境

  • Windows 10
  • Rustインストール(rustup-init.exe)
  • VSCode, rls extensionsインストール済み
  • Windows 10 SDK, dev toolsをインストール済み
  • Docker on Windowsインストール済み
参考

AWS Blog:Rust Runtime for AWS Lambda

Rustプロジェクトの作成


PS C:\Users\h\OneDrive\ドキュメント\rust> cargo new rustlambdasample --bin
     Created binary (application) `rustlambdasample` package
PS C:\Users\h\OneDrive\ドキュメント\rust>  

x86_64-unknown-linux-muslターゲットを追加

rustup target add x86_64-unknown-linux-musl

必要なライブラリのインストール

VS 2019 Visual C++ optionをインストールしろとな。


   Compiling futures v0.1.29
error: linker `link.exe` not found
  |
  = note: 指定されたファイルが見つかりません。 (os error 2)

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013, VS 2015, VS 2017 or VS 2019 was installed with the Visual C++ option

error: aborting due to previous error

error: could not compile `libc`.
warning: build failed, waiting for other jobs to finish...
error: build failed

windows上でのビルド環境がそろっていなかったのでbuildtoolとwindows 10 SDKをインストール。
ビルド環境参考
Windows 10SDK
visualcppbuildtools_full.exeダウンロードぺージ

x86_64-unknown-linux-muslの取り込み成功

crossのインストールとビルド

crossを利用すると簡単にクロスコンパイルできるらしいのでインストールしコンパイル実行する。


cargo install cross
cross build --release --target x86_64-unknown-linux-musl

PS C:\Users\h\OneDrive\ドキュメント\rust\rustlambdasample> cross build --release --target x86_64-unknown-linux-musl       
Unable to find image 'rustembedded/cross:x86_64-unknown-linux-musl-0.1.16' locally
x86_64-unknown-linux-musl-0.1.16: Pulling from rustembedded/cross
35c102085707: Pull complete
251f5509d51d: Pull complete
8e829fe70a46: Pull complete
6001e1789921: Pull complete
1361eb413a0a: Pull complete
e23741e5f3bc: Pull complete
33fbfcc5c23e: Pull complete
924e4a072267: Pull complete
661205389135: Pull complete
e782bbff3ee6: Pull complete
cf883c10f870: Pull complete
Digest: sha256:e6a64723b28e7874b643a09a173a78f5e5d1841271b980632a9c479e200bba87
Status: Downloaded newer image for rustembedded/cross:x86_64-unknown-linux-musl-0.1.16
   Compiling libc v0.2.66
   Compiling cfg-if v0.1.10
   Compiling semver-parser v0.7.0
   Compiling autocfg v0.1.7
   Compiling futures v0.1.29
  
~~~ 中略 ~~~

   Compiling tokio-uds v0.2.5
   Compiling tokio-udp v0.1.5
   Compiling tokio-tcp v0.1.3
   Compiling tokio-threadpool v0.1.17
   Compiling tokio-fs v0.1.6
   Compiling tokio v0.1.22
   Compiling lambda_runtime_client v0.1.0
   Compiling lambda_runtime v0.1.0
   Compiling rustlambdasample v0.1.0 (/project)
    Finished release [optimized] target(s) in 4m 30s
PS C:\Users\h\OneDrive\ドキュメント\rust\rustlambdasample> 

コンパイル成功!!

ユニットテストの実行

ユニットテストも実行してみる。
テスト書いてないけど。

PS C:\Users\h\OneDrive\ドキュメント\rust\rustlambdasample> cross test --release --target x86_64-unknown-linux-musl        
   Compiling rustlambdasample v0.1.0 (/project)
    Finished release [optimized] target(s) in 8.31s
     Running /target/x86_64-unknown-linux-musl/release/deps/bootstrap-6074c8f123664827

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

実行は問題なくできている模様

デプロイしてみる

zip化してLambda環境にデプロイしてみる。
今回はコンソール画面からzipファイルでデプロイしていますよ。


Compress-Archive -Path ./target/x86_64-unknown-linux-musl/release/bootstrap -DestinationPath rust.zip

Lambda関数設定値

  • カスタム関数
  • provided(ユーザー独自)

テストJSONリクエスト

{
  "firstName": "Rustacean"
}

そして、実行!!

fail.JPG

失敗・・・・。
実行権限がWindows上でのアーカイブだと設定できおらず、AWS Lambda内で実行できず失敗する模様。
方法わからなかったので分かる方いらっしゃったら教えてください・・・。

仕方ないので・・・

ここまできたらどうにかビルドしたbootstrapの動作確認がしたいので・・。

(結局)WSLを介してzip化を行い再度デプロイしてみる。


# zip -j rust.zip ./target/x86_64-unknown-linux-musl/release/bootstrap

再実行

成功!!

suc.JPG

おとなしく公式のいうとおり、WSL上でビルドします・・・。

まとめ

  • windows 10なら迷わずwslを使いましょう。
  • crossを利用すればクロスコンパイルの環境が簡単に用意できる。(ただしdocker環境が必要)
  • windows 7などでもdocker on Windowsがインストールできてればローカル環境でAWS Lambdaの実行環境でユニットテストできそう。
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1