@@ -6,6 +6,7 @@ use aws_sdk_iam::Client as IamClient;
66use aws_sdk_lambda:: Client as LambdaClient ;
77use aws_sdk_sts:: error:: ProvideErrorMetadata ;
88use rusteze_config:: RoutesConfig as Manifest ;
9+ use tracing:: { debug, error, info} ;
910
1011use crate :: aws:: lambda:: build:: { build_lambda_functions, deploy_function} ;
1112use std:: collections:: HashMap ;
@@ -14,22 +15,20 @@ use std::fs;
1415pub async fn deploy_to_aws (
1516 config : & RustezeConfig ,
1617) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
17- println ! (
18+ info ! (
1819 "Starting AWS deployment for service: {}" ,
1920 config. service_name
2021 ) ;
2122
2223 // Read the manifest file
2324 let manifest_content = fs:: read_to_string ( ".rusteze/manifest.json" ) ?;
24- println ! ( "Loading manifest: {}" , & manifest_content) ;
25+ debug ! ( "Loading manifest: {}" , & manifest_content) ;
2526 let mut manifest: Manifest = serde_json:: from_str ( & manifest_content) ?;
26- println ! ( "manifest loaded! Building functions" ) ;
27+ debug ! ( "manifest loaded! Building functions" ) ;
2728
28- // Build all Lambda functions
2929 build_lambda_functions ( ) ?;
3030
31- // Set up AWS clients
32- println ! (
31+ debug ! (
3332 "Setting up AWS clients for region: {}" ,
3433 config. deployment. region
3534 ) ;
@@ -40,17 +39,17 @@ pub async fn deploy_to_aws(
4039 . load ( )
4140 . await ;
4241
43- println ! ( "AWS region: {:?}" , shared_config. region( ) ) ;
42+ debug ! ( "AWS region: {:?}" , shared_config. region( ) ) ;
4443
4544 let lambda_client = LambdaClient :: new ( & shared_config) ;
4645 let api_client = ApiGatewayClient :: new ( & shared_config) ;
4746 let iam_client = IamClient :: new ( & shared_config) ;
4847
4948 // Get account ID first to verify credentials work
50- println ! ( "Verifying AWS credentials..." ) ;
49+ debug ! ( "Verifying AWS credentials..." ) ;
5150 let account_id = match get_account_id ( ) . await {
5251 Ok ( id) => {
53- println ! ( "✓ AWS Account ID: {}" , id) ;
52+ debug ! ( "✓ AWS Account ID: {}" , id) ;
5453 id
5554 }
5655 Err ( e) => {
@@ -63,7 +62,7 @@ pub async fn deploy_to_aws(
6362 } ;
6463
6564 // Ensure Lambda execution role exists
66- println ! ( "Setting up IAM role for Lambda execution..." ) ;
65+ debug ! ( "Setting up IAM role for Lambda execution..." ) ;
6766 let role_arn = match ensure_lambda_execution_role ( & iam_client, & account_id) . await {
6867 Ok ( arn) => arn,
6968 Err ( e) => {
@@ -87,14 +86,14 @@ pub async fn deploy_to_aws(
8786 let mut deployed_functions = HashMap :: new ( ) ;
8887
8988 for ( route_name, route_info) in & manifest. route {
90- println ! (
89+ debug ! (
9190 "Deploying Lambda function for route: {} {} {}" ,
9291 route_info. method, route_info. path, route_name
9392 ) ;
9493
9594 let function_name = format ! ( "{}-{}" , config. service_name, route_info. binary) ;
9695
97- println ! ( "Role being used: {}" , & role_arn) ;
96+ debug ! ( "Role being used: {}" , & role_arn) ;
9897
9998 let function_arn = match deploy_function (
10099 & config,
@@ -111,10 +110,10 @@ pub async fn deploy_to_aws(
111110 }
112111 } ;
113112
114- println ! ( "Finished deploying lambda: {:?}" , & function_arn) ;
113+ debug ! ( "Finished deploying lambda: {:?}" , & function_arn) ;
115114
116115 deployed_functions. insert ( route_name. clone ( ) , ( function_name. clone ( ) , function_arn) ) ;
117- println ! ( "✓ Deployed Lambda function: {}" , function_name) ;
116+ debug ! ( "✓ Deployed Lambda function: {}" , function_name) ;
118117 }
119118
120119 // Deploy topic subscriber functions
@@ -123,7 +122,7 @@ pub async fn deploy_to_aws(
123122 if let Some ( ref topic_map) = manifest. topic {
124123 for ( topic_name, topic_info) in topic_map {
125124 for ( subscriber_name, subscriber_info) in & topic_info. subscribers {
126- println ! (
125+ debug ! (
127126 "Deploying subscriber function: {} for topic: {}" ,
128127 subscriber_name, topic_name
129128 ) ;
@@ -158,7 +157,7 @@ pub async fn deploy_to_aws(
158157
159158 if let Some ( ref auth_map) = manifest. auth {
160159 for ( auth_name, auth_info) in auth_map {
161- println ! ( "Deploying auth function: {}" , auth_name) ;
160+ debug ! ( "Deploying auth function: {}" , auth_name) ;
162161
163162 let function_name = format ! ( "{}-{}" , config. service_name, auth_info. binary) ;
164163
@@ -182,19 +181,14 @@ pub async fn deploy_to_aws(
182181 }
183182
184183 // Set up API Gateway
185- println ! ( "Setting up API Gateway..." ) ;
186-
187- // let api_name = &config.service_name;
188- // let apis = api_client.get_apis().send().await?;
189- // let existing_api = apis.items().iter().find(|a| a.name() == Some(api_name));
184+ debug ! ( "Setting up API Gateway..." ) ;
190185
191186 let api_gateway_id = build_api_gateway ( config, & api_client) . await ?;
192187
193- // Create routes and integrations
194188 // @todo - need to also remove old routes + integrations that no longer exist on the updated code.
195189 for ( route_name, route_info) in & manifest. route {
196190 if let Some ( ( function_name, function_arn) ) = deployed_functions. get ( route_name) {
197- println ! (
191+ debug ! (
198192 "Creating API route: {} {}" ,
199193 route_info. method, route_info. path
200194 ) ;
@@ -277,7 +271,7 @@ pub async fn deploy_to_aws(
277271 . send ( )
278272 . await ; // Ignore errors as permission might already exist
279273
280- println ! ( "✓ Created route: {}" , route_key) ;
274+ info ! ( "✓ Created route: {}" , route_key) ;
281275 }
282276 }
283277
@@ -305,13 +299,13 @@ pub async fn deploy_to_aws(
305299 & api_gateway_id,
306300 ) ?;
307301
308- println ! ( "\n 🚀 Deployment complete!" ) ;
309- println ! ( "API Gateway ID: {}" , api_gateway_id) ;
310- println ! ( "API URL: {}" , api_url) ;
311- println ! ( "\n Available endpoints:" ) ;
302+ info ! ( "\n 🚀 Deployment complete!" ) ;
303+ info ! ( "API Gateway ID: {}" , api_gateway_id) ;
304+ info ! ( "API URL: {}" , api_url) ;
305+ info ! ( "\n Available endpoints:" ) ;
312306
313307 for ( _, route_info) in & manifest. route {
314- println ! (
308+ debug ! (
315309 " {} {} -> {}{}" ,
316310 route_info. method, route_info. path, api_url, route_info. path
317311 ) ;
@@ -341,30 +335,29 @@ async fn ensure_lambda_execution_role(
341335 let role_name = "lambda-execution-role" ;
342336 let role_arn = format ! ( "arn:aws:iam::{}:role/{}" , account_id, role_name) ;
343337
344- println ! ( "Checking for Lambda execution role: {}" , role_name) ;
338+ debug ! ( "Checking for Lambda execution role: {}" , role_name) ;
345339
346340 // Check if role exists
347341 match iam_client. get_role ( ) . role_name ( role_name) . send ( ) . await {
348342 Ok ( role_response) => {
349- println ! ( "✓ Lambda execution role already exists: {}" , role_name) ;
343+ debug ! ( "✓ Lambda execution role already exists: {}" , role_name) ;
350344
351345 // Verify the role has the correct trust policy
352346 if let Some ( role) = role_response. role ( ) {
353347 if let Some ( trust_policy) = role. assume_role_policy_document ( ) {
354348 if !trust_policy. contains ( "lambda.amazonaws.com" ) {
355- println ! (
349+ info ! (
356350 "⚠️ Warning: Existing role may not have correct trust policy for Lambda"
357351 ) ;
358- println ! (
352+ info ! (
359353 "Trust policy should contain 'lambda.amazonaws.com' service principal"
360354 ) ;
361355 } else {
362- println ! ( "✓ Role has correct Lambda trust policy" ) ;
356+ info ! ( "✓ Role has correct Lambda trust policy" ) ;
363357 }
364358 }
365359 }
366360
367- // Check if the basic execution policy is attached
368361 let policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" ;
369362 match iam_client
370363 . list_attached_role_policies ( )
@@ -379,20 +372,20 @@ async fn ensure_lambda_execution_role(
379372 . any ( |p| p. policy_arn ( ) == Some ( policy_arn) ) ;
380373
381374 if !has_basic_policy {
382- println ! ( "⚠️ Attaching missing AWSLambdaBasicExecutionRole policy..." ) ;
375+ info ! ( "⚠️ Attaching missing AWSLambdaBasicExecutionRole policy..." ) ;
383376 iam_client
384377 . attach_role_policy ( )
385378 . role_name ( role_name)
386379 . policy_arn ( policy_arn)
387380 . send ( )
388381 . await ?;
389- println ! ( "✓ Attached AWSLambdaBasicExecutionRole policy" ) ;
382+ info ! ( "✓ Attached AWSLambdaBasicExecutionRole policy" ) ;
390383 } else {
391- println ! ( "✓ Role has required execution policy" ) ;
384+ info ! ( "✓ Role has required execution policy" ) ;
392385 }
393386 }
394387 Err ( e) => {
395- println ! ( "⚠️ Could not check attached policies: {}" , e) ;
388+ error ! ( "⚠️ Could not check attached policies: {}" , e) ;
396389 }
397390 }
398391
@@ -401,9 +394,9 @@ async fn ensure_lambda_execution_role(
401394 Err ( e) => {
402395 // Check for specific error types
403396 if let Some ( service_error) = e. as_service_error ( ) {
404- println ! ( "IAM service error: {:?}" , service_error) ;
397+ debug ! ( "IAM service error: {:?}" , service_error) ;
405398 if service_error. is_no_such_entity_exception ( ) {
406- println ! ( "Role doesn't exist, will create it" ) ;
399+ info ! ( "Role doesn't exist, will create it" ) ;
407400 } else {
408401 return Err ( format ! (
409402 "Error checking for role '{}': {} (Code: {:?})" ,
@@ -425,7 +418,7 @@ async fn ensure_lambda_execution_role(
425418 }
426419 }
427420
428- println ! ( "Creating Lambda execution role: {}" , role_name) ;
421+ debug ! ( "Creating Lambda execution role: {}" , role_name) ;
429422
430423 // Trust policy document for Lambda - ensure proper JSON formatting
431424 let trust_policy = r#"{
@@ -450,7 +443,7 @@ async fn ensure_lambda_execution_role(
450443 . send ( )
451444 . await
452445 {
453- Ok ( _) => println ! ( "✓ Successfully created role: {}" , role_name) ,
446+ Ok ( _) => info ! ( "✓ Successfully created role: {}" , role_name) ,
454447 Err ( e) => {
455448 if let Some ( service_error) = e. as_service_error ( ) {
456449 return Err ( format ! (
@@ -481,7 +474,7 @@ async fn ensure_lambda_execution_role(
481474 . send ( )
482475 . await
483476 {
484- Ok ( _) => println ! ( "✓ Successfully attached policy: {}" , policy_arn) ,
477+ Ok ( _) => info ! ( "✓ Successfully attached policy: {}" , policy_arn) ,
485478 Err ( e) => {
486479 if let Some ( service_error) = e. as_service_error ( ) {
487480 return Err ( format ! (
@@ -503,15 +496,15 @@ async fn ensure_lambda_execution_role(
503496 }
504497 }
505498
506- println ! ( "✓ Created Lambda execution role: {}" , role_name) ;
499+ info ! ( "✓ Created Lambda execution role: {}" , role_name) ;
507500
508501 // Wait a moment for the role to propagate in AWS
509- println ! ( "Waiting for role to propagate..." ) ;
502+ info ! ( "Waiting for role to propagate..." ) ;
510503 tokio:: time:: sleep ( tokio:: time:: Duration :: from_secs ( 10 ) ) . await ;
511504
512505 // Verify the role was created and can be retrieved
513506 match iam_client. get_role ( ) . role_name ( role_name) . send ( ) . await {
514- Ok ( _) => println ! ( "✓ Role propagation verified" ) ,
507+ Ok ( _) => info ! ( "✓ Role propagation verified" ) ,
515508 Err ( e) => {
516509 return Err ( format ! (
517510 "Role was created but cannot be retrieved (propagation issue): {}" ,
0 commit comments