如何使AWS数据管道ShellCommandActivity脚本执行python文件

use*_*759 4 bash amazon-s3 amazon-web-services

我正在使用具有ShellCommandActivity的AWS Data Pipeline,该ShellCommandActivity将脚本uri设置为位于s3存储桶中的bash文件。bash文件将位于同一s3存储桶中的python脚本复制到EmrCluster,然后该脚本尝试执行该python脚本。

在此处输入图片说明

这是我的管道输出:

{
  "objects": [
    {
      "name": "DefaultResource1",
      "id": "ResourceId_27dLM",
      "amiVersion": "3.9.0",
      "type": "EmrCluster",
      "region": "us-east-1"
    },
    {
      "failureAndRerunMode": "CASCADE",
      "resourceRole": "DataPipelineDefaultResourceRole",
      "role": "DataPipelineDefaultRole",
      "pipelineLogUri": "s3://project/bin/scripts/logs/",
      "scheduleType": "ONDEMAND",
      "name": "Default",
      "id": "Default"
    },
    {
      "stage": "true",
      "scriptUri": "s3://project/bin/scripts/RunPython.sh",
      "name": "DefaultShellCommandActivity1",
      "id": "ShellCommandActivityId_hA57k",
      "runsOn": {
        "ref": "ResourceId_27dLM"
      },
      "type": "ShellCommandActivity"
    }
  ],
  "parameters": []
}
Run Code Online (Sandbox Code Playgroud)

这是RunPython.sh:

#!/usr/bin/env bash
aws s3 cp s3://project/bin/scripts/Test.py ./
python ./Test.py
Run Code Online (Sandbox Code Playgroud)

这是Test.py

__author__ = 'MrRobot'
import re
import os
import sys
import boto3

print "We've entered the python file"
Run Code Online (Sandbox Code Playgroud)

从Stdout日志中,我得到:

下载:s3://project/bin/scripts/Test.py到./

从Stdeer日志中,我得到:

python:无法打开文件“ Test.py”:[Errno 2]没有此类文件或目录

我也尝试过用python Test.py替换python ./Test.py,但是我得到了相同的结果。

如何获取AWS Data Pipeline以执行Test.py脚本。

编辑

当我将scriptUri设置为s3://project/bin/scripts/Test.py时,出现以下错误:

/mnt/taskRunner/output/tmp/df-0947490M9EHH2Y32694-59ed8ca814264f5d9e65b2d52ce78a53/ShellCommandActivityIdJiZP720170209T175934Attempt1_command.sh:第1行:作者:找不到命令/mnt/taskRunner/output/tmp/df-0947490M9EHH2Y32694-59ed8ca814264f5d9e65b2d52ce78a53/ShellCommandActivityIdJiZP720170209T175934Attempt1_command.sh:2号线:进口:找不到命令/mnt/taskRunner/output/tmp/df-0947490M9EHH2Y32694-59ed8ca814264f5d9e65b2d52ce78a53/ShellCommandActivityIdJiZP720170209T175934Attempt1_command.sh:第3行:导入:找不到命令/mnt/taskRunner/output/tmp/df-0947490M9EHH2Y32694-59ed8ca814264f5d9e65b2d52ce78a53/ShellCommandActivityIdJiZP720170209T175934Attempt1_command.sh:第4行:导入:找不到命令/ mnt / taskRunner / e9f76a9e9e2f2e8f9e9e2e2e8f9e2e8e9e2e8e8e9e54e9e9e9e9e9e7e9e7e7e8e9e7e2e9e6e7e7e7e7e7e7e7e7e7e7e7e7b7d9e8e9e2e9e7e7e7e7e7e7e7e7e7e7e7b0d ShellCommandActivityIdJiZP720170209T175934Attempt1_command.sh:第5行:导入:找不到命令/mnt/taskRunner/output/tmp/df-0947490M9EHH2Y32694-59ed8ca814264f5d9e65b2d52ce78a53/ShellCommandActivityIdJiZP720170209T175934Attempt1_command.sh:第7行:打印:找不到命令

编辑2

将以下行添加到Test.py

#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)

然后我收到以下错误:

错误:导入boto3中的第6行,导入错误:没有名为boto3的模块

使用@franklinsijo的建议,我在EmrCluster上创建了一个Bootstrap Action,其值如下:

s3://project/bin/scripts/BootstrapActions.sh

这是BootstrapActions.sh

#!/usr/bin/env bash
sudo pip install boto3
Run Code Online (Sandbox Code Playgroud)

这有效!!!!!!!

fra*_*ijo 7

使用以下命令配置ShellCommandActivity

  • 将python文件的S3 Uri路径作为传递Script Uri
  • #!/usr/bin/env python在脚本中添加shebang行。
  • 如果脚本中使用了任何非默认的python库,请将它们安装在目标资源上。
    • 如果runsOn选择,则将安装命令添加为EMR资源的引导操作
    • 如果workerGroup选择,则在激活管道之前,先在Worker组上安装所有库。

使用pipeasy_install安装python模块。