1+ from osbot_utils .utils .Http import GET_json
2+ from osbot_utils .utils .Objects import obj
3+ from osbot_aws .aws .lambda_ .Lambda import DEFAULT__LAMBDA__EPHEMERAL_STORAGE , DEFAULT__LAMBDA__MEMORY_SIZE
4+ from osbot_aws .aws .lambda_ .dependencies .Lambda__Dependency import Lambda__Dependency
15from osbot_utils .helpers .Random_Guid import Random_Guid
26from osbot_utils .utils .Env import get_env
3-
4- from osbot_aws .aws .lambda_ .dependencies .Lambda_Upload_Package import Lambda__Upload_Package
57from osbot_fast_api .api .Fast_API import ENV_VAR__FAST_API__AUTH__API_KEY__NAME , ENV_VAR__FAST_API__AUTH__API_KEY__VALUE
68from osbot_aws .AWS_Config import AWS_Config
79from osbot_aws .deploy .Deploy_Lambda import Deploy_Lambda
810from osbot_utils .decorators .methods .cache_on_self import cache_on_self
911from osbot_utils .helpers .Safe_Id import Safe_Id
10- from osbot_utils .helpers .duration .decorators .capture_duration import capture_duration
1112from osbot_utils .type_safe .Type_Safe import Type_Safe
12- from osbot_fast_api_serverless .fast_api .lambda_handler import LAMBDA_DEPENDENCIES , run
13+ from osbot_fast_api_serverless .fast_api .lambda_handler import run , LAMBDA_DEPENDENCIES
1314from osbot_fast_api_serverless .deploy .Schema__AWS_Setup__Serverless__Fast_API import Schema__AWS_Setup__Serverless__Fast_API
1415
15- BASE__LAMBDA_NAME = 'serverless_fast_api' # make this a Safe_Str__Lambda_Name
16-
17-
16+ BASE__LAMBDA_NAME__FAST_API__SERVERLESS = 'fast-api__serverless' # make this a Safe_Str__Lambda_Name
17+ DEFAULT__ERROR_MESSAGE__WHEN_FAST_API_IS_OK = ('The adapter was unable to infer a handler to use for the '
18+ 'event. This is likely related to how the Lambda function was '
19+ 'invoked. (Are you testing locally? Make sure the request '
20+ 'payload is valid for a supported handler.)' )
1821
1922class Deploy__Serverless__Fast_API (Type_Safe ):
20- stage : Safe_Id = Safe_Id ('dev' )
21-
22- @cache_on_self
23- def aws_config (self ):
24- return AWS_Config ()
23+ aws_config : AWS_Config
24+ stage : Safe_Id = Safe_Id ('dev' )
25+ ephemeral_storage : int = DEFAULT__LAMBDA__EPHEMERAL_STORAGE
26+ memory_size : int = DEFAULT__LAMBDA__MEMORY_SIZE
2527
2628 @cache_on_self
2729 def api_key__name (self ):
@@ -37,14 +39,24 @@ def s3(self):
3739
3840 @cache_on_self
3941 def deploy_lambda (self ):
40- with Deploy_Lambda (run , lambda_name = self .lambda_name ()) as _ :
41- _ .add_osbot_aws ()
42+ kwargs = dict (lambda_name = self .lambda_name () ,
43+ stage = self .stage ,
44+ ephemeral_storage = self .ephemeral_storage ,
45+ memory_size = self .memory_size )
46+ with Deploy_Lambda (run , ** kwargs ) as _ :
47+ _ .add_file__boto3__lambda () # this file allows the dynamically load of dependencies
4248 _ .set_env_variable (ENV_VAR__FAST_API__AUTH__API_KEY__NAME , self .api_key__name ())
4349 _ .set_env_variable (ENV_VAR__FAST_API__AUTH__API_KEY__VALUE , self .api_key__value ())
4450 return _
4551
46- # main methods
52+ def delete (self ):
53+ return self .lambda_function ().delete ()
4754
55+ def create (self ):
56+ if self .create_or_update__lambda_function ():
57+ self .create__lambda_function__url ()
58+ return True
59+ return False
4860
4961 def create_or_update__lambda_function (self ):
5062 with self .deploy_lambda () as _ :
@@ -66,20 +78,34 @@ def create__lambda_function__url(self):
6678 function_url = result .get ('function_url_create' ).get ('FunctionUrl' )
6779 return function_url
6880
81+ def invoke (self , payload = None ):
82+ return self .lambda_function ().invoke (payload )
83+
84+ def invoke__function_url (self , path = '' ):
85+ with self .lambda_function () as _ :
86+ url = _ .function_url () + path
87+ headers = { self .api_key__name (): self .api_key__value ()}
88+ return GET_json (url , headers = headers )
89+
90+ def lambda_configuration (self ):
91+ return obj (self .lambda_function ().info ().get ('Configuration' ))
92+
6993 def lambda_name (self ):
70- return f' { BASE__LAMBDA_NAME } __ { self . stage } '
94+ return BASE__LAMBDA_NAME__FAST_API__SERVERLESS
7195
7296 def lambda_function (self ):
7397 return self .deploy_lambda ().lambda_function ()
7498
99+
100+
75101 def lambda_files_bucket_name (self ):
76102 return self .lambda_function ().s3_bucket
77103
78104 def setup_aws_environment (self ):
79105
80106 kwargs = dict (bucket__osbot_lambdas__exists = self .s3 ().bucket_exists (self .lambda_files_bucket_name ()),
81107 bucket__osbot_lambdas__name = self .lambda_files_bucket_name (),
82- current_aws_region = self .aws_config () .region_name ())
108+ current_aws_region = self .aws_config .region_name ())
83109
84110 aws_setup = Schema__AWS_Setup__Serverless__Fast_API (** kwargs )
85111 with aws_setup as _ :
@@ -91,10 +117,17 @@ def setup_aws_environment(self):
91117 raise Exception (result )
92118 return aws_setup
93119
120+
94121 def upload_lambda_dependencies_to_s3 (self ):
95- with Lambda__Upload_Package () as _ :
96- return _ .upload_lambda_dependencies (LAMBDA_DEPENDENCIES )
97- # status__packages = {}
122+ upload_results = {}
123+ for package_name in LAMBDA_DEPENDENCIES :
124+ with Lambda__Dependency (package_name = package_name ) as _ :
125+ upload_results [package_name ] = _ .install_and_upload ()
126+ return upload_results
127+
128+ # with Lambda__Upload_Package() as _:
129+ # return _.upload_lambda_dependencies(LAMBDA_DEPENDENCIES)
130+ # status__packages = {}
98131 # for package in LAMBDA_DEPENDENCIES:
99132 # if
100133 # with capture_duration() as duration__install_locally:
0 commit comments