[{"data":1,"prerenderedAt":81},["ShallowReactive",2],{"content-guides-refactoring-serverless-extract_send_message":3},{"markdown":4,"frontMatterAttributes":5,"bodyRaw":7,"contributorPaths":8,"menuType":12,"contentName":13,"slug":14,"readingTime":15,"menu":16,"nextDocItem":48,"previousDocItem":20},"\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fguides\u002Frefactoring-serverless\u002Fassets\u002Fimages\u002Fmessage-send.png\"\u002F>\n\n\u003Ch2>Description\u003C\u002Fh2>\n\u003Cp>When Lambda function code sends a message to a SQS or SNS channel directly, the application&#39;s topology is hidden inside the function code. To figure out how messages move, you have to look at the code.\u003C\u002Fp>\n\u003Cp>An example might look as follows:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-py\">queue_name = os.environ[&#39;SQS_NAME&#39;]\nsqs = boto3.resource(&#39;sqs&#39;)\nqueue = sqs.get_queue_by_name(QueueName=queue_name)\n\ndef handler(event, context):\n   ...\n  response = queue.send_message(MessageBody=&#39;world&#39;)\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2>Solution\u003C\u002Fh2>\n\u003Cp>Use \u003Ca href=\"https:\u002F\u002Faws.amazon.com\u002Fblogs\u002Fcompute\u002Fintroducing-aws-lambda-destinations\u002F\">Lambda Destination \u003C\u002Fa> to send the message and configure the channel name in CDK or CloudFormation to make the application topology explicit.\u003C\u002Fp>\n\u003Cp>See the CDK implementation in Typescript on \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Faws-samples\u002Faws-refactoring-to-serverless\u002Ftree\u002Fmain\u002Fimplementation\u002Fextract-send-message\">GitHub\u003C\u002Fa>.\u003C\u002Fp>\n\u003Ch2>Considerations\u003C\u002Fh2>\n\u003Ch4>Advantages\u003C\u002Fh4>\n\u003Cul>\n\u003Cli>Extracting the message sending into a destination makes the application topology explicit and separates composition from application logic\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch4>Applicability\u003C\u002Fh4>\n\u003Cul>\n\u003Cli>\u003Cp>Lambda Destinations are triggered only for \u003Cem>asynchronous\u003C\u002Fem> invocations. Therefore, this refactoring cannot be applied to synchronous invocations, e.g. from the console. If your functions returns a synchronous result, this is now replaced by the message to be sent\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\u003Cp>Lambda Destinations are triggered at the end of the function execution whereas the code could send the message at any place. However, as the consumptionm of the SQS or SNS channel takes place asynchronously, this won&#39;t affect the overall flow.\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\u003Cp>Sending a message from code allows you to set additional attributes like \u003Ca href=\"https:\u002F\u002Fboto3.amazonaws.com\u002Fv1\u002Fdocumentation\u002Fapi\u002Flatest\u002Freference\u002Fservices\u002Fsqs.html#SQS.Client.send_message\">\u003Ccode>MessageDeduplicationId \u003C\u002Fcode>\u003C\u002Fa> or \u003Ccode>DelaySeconds\u003C\u002Fcode>. You cannot set these attributes from a Lambda destination.\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\u003Cp>Destinations that invoke another Lambda function have the option to send the message body only, i.e. the data structure that your function returns (via the \u003Ccode>responseOnly\u003C\u002Fcode> option in \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fcdk\u002Fapi\u002Fv2\u002Fdocs\u002Faws-cdk-lib.aws_lambda_destinations.LambdaDestinationOptions.html\">\u003Ccode>LambdaDestinationOptions\u003C\u002Fcode>\u003C\u002Fa>). No such option exists for destinations that send messages to SQS or SNS. The message therefore includes the event wrapper and the actual data structure inside the \u003Ccode>detail\u002FresponsePayload\u003C\u002Fcode> element. This change will affect downstream event consumers. To maintain the same message format without the wrapper, you would need to direct the destination to an EventBridge, which filters the message content and then passes it onto the SQS chanel or SNS topic. An example of this is shown in the \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fspac3lord\u002Feip\u002Fblob\u002Fmaster\u002FLoanBroker\u002FAwsStepFunctions\u002FPubSub\u002FLoanBrokerPubSub.yml\">Loan Broker CDK Example\u003C\u002Fa>\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch2>Related Refactorings\u003C\u002Fh2>\n\u003Cul>\n\u003Cli>\u003Ca href=\".\u002Fextract_message_filter\">Extract Message Filter\u003C\u002Fa>\u003C\u002Fli>\n\u003C\u002Ful>\n",{"title":6},"Extract Send Message","\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fguides\u002Frefactoring-serverless\u002Fassets\u002Fimages\u002Fmessage-send.png\"\u002F>\n\n## Description\n\nWhen Lambda function code sends a message to a SQS or SNS channel directly, the application's topology is hidden inside the function code. To figure out how messages move, you have to look at the code.\n\nAn example might look as follows:\n\n```py\nqueue_name = os.environ['SQS_NAME']\nsqs = boto3.resource('sqs')\nqueue = sqs.get_queue_by_name(QueueName=queue_name)\n\ndef handler(event, context):\n   ...\n  response = queue.send_message(MessageBody='world')\n```\n\n## Solution\n\nUse [Lambda Destination ](https:\u002F\u002Faws.amazon.com\u002Fblogs\u002Fcompute\u002Fintroducing-aws-lambda-destinations\u002F) to send the message and configure the channel name in CDK or CloudFormation to make the application topology explicit.\n\nSee the CDK implementation in Typescript on [GitHub](https:\u002F\u002Fgithub.com\u002Faws-samples\u002Faws-refactoring-to-serverless\u002Ftree\u002Fmain\u002Fimplementation\u002Fextract-send-message).\n\n## Considerations \n\n#### Advantages\n* Extracting the message sending into a destination makes the application topology explicit and separates composition from application logic\n\n#### Applicability\n\n* Lambda Destinations are triggered only for *asynchronous* invocations. Therefore, this refactoring cannot be applied to synchronous invocations, e.g. from the console. If your functions returns a synchronous result, this is now replaced by the message to be sent\n\n* Lambda Destinations are triggered at the end of the function execution whereas the code could send the message at any place. However, as the consumptionm of the SQS or SNS channel takes place asynchronously, this won't affect the overall flow.\n\n* Sending a message from code allows you to set additional attributes like [`MessageDeduplicationId `](https:\u002F\u002Fboto3.amazonaws.com\u002Fv1\u002Fdocumentation\u002Fapi\u002Flatest\u002Freference\u002Fservices\u002Fsqs.html#SQS.Client.send_message) or `DelaySeconds`. You cannot set these attributes from a Lambda destination.\n\n* Destinations that invoke another Lambda function have the option to send the message body only, i.e. the data structure that your function returns (via the `responseOnly` option in [`LambdaDestinationOptions`](https:\u002F\u002Fdocs.aws.amazon.com\u002Fcdk\u002Fapi\u002Fv2\u002Fdocs\u002Faws-cdk-lib.aws_lambda_destinations.LambdaDestinationOptions.html)). No such option exists for destinations that send messages to SQS or SNS. The message therefore includes the event wrapper and the actual data structure inside the `detail\u002FresponsePayload` element. This change will affect downstream event consumers. To maintain the same message format without the wrapper, you would need to direct the destination to an EventBridge, which filters the message content and then passes it onto the SQS chanel or SNS topic. An example of this is shown in the [Loan Broker CDK Example](https:\u002F\u002Fgithub.com\u002Fspac3lord\u002Feip\u002Fblob\u002Fmaster\u002FLoanBroker\u002FAwsStepFunctions\u002FPubSub\u002FLoanBrokerPubSub.yml)\n\n\n## Related Refactorings\n- [Extract Message Filter](.\u002Fextract_message_filter)",[9,10,11],"content\u002Fcontributors\u002Fgregor-hohpe.json","content\u002Fcontributors\u002Fsindhu-pillai.json","content\u002Fcontributors\u002Fbhuvan-annamreddi.json","LIST","Refactoring to Serverless","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fextract_send_message","2 min",[17,42],{"title":18,"content":19},"Introduction",[20],{"title":13,"order":21,"callout":22,"resources":26,"time":38,"path":39,"id":40,"link":41},1,{"title":23,"description":24,"link":25},"Watch the video","This session shows how you can refactor an existing serverless application to fully utilize AWS managed services and make service dependencies explicit, resulting in cleaner and more robust code.","https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=F7wxgWaccHs",[27,31,34],{"link":28,"text":29,"type":30},"https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=ttJAIQf7cTw","Building modern cloud applications? Think integration!","video",{"link":32,"text":33,"type":30},"https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=Ud9h1hJgoKk","Modern cloud applications - do they lock you in?",{"link":35,"text":36,"type":37},"https:\u002F\u002Fcdkpatterns.com\u002Fpatterns\u002F","CDK Patterns Collection","website","3 min","introduction","introduction.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fintroduction",{"title":43,"content":44},"Refactorings",[45,48,53,58,64,69,74],{"title":6,"time":15,"path":46,"id":47,"link":14},"extract_send_message","extract_send_message.md",{"title":49,"time":15,"path":50,"id":51,"link":52},"Extract function invocation","extract_function_invocation","extract_function_invocation.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fextract_function_invocation",{"title":54,"time":15,"path":55,"id":56,"link":57},"Replace Lambda with Service Integration","service_integration","service_integration.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fservice_integration",{"title":59,"time":60,"path":61,"id":62,"link":63},"Direct database access","1 min","direct_database_access","direct_database_access.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fdirect_database_access",{"title":65,"time":60,"path":66,"id":67,"link":68},"Replace Polling with Callback","replace_polling_with_callback","replace_polling_with_callback.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Freplace_polling_with_callback",{"title":70,"time":15,"path":71,"id":72,"link":73},"Send Message via Pipes","send_message_via_pipes","send_message_via_pipes.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fsend_message_via_pipes",{"title":75,"authors":76,"time":38,"path":78,"id":79,"link":80},"Convert Choreography to Orchestration",[77],"Bhuvan Annamreddi","choreography-to-orchestration","choreography-to-orchestration.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fchoreography-to-orchestration",1792578866224]