# AWS Batchを自前でVPCを構築して動かす

- URL: https://www.blog.danishi.net/2020/03/07/post-3216/
- 公開日: 2020-03-07
- 更新日: 2025-05-31
- 著者: danishi
- カテゴリー: 使ってみた
- タグ: AWS, CloudWatch Logs, AWS Batch, EC2, VPC, ECS, コンテナ
- この記事には広告（アフィリエイトリンク）が含まれています。

[AWS Batch](https://aws.amazon.com/jp/batch/)はフルマネージドのバッチ処理環境を用意してくれるサービスです。

このスライドが概要をかなり分かりやすく説明してくれています。

[https://www.slideshare.net/AmazonWebServicesJapan/20190911-aws-black-belt-online-seminar-aws-batch](https://www.slideshare.net/AmazonWebServicesJapan/20190911-aws-black-belt-online-seminar-aws-batch)（www.slideshare.net）

ジョブを定義、キューイングしてスケーラブルに実行してくれるサービスのようですね。

バッチ処理でよく必要になるジョブスケジューリングまでは含んでないようなので、そこは他で工夫する必要がありそうです。

---

## VPCを構築する

AWS Batchには実行環境となるためのVPCが必要なので、事前にVPCを構築します（デフォルトVPCを使ってもよい）。

まずは、VPCを作成します。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-1024x335.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/1-1024x194.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/1.png)

VPCのDNSホスト名を有効にしておきましょう。

次はサブネットを作ります。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/1-1-1024x362.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/1-1.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/1-2-1024x235.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/1-2.png)

IPv4の自動割り当てを有効にしておきましょう。

インターネットゲートウェイを作ってVPCにアタッチします。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-1.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-1.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file.png)

サブネットのルートテーブルにインターネットゲートウェイをデフォルトゲートウェイとして設定します。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-1.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-1.png)

最後にセキュリティグループを作成します。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-2.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-2.png)

インバウンドのルールは、タイプをすべてでソースをセキュリティグループ自身に設定します。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-3-1024x253.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-3.png)

これで、インターネットに出れるVPC環境ができました。\
いよいよAWS Batchを設定します。

## AWS Batchを設定する

ウィザードの流れに沿って進めていきます。\
ここではジョブとジョブキュー、コンピューティング環境を一度に作成します。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-2-1024x559.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-2.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-3-1024x486.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-3.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-4-1024x621.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-4.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-5-1024x336.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-5.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-6-1024x520.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-6.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-7-1024x490.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-7.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-4-1024x664.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/unnamed-file-4.png)

VPCだけ、先ほど作ったものを設定し。\
作成すれば、コンピューティング環境とジョブキューが定義され、ジョブが実行されます。

## 実行されたジョブを確認する

ダッシュボードを確認すると、先ほど作成したジョブがキューに投入されているのが分かります。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-8-1024x243.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-8.png)

ジョブの状況に応じてステータスが遷移していきます。\
**RUNNABLEのまま止まってしまう場合**は事前に作成したVPCの設定に問題があるかもしれません。

裏で、先ほど作ったVPCにEC2インスタンスが立ち上がります。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-9-1024x144.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-9.png)

初回はこの立ち上がりがあるのでジョブの実行に少し時間がかかりますが、二回目以降は立ち上がっているのではすぐに実行されます。\
<strong>裏を返せば立ち上がりっぱなしなので、必要がなければインスタンスは削除しましょう。</strong>\
コンピューティング環境のリンクからそのEC2インスタンスで実行されているECSクラスタも確認できます。

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-11-1024x169.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-11.png)

[![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-12-1024x432.png)](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-12.png)

AWS Batchは標準出力がCloudWatch Logに出力されます。\
実行したジョブの出力結果が見れます。

![](https://www.blog.danishi.net/wp-content/uploads/2020/03/image-10-1024x232.png)

---

以上、簡単にですがジョブが実行できました。

最初はフルマネージドのバッチサーバーみたいなものを想像していたのですが、まったく毛色が違うものでした。\
バッチ処理をコンテナ化していることが前提条件なので、既存のバッチサーバーをそのまま置き換えるとかは難度が高そう。

では。

---

[Docker/Kubernetes 実践コンテナ開発入門](https://www.amazon.co.jp/dp/4297100339?tag=danishi0a-22)（Amazon、PR）
