LoginSignup
4
5

More than 5 years have passed since last update.

SwiftとHexavilleで作ったWEB APIをAWS Lambdaにデプロイした

Last updated at Posted at 2017-12-17

この記事はSwiftとHexavilleで駅探索APIを作ってみたの続編です。

環境

  • Mac OS 10.12.6
  • Swift 4.0
  • Xcode 9.2
  • Hexaville 0.5.6

モチベーション

  • ローカルで動くAPIができたのでデプロイしたい
  • HexavilleはAWS Lambdaへのデプロイをサポートしている

Hexaville は簡単に言えば、Swiftの為のモダンなWebアプリケーションフレームワーク及びエンジンです。Swiftで作られたwebアプリをAWS Lambda と ApiGatewayで実行する事をコンセプトにしているそうです。手軽にAWS Lambdaにデプロイできる仕組みを持っています。

準備

  • AWSのアカウント
  • access_key_id, secret_access_key
  • Lambdaを実行するロール

ここからデプロイ手順になります。

Hexavillefile.ymlの編集

プロジェクト作成で自動生成されているHexavillefile.ymlを編集。

name: NearStations
service: aws
aws:
  credential:
     access_key_id: xxxxxxxxxxxxxxxxxx
     secret_access_key: xxxxxxxxxxxxxxxxxx
  region: ap-northeast-1
  lambda:
     bucket: hexaville-nearstations-pglda-bucket
     role: xxxxxxxxxxxxxxxxxxxxxxxxxx
#     vpc:
#       subnetIds:
#         - xxxxxx
#         - xxxxxx
#       securityGroupIds:
#         - xxxxxx
#         - xxxxxx
build:
  nocache: false
swift:
  version: 4.0
  build:
     configuration: release # default is debug

xxxxxxxxxxxxxxxxxx はAWSで準備した値を設定してください。

デプロイコマンド実行

hexaville deploy NearStations

hexaville deploy < projectName > になります。

叩いたら、しばらく待ちます。
コンソールにかなりログが表示されていきますが、じっと待ちましょう。
初回は結構時間がかかると思います。
デプロイが成功すると、このように表示されます。
スクリーンショット 2017-12-16 21.04.46.png

AWSコンソールで確認

スクリーンショット 2017-12-16 21.14.01.png

関数が作られていますね。

動作確認

ニジボックスの入っているビルで試してみましょう。緯度:35.660944, 経度:139.775199

スクリーンショット 2017-12-16 20.57.09.png

無事、付近の駅が取得できました! 簡単ですね。
https対応の駅探索APIの完成です!

ただ同様に実行しても、たまに以下のようなサーバーエラーが返ることがありました。これは原因不明です。
スクリーンショット 2017-12-16 23.25.55.png

トラブルシューティング

ここまでだと簡単ですが、私が最初試した時は、上記のデプロイコマンド実行後に、コンソールに身に覚えのないエラーが表示され、大分苦労しました。

Uploading code to s3.....
fatal error: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [SwiftAWSS3.S3.ListBucketsOutput.(CodingKeys in _32757E5A7EBE8A89819CEB17805ED919).buckets], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil)): file /Users/yuki/WorkingsPlace/program/oss/Hexaville/Sources/Hexaville/main.swift, line 206
Illegal instruction: 4

どうしたかというと、Hexavilleのissueに表示されたエラーを貼り付けて質問しました。
これはHexavilleのバグでデプロイ失敗していたようで、作者の方に1日もかからず直して頂きました。
Hexavillefileを更新するにはgithubに沿ってコマンド実行するだけです。

他、詰まった例として、

Uploading code to s3.....
Fatal error: AWSResponseError(errorCode: "InvalidLocationConstraint", message: Optional("The specified location-constraint is not valid")): file /Users/yuki/WorkingsPlace/program/oss/Hexaville/Sources/Hexaville/main.swift, line 206
Illegal instruction: 4

これはymlのregion部分での書き方を誤りました。

所感

手順がすんなりいけば、AWSの経験が浅くても、手軽にAWS Lambdaにデプロイできてすごいです。
Hexavillefileはバグへの対処も早いですね。
不満はコマンド実行後の待ち時間ぐらいです。

今回のソースコードはgithubに上がっています。こちら
ありがとうございました!

4
5
3

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
4
5