<?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: Nsikan </title>
    <description>The latest articles on DEV Community by Nsikan  (@noce2).</description>
    <link>https://dev.to/noce2</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%2F541841%2Fe7d832aa-763c-402a-8f0d-072499c5423f.jpg</url>
      <title>DEV Community: Nsikan </title>
      <link>https://dev.to/noce2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/noce2"/>
    <language>en</language>
    <item>
      <title>CloudFormation Custom Resources - Why and When</title>
      <dc:creator>Nsikan </dc:creator>
      <pubDate>Tue, 24 Aug 2021 07:25:57 +0000</pubDate>
      <link>https://dev.to/noce2/cloudformation-custom-resources-why-and-when-15jm</link>
      <guid>https://dev.to/noce2/cloudformation-custom-resources-why-and-when-15jm</guid>
      <description>&lt;p&gt;All good solutions start with a problem :). Imagine you'd like to deploy an application as a CloudFormation stack and part of its infrastructure includes s3 for storage, some contents of which you'd like to be replicated to another region. The storage part of the application might look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wk5fVapD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ww6wactf6vgse5gkig2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wk5fVapD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ww6wactf6vgse5gkig2p.png" alt="Picture of 2 AWS S3 buckets with replication between them."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The question arises of how to deploy this via infrastructure-as-code, as close to a 1-click/push button deploy as possible. One might try the below steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Provision the bucket and the necessary KMS keys in the secondary region&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Extract their ARNs from the CF output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run another CF deploy into the primary region providing the above ARNs as parameters.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whilst this approach would get you the end result, it doesn’t easily let you build robustness into the system and although logically your resources are one unit, you haven’t deployed them as such.&lt;/p&gt;

&lt;p&gt;How do we get round these issues? Ultimately we want to deploy all the resources as part of one logical Cloudformation template but resources in multiple regions can only be deployed as &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-concepts.html"&gt;StackSets&lt;/a&gt; using Cloudformation. StackSets don’t help us in this circumstance because they deploy resources in a cookie-cutter fashion across the regions, i.e. everything is the same and there cannot be dependencies between regions (which is what we need here).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GcGtRl4O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/images/stack_sets_operations_stacks_sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GcGtRl4O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/images/stack_sets_operations_stacks_sv.png" alt="Picture of AWS StackSets - illustration by AWS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter in &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html"&gt;Lambda-backed custom resources&lt;/a&gt;! These basically allow you execute any code as part of the Cloudformation lifecycle of a custom type of resource that you define. Extremely powerful. I’ll refer to other great articles like &lt;a href="https://www.alexdebrie.com/posts/cloudformation-custom-resources/"&gt;this&lt;/a&gt; for further detail. What does our application from before look like with this in place?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gBY0AZcW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11mpj21qt6o4yjczkbbd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gBY0AZcW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11mpj21qt6o4yjczkbbd.png" alt="Previous s3 replication architecture with a Lambda function as a Custom Resource Provider"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How about we stare at some code then? In the interests of keeping this post short, I'll focus on just the S3 part of the custom resource provision.&lt;/p&gt;

&lt;p&gt;In the long-winded attempt at doing this, deploying then copying again, this is what out backup bucket could have looked like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  MyBackupBucket:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: "my-super-unique-backup-thingy"
      AccessControl: "Private"
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: "AES256"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, using a custom resource provider, to deploy this it would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  MyBackupBucket:
    Type: Custom::CrossRegionReplicationBucket
    Properties:
      ServiceToken: !GetAtt CRRDestinationBucketProvider.Arn
      CRRDestinationBucketName: "my-super-unique-backup-thingy"
      CRRDestinationBucketRegion: "YOUR_DESIRED_BACKUP_REGION"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, so the &lt;code&gt;Type&lt;/code&gt; attribute is self-explanatory, but what's the &lt;code&gt;ServiceToken&lt;/code&gt;? Well, this is the &lt;code&gt;Arn&lt;/code&gt; of the Lambda that will actually deploy the desired custom resource, in this case a bucket in another region, for us. Unless you're planning to use a lamba/other provider that is shared by lots of stacks outside of yours, I'd recommend deploying this as part of the same template so we'd have some thing like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  CRRDestinationBucketProvider:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: nodejs12.x
      Role: #some lambda role with permissions for s3 operations
      Handler: index.handler
      Code:
        ZipFile: |

          const {       
               S3Client,
               CreateBucketCommand,
               DeleteBucketCommand,
          } = require('@aws-sdk/client-s3')

          var response = require('cfn-response')

          exports.handler = async function(event, context) {
              console.log("REQUEST RECEIVED:\n" + JSON.stringify(event))

              const s3client = new S3Client({region: event.ResourceProperties.CRRDestinationBucketRegion});

              if (event.RequestType == "Delete") {
                  const request = new DeleteBucketCommand({
Bucket:event.ResourceProperties.CRRDestinationBucketName
                  });

                  const deleteResponseData = await s3client.send(request);
                  response.send(event, context, "SUCCESS")
                  return
              }

              if (event.RequestType == "Create") {
                  const request = new CreateBucketCommand({
    Bucket:event.ResourceProperties.CRRDestinationBucketName,
    CreateBucketConfiguration: {
          LocationConstraint: 
          event.ResourceProperties.CRRDestinationBucketRegion,
    },
                  });

                  const createResponseData = await s3client.send(request);
                  response.send(event, context, "SUCCESS",  {BucketArn: `arn:aws:s3:::${event.ResourceProperties.CRRDestinationBucketName}`}, event.ResourceProperties.CRRDestinationBucketName)
                  return
              } 

              // include code to handle `Update` events here

          }
      Description: Custom resource provider for s3 buckets in other regions.
      TracingConfig:
        Mode: Active
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is neither perfect not prod-ready but it does give a good idea of what's necessary to start using custom resources to solve a common type of problem faced when building resilient architectures compliant with certain industry standards (common in areas like Healthcare or Finance).&lt;/p&gt;

&lt;p&gt;From the above example, you can see the bucket name is sent back as the physical resource id and the Arn is sent as part of the CloudFormation event response data. Setting the Arn as part of the response data allows this, and any other attributes you'd like, to be retrieved for an instance of your custom resource by using the &lt;code&gt;!GetAtt&lt;/code&gt; command. In this case, we'd have something like &lt;code&gt;!GetAtt MyBackupBucket.BucketArn&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Finally it's worth noting that there are other solutions to this problem outside CloudFormation, for example Terraform. However, if you need a simple solution that doesn't involve adding more tooling to your tech stack, CloudFormation Custom Resources might just be the thing for you.&lt;/p&gt;

&lt;p&gt;Have fun.&lt;/p&gt;

&lt;p&gt;P.S. If you're thinking developing and maintaining Custom Resources is challenging/you would like to reuse great resources that others have developed, AWS have now developed and released a &lt;a href="https://aws.amazon.com/es/blogs/aws/introducing-a-public-registry-for-aws-cloudformation/"&gt;Cloudformation Registry&lt;/a&gt;. This will allow you to reuse Custom Resources developed by first and third-parties in the community. Very cool!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>cloudformation</category>
      <category>infrastructureascode</category>
    </item>
  </channel>
</rss>
