[{"data":1,"prerenderedAt":87},["ShallowReactive",2],{"content-guides-refactoring-serverless-introduction":3},{"markdown":4,"frontMatterAttributes":5,"bodyRaw":24,"contributorPaths":25,"menuType":29,"contentName":6,"slug":30,"readingTime":31,"menu":32,"nextDocItem":47,"previousDocItem":86},"\u003Cp>Serverless is more than just a run-time for your code. It&#39;s a suite of cloud provider managed services to help fine-grained, event-driven applications best utilize the cloud. Automation is an integral part of building serverless applications and often the same functionality  can be achieved from application code or through a platform feature. Take sending a message from a Lambda function to SQS as an example. You could do this by writing code, utilizing a client library like \u003Ccode>boto3\u003C\u002Fcode> or you could configure a \u003Ca href=\"https:\u002F\u002Faws.amazon.com\u002Fblogs\u002Fcompute\u002Fintroducing-aws-lambda-destinations\u002F\">Lambda Destination\u003C\u002Fa>. The latter approach is preferred because it utilizes the cloud platform and separates application topology (which component talks to which others) from the application logic. Interestingly, with \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fcdk\u002Fv2\u002Fguide\u002Fhome.html\">AWS CDK\u003C\u002Fa> you can configure the Lambda destination using the same programming language that you used to write your application code. \u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>You can improve the design of your serverless application by replacing application code with automation code, while using the same programming language.\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Cp>That&#39;s what we call \u003Cem>Refactoring to Serverless\u003C\u002Fem>, based on Martin Fowler&#39;s definition of Refactoring as a popular coding technique to improve your application design:\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>A disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Cp>In our case, the refactoring moves code from the application to CDK automation. Other refactorings might improve the overall application design. Let&#39;s look at the example in more detail.\u003C\u002Fp>\n\u003Ch2>An Example: Extract Send Message\u003C\u002Fh2>\n\u003Cp>Lambda code often performs tasks that could be more easily and more reliably performed by using a platform feature. For example, the following code was taken from a public GitHub example:\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\u003Cp>This code isn&#39;t just unnecessary, it also hides the application&#39;s topology in application code and environment variables. You would not know that this function connects to the SQS queue unless you inspect the source code and the value of the environment variable. \u003C\u002Fp>\n\u003Cp>The refactoring replaces this function code with a Lambda destination. By extracting the code, the dependency between the functions and the SQS channel becomes explicit in the automation code:\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-js\">new lambda.Function(this, config.bankName, {\n    runtime: lambda.Runtime.NODEJS_14_X,\n    functionName: &quot;MyFunction&quot;,\n    onSuccess: new destinations.SqsDestination(sqsChannel) } )\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>You could now, for example, use your IDE to search for all references to \u003Ccode>sqsChannel\u003C\u002Fcode> to more easily understand dependencies. You can find the full description of this refactoring at \u003Ca href=\"extract_send_message\">Extract Send Message\u003C\u002Fa>\u003C\u002Fp>\n\u003Ch2>Why refactor to serverless?\u003C\u002Fh2>\n\u003Cp>There are multiple benefits to refactoring your solution to serverless:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>The refactored code better \u003Cem>leverages platform capabilities\u003C\u002Fem>. Automation features don&#39;t need code library upgrades, testing, or debugging. They also generally scale better and are more robust than application code.\u003C\u002Fli>\n\u003Cli>AWS continually adds \u003Cem>new services and capabilities\u003C\u002Fem>, so you might find that you had written application code for a function that can now be done using a service capability. Time to refactor!\u003C\u002Fli>\n\u003Cli>Occasionally your \u003Cem>application&#39;s needs outgrow the capability\u003C\u002Fem> of one service. For example, EventBridge has many features to filter incoming messages but if your application&#39;s needs evolve to require a complex rule across multiple fields, you might need to refactor from EventBridge to a custom lambda function.\u003C\u002Fli>\n\u003Cli>Refactoring to serverless can \u003Cem>improve your run-time characteristics\u003C\u002Fem> and reduce cost. For example, if you can replace polling for results with a callback or a wait state, you reduce latency and cost.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>Because of these benefits, refactoring should be an integral part of serverless development.\u003C\u002Fp>\n",{"title":6,"order":7,"callout":8,"resources":12},"Refactoring to Serverless",1,{"title":9,"description":10,"link":11},"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",[13,17,20],{"link":14,"text":15,"type":16},"https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=ttJAIQf7cTw","Building modern cloud applications? Think integration!","video",{"link":18,"text":19,"type":16},"https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=Ud9h1hJgoKk","Modern cloud applications - do they lock you in?",{"link":21,"text":22,"type":23},"https:\u002F\u002Fcdkpatterns.com\u002Fpatterns\u002F","CDK Patterns Collection","website","Serverless is more than just a run-time for your code. It's a suite of cloud provider managed services to help fine-grained, event-driven applications best utilize the cloud. Automation is an integral part of building serverless applications and often the same functionality  can be achieved from application code or through a platform feature. Take sending a message from a Lambda function to SQS as an example. You could do this by writing code, utilizing a client library like `boto3` or you could configure a [Lambda Destination](https:\u002F\u002Faws.amazon.com\u002Fblogs\u002Fcompute\u002Fintroducing-aws-lambda-destinations\u002F). The latter approach is preferred because it utilizes the cloud platform and separates application topology (which component talks to which others) from the application logic. Interestingly, with [AWS CDK](https:\u002F\u002Fdocs.aws.amazon.com\u002Fcdk\u002Fv2\u002Fguide\u002Fhome.html) you can configure the Lambda destination using the same programming language that you used to write your application code. \n\n> You can improve the design of your serverless application by replacing application code with automation code, while using the same programming language.\n\nThat's what we call *Refactoring to Serverless*, based on Martin Fowler's definition of Refactoring as a popular coding technique to improve your application design:\n\n> A disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior\n\nIn our case, the refactoring moves code from the application to CDK automation. Other refactorings might improve the overall application design. Let's look at the example in more detail.\n\n## An Example: Extract Send Message\n\nLambda code often performs tasks that could be more easily and more reliably performed by using a platform feature. For example, the following code was taken from a public GitHub example:\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\nThis code isn't just unnecessary, it also hides the application's topology in application code and environment variables. You would not know that this function connects to the SQS queue unless you inspect the source code and the value of the environment variable. \n\nThe refactoring replaces this function code with a Lambda destination. By extracting the code, the dependency between the functions and the SQS channel becomes explicit in the automation code:\n\n```js\nnew lambda.Function(this, config.bankName, {\n    runtime: lambda.Runtime.NODEJS_14_X,\n    functionName: \"MyFunction\",\n    onSuccess: new destinations.SqsDestination(sqsChannel) } )\n```\n\nYou could now, for example, use your IDE to search for all references to `sqsChannel` to more easily understand dependencies. You can find the full description of this refactoring at [Extract Send Message](extract_send_message)\n\n## Why refactor to serverless?\n\nThere are multiple benefits to refactoring your solution to serverless:\n\n* The refactored code better *leverages platform capabilities*. Automation features don't need code library upgrades, testing, or debugging. They also generally scale better and are more robust than application code.\n* AWS continually adds *new services and capabilities*, so you might find that you had written application code for a function that can now be done using a service capability. Time to refactor!\n* Occasionally your *application's needs outgrow the capability* of one service. For example, EventBridge has many features to filter incoming messages but if your application's needs evolve to require a complex rule across multiple fields, you might need to refactor from EventBridge to a custom lambda function.\n* Refactoring to serverless can *improve your run-time characteristics* and reduce cost. For example, if you can replace polling for results with a callback or a wait state, you reduce latency and cost.\n\nBecause of these benefits, refactoring should be an integral part of serverless development.\n",[26,27,28],"content\u002Fcontributors\u002Fgregor-hohpe.json","content\u002Fcontributors\u002Fsindhu-pillai.json","content\u002Fcontributors\u002Fbhuvan-annamreddi.json","LIST","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fintroduction","3 min",[33,44],{"title":34,"content":35},"Introduction",[36],{"title":6,"order":7,"callout":37,"resources":38,"time":31,"path":42,"id":43,"link":30},{"title":9,"description":10,"link":11},[39,40,41],{"link":14,"text":15,"type":16},{"link":18,"text":19,"type":16},{"link":21,"text":22,"type":23},"introduction","introduction.md",{"title":45,"content":46},"Refactorings",[47,53,58,63,69,74,79],{"title":48,"time":49,"path":50,"id":51,"link":52},"Extract Send Message","2 min","extract_send_message","extract_send_message.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fextract_send_message",{"title":54,"time":49,"path":55,"id":56,"link":57},"Extract function invocation","extract_function_invocation","extract_function_invocation.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fextract_function_invocation",{"title":59,"time":49,"path":60,"id":61,"link":62},"Replace Lambda with Service Integration","service_integration","service_integration.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fservice_integration",{"title":64,"time":65,"path":66,"id":67,"link":68},"Direct database access","1 min","direct_database_access","direct_database_access.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fdirect_database_access",{"title":70,"time":65,"path":71,"id":72,"link":73},"Replace Polling with Callback","replace_polling_with_callback","replace_polling_with_callback.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Freplace_polling_with_callback",{"title":75,"time":49,"path":76,"id":77,"link":78},"Send Message via Pipes","send_message_via_pipes","send_message_via_pipes.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fsend_message_via_pipes",{"title":80,"authors":81,"time":31,"path":83,"id":84,"link":85},"Convert Choreography to Orchestration",[82],"Bhuvan Annamreddi","choreography-to-orchestration","choreography-to-orchestration.md","\u002Fcontent\u002Fguides\u002Frefactoring-serverless\u002Fchoreography-to-orchestration",null,1790418892267]