<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sebastian Schlecht</title>
    <description>The latest articles on DEV Community by Sebastian Schlecht (@sebastianschlecht).</description>
    <link>https://dev.to/sebastianschlecht</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F475558%2F4e25853d-77f1-437c-a6bd-cba1304f0aee.png</url>
      <title>DEV Community: Sebastian Schlecht</title>
      <link>https://dev.to/sebastianschlecht</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sebastianschlecht"/>
    <language>en</language>
    <item>
      <title>Scheduling automated deletion of AWS Cloudformation stacks
</title>
      <dc:creator>Sebastian Schlecht</dc:creator>
      <pubDate>Sat, 23 Oct 2021 08:30:42 +0000</pubDate>
      <link>https://dev.to/sebastianschlecht/scheduling-automated-deletion-of-aws-cloudformation-stacks-8oi</link>
      <guid>https://dev.to/sebastianschlecht/scheduling-automated-deletion-of-aws-cloudformation-stacks-8oi</guid>
      <description>&lt;p&gt;&lt;em&gt;Article preview: the full article can be found &lt;a href="https://medium.com/capmo-stories/scheduling-automated-deletion-of-aws-cloudformation-stacks-bffd58ec4cd7"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When developers within a team are working on a feature, they are often faced with the situation that each developer is trying to work on a certain aspect of the feature that requires a different version of the infrastructure than we’ve deployed via a Cloudformation(CF) stack, be it an ECS(Elastic Container Service) cluster with multiple task definitions, or when using any other service provided by AWS.&lt;/p&gt;

&lt;p&gt;We at Capmo also ran into a similar situation within our teams. The challenge was not the multiple versions of our infrastructure being deployed: It was how were we going to manage the clean up of the Cloudformation stacks once their work is done. That’s when the idea of auto-deleting the CF stacks came up. After doing some research on our side, we found out that the benefit of owning a piece of infrastructure stack for testing a feature makes the process more agile, flexible and reduces any dependency that might arise when subsequent infrastructure deployments take place, overwriting the previous version of the deployment.&lt;/p&gt;

&lt;p&gt;Since our infrastructure deployments are taken care of by CDK, we wanted to have a solution in place which we can integrate with our existing Cloudformation stacks without any hassle. So we decided to package our solution that can be used in all our microservices if needed. And also with the possibility of making it open-source at some point in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automating deletion of Cloudformation stacks
&lt;/h3&gt;

&lt;p&gt;Now that we knew what we wanted, the question was how to build the solution. We would need a trigger that would initiate the auto-deletion of the Cloudformation stack. This can be achieved with the help of an event, for which we used an EventBridge rule. EventBridge rules have the ability to match an event pattern and trigger its targets. The target of choice was a Lambda function that would initiate the auto-deletion sequence. A serverless solution was a clear answer to this as it makes write and deploy easier, with no effort of managing the deployed resources. All of this was deployed within the same Cloudformation stack that we wanted to delete. Now let’s dig deeper into how we implemented this.&lt;/p&gt;

&lt;p&gt;To read on, the full article is published on our &lt;a href="https://medium.com/capmo-stories/scheduling-automated-deletion-of-aws-cloudformation-stacks-bffd58ec4cd7"&gt;engineering blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>automation</category>
      <category>typescript</category>
      <category>cdk</category>
    </item>
    <item>
      <title>Serverless Nest.js micro-services &amp; integrations without HTTP
</title>
      <dc:creator>Sebastian Schlecht</dc:creator>
      <pubDate>Sat, 09 Jan 2021 12:19:19 +0000</pubDate>
      <link>https://dev.to/sebastianschlecht/serverless-nest-js-micro-services-integrations-without-http-ikk</link>
      <guid>https://dev.to/sebastianschlecht/serverless-nest-js-micro-services-integrations-without-http-ikk</guid>
      <description>&lt;p&gt;Originally posted on &lt;a href="https://medium.com/capmo-stories/serverless-nest-js-micro-services-integrations-without-http-42f453236b39"&gt;our engineering blog&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Serverless Nest.js micro-services &amp;amp; integrations without HTTP
&lt;/h1&gt;

&lt;p&gt;At &lt;a href="https://www.capmo.de/"&gt;Capmo&lt;/a&gt;, we are currently evaluating to move our entire backend stack to &lt;a href="https://nestjs.com/"&gt;Nest.js&lt;/a&gt; because of its many benefits (which I won’t mention in this blog post as there are plenty of others on that matter).&lt;/p&gt;

&lt;p&gt;Since we already have a legacy application running in production, we unfortunately are not looking at a green field here but need to move building blocks piece by piece — among others we have some serverless micro-services that are built using AWS Lambda which we also want to migrate and extend to keep a consistent structure and experience across our backend stack. Those services mainly use native AWS integrations via API Gateway, S3 events or SQS triggers. While there is material on the Internet on how to integrate Nest.js with API Gateway (e.g. &lt;a href="https://medium.com/limehome-engineering/a-nestjs-microservice-with-serverless-framework-3435fac7e0bd"&gt;here&lt;/a&gt;), most approaches rely on a proxy integration between API Gateway and a (cached) express.js or fastify instance. There are ways to interface with other cloud-native integrations using Nest.js &lt;a href="https://docs.nestjs.com/microservices/basics"&gt;micro-services&lt;/a&gt;, but many transports don’t come out of the box and we found that wiring up new ones is quite cumbersome and thus were looking for a more straightforward approach that might play nicely with AWS native integrations for Lambda.&lt;/p&gt;

&lt;h2&gt;
  
  
  Standalone applications to the rescue
&lt;/h2&gt;

&lt;p&gt;Nest.js already provides &lt;a href="https://docs.nestjs.com/standalone-applications"&gt;a guide&lt;/a&gt; on how to use the framework in non-http contexts like cron-jobs. In such a scenario, one can benefit from its structure, modularity and other goodies without the overhead of an HTTP server.&lt;/p&gt;

&lt;p&gt;When looking at native Lambda integrations however, one might still want to preserve the concept of &lt;em&gt;a request scope.&lt;/em&gt; This can be useful for use-cases like preserving native AWS &lt;em&gt;requestIds&lt;/em&gt; throughout the lifecycle of an invocation for logging, or pass authorisation headers down to app modules. With standalone apps as described in the guide however, one has only access to &lt;a href="https://docs.nestjs.com/fundamentals/injection-scopes"&gt;singleton scoped providers&lt;/a&gt;. What if, for example, we want to share a &lt;em&gt;requestId&lt;/em&gt; or &lt;em&gt;userId&lt;/em&gt; among all providers within a single invocation / execution context to make logging more transparent, handle authorisation/tenancy, etc.?&lt;/p&gt;

&lt;p&gt;In such a case, we can leverage Nest.js’ dependency injection system to create a request-scoped sub-tree for us, which we can use during the life-cycle of a Lambda function invocation. We first instantiate and cache a standalone Nest.js application context, and then create a scoped sub-tree for each invocation:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Subsequently, we can now use &lt;a href="https://docs.nestjs.com/fundamentals/injection-scopes#request-provider"&gt;request scoped providers&lt;/a&gt; throughout our application and inject the { context } object from line 44 through the &lt;code&gt;@Inject&lt;/code&gt; decorator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @Injectable({ scope: Scope.REQUEST })
    export class CatsService {
      constructor(@Inject(REQUEST) private requestContext: Context) {}
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;whereas requestContext in this example is the injected Lambda execution context, but can be arbitrary — for example we can extract headers from the invoking request or event-payload like a &lt;em&gt;userId&lt;/em&gt; or &lt;em&gt;tenantId&lt;/em&gt; which we can make available to all services through DI.&lt;/p&gt;

&lt;h3&gt;
  
  
  A note on memory-leaks
&lt;/h3&gt;

&lt;p&gt;The attentive reader now might think — what about memory-leaks? We are not cleaning up the instantiated sub-tree in the example above and cache the application context over multiple function invocations?&lt;/p&gt;

&lt;p&gt;Luckily, Nest.js uses &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"&gt;WeakMap&lt;/a&gt; internally, hence as soon as the reference to contextId (which is an object!) is being garbage-collected, the request-scoped sub-tree will also be garbage-collected subsequently. Details can be found &lt;a href="https://github.com/nestjs/nest/blob/master/packages/core/helpers/context-id-factory.ts#L5"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The example above illustrates how to wire up a Nest.js app without HTTP overhead directly to Lambda integrations and cache the underlying dependency injection contexts. We plan to use this internally through AWS CDK until Nest.js’ serverless core matures in order to progress our application landscape.&lt;/p&gt;

&lt;h3&gt;
  
  
  And what’s the downside here?
&lt;/h3&gt;

&lt;p&gt;The downside here is that we are losing quite some framework mechanics when invoking services directly, like for example pipes, guards or exception filters. This is not a problem when going the HTTP proxy way but there is definitely room for improvement when going the serverless way without using HTTP or the Nest.js style micro-services pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; We are looking for kick-ass software engineers to join our team to shake up the construction industry. If we caught your interest, feel free to get in touch &lt;a href="https://capmo-jobs.personio.de/?language=en"&gt;here&lt;/a&gt; or directly via &lt;a href="https://github.com/sebastian-schlecht"&gt;Github&lt;/a&gt;/&lt;a href="https://www.linkedin.com/in/sebastian-schlecht-88445999/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>typescript</category>
      <category>nestjs</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
