Source code for runway.cfngin.hooks.awslambda._python_hooks
"""Hook for creating an AWS Lambda Function using Python runtime."""from__future__importannotationsimportloggingfromtypingimportTYPE_CHECKING,Any,ClassVarfrom....compatimportcached_propertyfrom.base_classesimportAwsLambdaHookfrom.models.argsimportPythonHookArgsfrom.python_requirementsimportPythonDeploymentPackage,PythonProjectifTYPE_CHECKING:from....contextimportCfnginContextfrom.base_classesimportDeploymentPackageLOGGER=logging.getLogger(__name__.replace("._","."))
[docs]classPythonFunction(AwsLambdaHook[PythonProject]):"""Hook for creating an AWS Lambda Function using Python runtime."""BUILD_LAYER:ClassVar[bool]=False"""Flag to denote that this hook creates a Lambda Function deployment package."""args:PythonHookArgs"""Parsed hook arguments."""
@cached_propertydefdeployment_package(self)->DeploymentPackage[PythonProject]:"""AWS Lambda deployment package."""returnPythonDeploymentPackage.init(self.project,"layer"ifself.BUILD_LAYERelse"function")@cached_propertydefproject(self)->PythonProject:"""Project being deployed as an AWS Lambda Function."""returnPythonProject(self.args,self.ctx)
[docs]defcleanup(self)->None:"""Cleanup after execution."""self.project.cleanup()
[docs]defcleanup_on_error(self)->None:"""Cleanup after an error has occurred."""self.deployment_package.delete()self.project.cleanup_on_error()
[docs]defpre_deploy(self)->Any:"""Run during the **pre_deploy** stage."""try:self.deployment_package.upload()returnself.build_response("deploy").model_dump(by_alias=True)exceptBaseException:self.cleanup_on_error()raisefinally:self.cleanup()
[docs]classPythonLayer(PythonFunction):"""Hook for creating an AWS Lambda Layer using Python runtime."""BUILD_LAYER:ClassVar[bool]=True"""Flag to denote that this hook creates a Lambda Layer deployment package."""