LoginSignup
1

More than 5 years have passed since last update.

DataPipelineを使ってdockerコンテナを定期実行してみる

Last updated at Posted at 2017-05-27

やること

cronでジョブを実行するのと同じような感じで、DataPipelineを使ってdockerコンテナを定期実行するようにしてみる。
ジョブを定時実行するパイプラインがさくっと出来上がるようなCloudFormationのテンプレートを作る。

用意するもの

Docker Imageの実行定義

TaskDefinitionをrunするためのクラスター

CloudFormation Template

CloudFormationパラメータ

パラメータ名
EcsClusterName タスクを実行するクラスタ名
EcsTaskDefinitioinArn クラスタ上で実行するタスク

30分ごとにDataPipelineがEC2インスタンスを立ち上げaws-cliのecs run-taskコマンドでTaskDefinitionを実行しています。

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  EcsClusterName:
    Type: String
    Description: Enter ECS Cluster Name to run docker.
  EcsTaskDefinitioinArn:
    Type: String
    Description: Enter TaskDefinitionArn to run.
Resources:
  EcsRunTaskSample:
    Type: 'AWS::DataPipeline::Pipeline'
    Properties:
      Name: ecs-run-task-sample
      Description: "Pipeline to run task on ECS"
      Activate: true
      ParameterObjects:
        - 
          Id: "myAWSCLICmd"
          Attributes: 
            - 
              Key: "description"
              StringValue: "command to execute"
            - 
              Key: "type"
              StringValue: "String"
      ParameterValues:
        - 
          Id: "myAWSCLICmd"
          StringValue:
            Fn::Join:
              - ""
              - - "AWS_DEFAULT_REGION="
                - Ref: AWS::Region
                - " aws ecs run-task --cluster "
                - Ref: EcsClusterName
                - " --task-definition "
                - Ref: EcsTaskDefinitioinArn
      PipelineObjects:
        - 
          Id: "Default"
          Name: "Default"
          Fields: 
            - 
              Key: "type"
              StringValue: "Default"
            - 
              Key: "failureAndRerunMode"
              StringValue: "CASCADE"
            - 
              Key: "role"
              StringValue: "DataPipelineDefaultRole"
            - 
              Key: "schedule"
              RefValue: "DefaultSchedule"
            - 
              Key: "resourceRole"
              StringValue: "DataPipelineDefaultResourceRole"
            - 
              Key: "scheduleType"
              StringValue: "cron"
        - 
          Id: "DefaultSchedule"
          Name: "Every 30 minutes"
          Fields: 
            - 
              Key: "parent"
              RefValue: "Default"
            - 
              Key: "type"
              StringValue: "Schedule"
            - 
              Key: "period"
              StringValue: "30 Minutes"
            - 
              Key: "startAt"
              StringValue: "FIRST_ACTIVATION_DATE_TIME"
        - 
          Id: "Ec2Instance"
          Name: "Ec2Instance"
          Fields: 
            - 
              Key: "parent"
              RefValue: "Default"
            - 
              Key: "type"
              StringValue: "Ec2Resource"
            - 
              Key: "instanceType"
              StringValue: "t2.micro"
            - 
              Key: "terminateAfter"
              StringValue: "10 Minutes"
        - 
          Id: "CliActivity"
          Name: "CliActivity"
          Fields: 
            - 
              Key: "parent"
              RefValue: "Default"
            - 
              Key: "type"
              StringValue: "ShellCommandActivity"
            - 
              Key: "runsOn"
              RefValue: "Ec2Instance"
            - 
              Key: "command"
              StringValue: "(sudo yum -y update aws-cli) && (#{myAWSCLICmd})"

ParameterObjects&ParameterValues

DataPipeline用のParameterが使えるようなので宣言する。(変数はmyで始めなきゃいけないという決まりがある)
myAWSCLICmdを宣言してCliActivityで実行するコマンドとして使う。

PipelineObjects

実際に実行するパイプラインの内容。

パイプラインオブジェクトの種類 type やってること
Schedule Schedule 30分毎にActivityを実行する
Resource Ec2Resource t2.microインスタンスを用意する。
Activity ShellCommandActivity aws-cliのaws run-taskでECSクラスター上にタスクを走らせる。

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
1