如何解决基于Python的AWS Lambda代码中的“没有名为'mysql'的模块”错误?

2024-06-02 01:08:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python、MySQL、AWS API网关、AWS Lambda测试一个文件。下面是代码

data.py

import json
import mysql.connector

    def lambda_handler(event, context):
    
        mydb = mysql.connector.connect(
        host="aaaa.cbandqavxx.us-east-1.rds.amazonaws.com",
        user="admin",
        password="admin123",
        database="aaaa"
        )
    
        mycursor = mydb.cursor()
    
        mycursor.execute("SELECT * FROM accounting_type")
    
        myresult = mycursor.fetchall()
    
        for x in myresult:
            print(x)

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  python-test

  Sample SAM Template for python-test

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

  DatabaseFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: data.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /data
            Method: get
      Role: !GetAtt LambdaRole.Arn
      VpcConfig:
        SecurityGroupIds:
          - sg-041f2459dcd921e8e
        SubnetIds:
          - subnet-0381db2d
          - subnet-c4d5c4cb
  
  LambdaRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Policies:
        - PolicyName: root
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - ec2:DescribeNetworkInterfaces
                  - ec2:CreateNetworkInterface
                  - ec2:DeleteNetworkInterface
                  - ec2:DescribeInstances
                  - ec2:AttachNetworkInterface
                Resource: '*'

当我将其部署到AWS时,我得到以下错误

START RequestId: 68bc2bf7-c0c8-4884-9d72-b5dc3b8277f1 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'data': No module named 'mysql'
Traceback (most recent call last):
END RequestId: 68bc2bf7-c0c8-4884-9d72-b5dc3b8277f1
REPORT RequestId: 68bc2bf7-c0c8-4884-9d72-b5dc3b8277f1  Duration: 1.68 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 51 MB  

这个问题是什么

另外,我通过下载文件-https://dev.mysql.com/downloads/connector/python/从这里安装了python


Tags: httpsinfogithubmastercomawsmodelapplication