血と汗となみだを流す

個人の思ったこと、やったことの吐き出し口です。

【未完】Developers.ioの記事を読んでやってみる「API Gateway + LambdaでAWS BatchのJobを実行する」(2018/03/22)

対象

やってみる

API Gateway + LambdaからBatch Jobを実行する

  • APIに対してリクエストを送る
  • LambdaはBatchに対してジョブの実行を要求してすぐにレスポンスを返す
  • Batchは非同期でジョブを実行する
    • Batchを使うことでジョブの要求数に応じて環境をスケールさせて実行できる

Batchの構築

  • 公式ページを見ながらterraformで書いてみる
  • Batchを作る上で必要なもの
    • VPC
    • Subnet
    • IAM Policy
      • Batch用(AWSBatchServiceRole)
      • ECS用(AmazonEC2COntainerServiceforEC2Role)
    • Security Group
  • EC2のタイプは以下じゃないと駄目っぽい(t2.microにしたらエラー)
Error executing request, Exception : Instance type can only be one of [r3.8xlarge, r3, r4, optimal, m4.10xlarge, m3.xlarge, r4.16xlarge, m4.xlarge, i3.8xlarge, g2.2xlarge, m4.2xlarge, c3.4xlarge, i3.4xlarge, p2.16xlarge, c4.2xlarge, d2.xlarge, g2.8xlarge, d2.8xlarge, x1.16xlarge, r3.2xlarge, g2, p3.8xlarge, c3, x1.32xlarge, c4, r4.4xlarge, d2.2xlarge, m4.large, i2.xlarge, x1, m4.4xlarge, c4.8xlarge, i2.2xlarge, p2.xlarge, p2, i2.8xlarge, p3, c4.large, r3.xlarge, r3.large, d2, r4.xlarge, c4.4xlarge, m3.2xlarge, c4.xlarge, c3.xlarge, r4.8xlarge, p3.2xlarge, m3.medium, r4.2xlarge, i3.2xlarge, m3, i3.16xlarge, m4, r4.large, m4.16xlarge, i2, i3, c3.2xlarge, c3.large, i2.4xlarge, m3.large, i3.xlarge, p2.8xlarge, p3.16xlarge, d2.4xlarge, c3.8xlarge, r3.4xlarge]
  • ジョブキューのARN確認
$ aws batch help
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument command: Invalid choice, valid choices are:
...
  • aws-cliのバージョン確認
aws-cli/1.9.9 Python/2.7.10 Darwin/17.4.0 botocore/1.3.9
  • aws-cli/1.9.9だと aws batch 無いっぽい
  • aws-cliを最新化
$ pip install awscli --upgrade --user
Collecting awscli
  Downloading awscli-1.14.50-py2.py3-none-any.whl (1.2MB)
    100% |████████████████████████████████| 1.2MB 624kB/s
Collecting colorama<=0.3.7,>=0.2.5 (from awscli)
...
$ aws --version
aws-cli/1.14.50 Python/2.7.10 Darwin/17.4.0 botocore/1.9.3
$ aws batch describe-job-queues
{
    "jobQueues": [
        {
            "status": "VALID",
            "jobQueueArn": "arn:aws:batch:ap-northeast-1:xxxxxxxxxxxx:job-queue/batch-job-queue",
            "computeEnvironmentOrder": [
                {
                    "computeEnvironment": "arn:aws:batch:ap-northeast-1:xxxxxxxxxxxx:compute-environment/batch",
                    "order": 0
                }
            ],
            "statusReason": "JobQueue Healthy",
            "priority": 1,
            "state": "ENABLED",
            "jobQueueName": "batch-job-queue"
        }
    ]
}

Lambda + API Gatewayの構築

実行してみる

curl -X POST -d '{ "JobName" : "testName" }'  https://xxxxxxxxxxxxxx.ap-northeast-1.amazonaws.com/development/resource

※「xxxxxxxxxxxxxx」はAPI Gatewayのendpoint
  • ジョブキューに入ったことを確認! f:id:Anorlondo448:20180321162923p:plain
  • が、runnableのまま動かず・・・
    • vCPU数が、ジョブ定義のvCPU数を満たしていなかった
    • vCPUを増やしたら実行確認!

が、、、

  • terraformから実行すると、以下エラーがでる。
CLIENT_ERROR - One or more security groups configured in the launch configuration are not linked to the VPCs configured in the Auto Scaling group
  • 何か足りてないっぽい・・・
  • せっかくサーバレスなのに、Launch Configとか気にしないといけないのは・・・
  • 複合リソースのものはterraformでやらないほうがよい?
プライバシーポリシー