【发布时间】:2019-08-04 03:51:18
【问题描述】:
我正在尝试实现一个自定义 AWS Lambda 层,以便将其与我的函数一起使用。
它应该是一个简单的层,从 ssm 获取一些参数并初始化 puresec 的 function_shield 以保护我的服务。
代码看起来更像这样:
import os
import boto3
import function_shield as shield
STAGE = os.environ['stage']
REGION = os.environ['region']
PARAMETERS_PREFIX = os.environ['parametersPrefix']
class ParameterNotFoundException(Exception):
pass
session = boto3.session.Session(region_name=REGION)
ssm = session.client('ssm')
# function_shield config
parameter_path = f"/{PARAMETERS_PREFIX}/{STAGE}/functionShieldToken"
try:
shield_token = ssm.get_parameter(
Name=parameter_path,
WithDecryption=True,
)['Parameter']['Value']
except Exception:
raise ParameterNotFoundException(f'Parameter {parameter_path} not found.')
policy = {
"outbound_connectivity": "block",
"read_write_tmp": "block",
"create_child_process": "block",
"read_handler": "block"
}
def configure(p):
"""
update function_shield policy
:param p: policy dict
:return: null
"""
policy.update(p)
shield.configure({"policy": policy, "disable_analytics": True, "token": shield_token})
configure(policy)
我希望能够将此层链接到我的函数,以便在运行时对其进行保护。
我正在使用无服务器框架,看起来我的层部署得很好,就像我的示例函数一样。此外,AWS 控制台向我显示该层已链接到我的函数中。
我将图层命名为“shield”并尝试在我的测试函数中按其名称导入它:
import os
import shield
def test(event, context):
shield.configure(policy) # this should be reusable for easy tweaking whenever I need to give more or less permissions to my lambda code.
os.system('ls')
return {
'rep': 'ok'
}
理想情况下,我应该在 CloudWatch 上收到错误消息,告诉我 function_shield 阻止了 child_process 运行,但是我收到一个错误消息,告诉我在运行时没有声明“屏蔽”。
我错过了什么? 除了 numpy、scipy、二进制文件等之外,我找不到任何用于层的自定义代码示例。
我很抱歉我的愚蠢...
谢谢你的好意!
【问题讨论】:
标签: python amazon-web-services aws-lambda