尝试在AWS Lambda函数python中使用bash脚本创建文件时出现错误“只读文件系统”

问题描述

我在挂载/mnt/my-mount中具有连接到EFS的python(3.8)Lambda函数

我想通过该函数运行bash脚本,所以我创建了另一个文件script.sh

这是python函数

import json
import os

def lambda_handler(event,context):
    os.system("sh script.sh")

和bash脚本script.sh

#!/bin/bash
touch hello.txt

,我收到以下错误消息:

cannot touch script.sh: Read-only file system

注意:

  • 我可以使用python函数(f.write)创建文件
  • 如果我运行os.system("chmod 777 a.sh"),我将再次得到Read-Only file system
  • 如果我使用rc = subprocess.call("bash a.sh"),我会得到No such file or directory: 'bash a.sh'
  • EFS具有具有1000:1000权限的用户777的访问点。

解决方法

这可能是因为 Lambda 角色没有适当的写入权限。您应该附加适当的政策或定义您的自定义政策: enter image description here