LoginSignup
1
1

More than 5 years have passed since last update.

CloudFomation:"errorMessage": "Unable to import module 'GreengrassLambda'"への対処

Posted at

これまでの経緯・前提等

  • Greengrassを使って、RaspberryPiにLambdaをデプロイしたい
  • バージョン管理等も含めてやりたかったため、CloudFormationをつかって以下のようなYamlを書き、デプロイした
  • lambdaの関数は、greengrass-hello-worldをコピペした。(Python2.7)
  • GreengrassへのLambdaのデプロイ・サブスクリプションの設定はドキュメント通りに行ったが、Topicが飛んでこない。
  • おかしいと思い、適当なイベントを作成し、テストしてみたところ、以下のようなエラーが発生しており、これが原因でトピックが飛んでこないものと考えられる
error_massage
"errorMessage": "Unable to import module 'GreengrassLambda'"
log
START RequestId: d8e9bd79-7324-45b7-a9fc-9947bae2a222 Version: $LATEST
Unable to import module 'GreengrassLambda': No module named greengrass_common.function_arn_fields

END RequestId: d8e9bd79-7324-45b7-a9fc-9947bae2a222
REPORT RequestId: d8e9bd79-7324-45b7-a9fc-9947bae2a222  Duration: 0.47 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 44 MB  
  • 実際、コンソールでgreengrass-hello-worldを作成し、テストを行うと上記エラーは発生せず、RaspberryPiにデプロイするときちんとTpicが飛んでくる

(以下、長いので読み飛ばしてください。)
要するに、pipでgreengrasssdkをダウンロードしてるし大丈夫でしょ、と思ってた

template.yml
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar

Parameters:
  ProjectId:
    Type: String
    Description: CodeStar projectId used to associate new resources to team members
  CodeDeployRole:
    Type: String
    Description: IAM role to allow AWS CodeDeploy to manage deployment of AWS Lambda functions
  Stage:
    Type: String
    Description: The name for a project pipeline stage, such as Staging or Prod, for which resources are provisioned and deployed.
    Default: ''

Globals:
  Function:
    AutoPublishAlias: live
    DeploymentPreference:
      Enabled: true
      Type: Canary10Percent5Minutes
      Role: !Ref CodeDeployRole

Resources:
  GreengrassLambda:
    Type: AWS::Serverless::Function
    Properties:
      Handler: GreengrassLambda.lambda_handler
      CodeUri: GreengrassLambda
      Runtime: python2.7
      Role: !GetAtt GreengrassLambdaRole.Arn  
      Timeout: 25

  GreengrassLambdaRole:
    Description: Creating service role in IAM for AWS Lambda
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub 'CodeStar-${ProjectId}-GreengrassRole'
      AssumeRolePolicyDocument:
        Statement:
        - Effect: Allow
          Principal:
            Service: [lambda.amazonaws.com]
          Action: sts:AssumeRole
      Path: /
      ManagedPolicyArns:
        -  arn:aws:iam::aws:policy/service-role/AWSGreengrassResourceAccessRolePolicy
        -  arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      PermissionsBoundary: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:policy/CodeStar_${ProjectId}_PermissionsBoundary'

buildspec.yml
version: 0.2

phases:
  install:
    commands:

      # Upgrade AWS CLI to the latest version
      - pip install --upgrade awscli
      - pip install greengrasssdk


  build:
    commands:

      # Use AWS SAM to package the application by using AWS CloudFormation
      - aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml

      # Do not remove this statement. This command is required for AWS CodeStar projects.
      # Update the AWS Partition, AWS Region, account ID and project ID in the project ARN on template-configuration.json file so AWS CloudFormation can tag project resources.
      - sed -i.bak 's/\$PARTITION\$/'${PARTITION}'/g;s/\$AWS_REGION\$/'${AWS_REGION}'/g;s/\$ACCOUNT_ID\$/'${ACCOUNT_ID}'/g;s/\$PROJECT_ID\$/'${PROJECT_ID}'/g' template-configuration.json

artifacts:
  type: zip
  files:
    - template-export.yml
    - template-configuration.json

原因

pipではgreengrasssdkはインストールされるが、付属するgreengrass_commongreengrass_ipc_python_sdkはインストールされていなかった。このため、インポートエラーが発生した

対処法

AWS IoTコンソールからSDKをダウンロードし、greengrass_commongreengrass_ipc_python_sdkをpythonファイルと同一ディレクトリに保存する。

感想

端的に言ってエラーログを最後まで読んでいなかった。これが原因で2日は潰れた。
"errorMessage": "Unable to import moduleで検索すると、ファイルでなくディレクトリを圧縮したときに起こる例ばかりが検索に引っかかり、これがさらに混乱のもととなってしまった。

っていうかお願いだからpipでgreengrass_commongreengrass_ipc_python_sdkもインストールできるようにしてほしい

1
1
0

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
1