<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="https://g7an.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://g7an.github.io/" rel="alternate" type="text/html" /><updated>2022-06-27T05:44:17+00:00</updated><id>https://g7an.github.io/feed.xml</id><title type="html">Gloria Tan</title><subtitle>Material theme based on &lt;a href=&quot;http://materializecss.com&quot;&gt;Materialize.css&lt;/a&gt; for jekyll sites
</subtitle><entry><title type="html">Infrastructure As Code and How to do it on AWS</title><link href="https://g7an.github.io/2022/06/26/infrastructure-as-code-and-how-to-do-it-on-aws" rel="alternate" type="text/html" title="Infrastructure As Code and How to do it on AWS" /><published>2022-06-26T00:00:00+00:00</published><updated>2022-06-26T00:00:00+00:00</updated><id>https://g7an.github.io/2022/06/26/infrastructure-as-code-and-how-to-do-it-on-aws</id><content type="html" xml:base="https://g7an.github.io/2022/06/26/infrastructure-as-code-and-how-to-do-it-on-aws">&lt;p&gt; 
One interesting new concept I learnt when I was an intern at AWS is Infrastructure as Code (IaC). 
IaC refers to provision, management and deployment of cloud infrastructure, i.e. the cloud application resources, through code. 
This would replace the old practice of manually managing cloud resources (e.g. using AWS console) or writing scripts (e.g. using AWS CLI).
&lt;/p&gt;
&lt;p&gt;
IaC provides the smoothness of managing cloud infrastructure by writing code, and managing the code using the same way as we do to application codes. 
You can write unit and integration tests to test the code defining your infra stack to make it more robust, and you can also do code reviews and use source version control systems like Git to manage the code.
&lt;/p&gt;
&lt;p&gt; 
&lt;b&gt;The benefit of using IaC is that it will make the whole process of managing cloud infrastructure to be repeatable, reliable and consistent.&lt;/b&gt;
&lt;/p&gt;

&lt;h5 id=&quot;how-can-we-do-iac-using-aws&quot;&gt;How can we do IaC using AWS&lt;/h5&gt;

&lt;p&gt; 
If you are using AWS, one possible tool you can use is AWS Cloud Development Kit (AWS CDK).  
&lt;/p&gt;

&lt;p&gt; 
AWS CDK is an open-source software development framework that defines several high-level concepts like constructs and stacks that could be used to build infrastructure stacks. Once the infrastructure stack is defined through code, a template will be generated by AWS CloudFormation which is used for deployment to the cloud. The template could be re-used so that the same infrastructure could be generated repeatedly, in a reliable and consistent way.
&lt;/p&gt;

&lt;h5 id=&quot;high-level-concepts-of-aws-cdk&quot;&gt;High-level concepts of AWS CDK&lt;/h5&gt;
&lt;p&gt;
Diagram below shows how infrastructure stacks are generated using AWS CDK:  
&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/AppStacks.png&quot; alt=&quot;High-level concepts in AWS CDK&quot; /&gt;&lt;/p&gt;

&lt;p&gt;
  In the diagram, you can see the smallest building block of an App is Construct. Constructs are re-usable cloud components that could either represent a single AWS resource (e.g. S3, Lambda), or it could be a higher-level abstraction of multiple AWS resources. To initialize a stack, you have to define 3 parameters, namely scope, id and props. An example provided by AWS developer guide could be seen as follow:
&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-TypeScript&quot;&gt;import { App, Stack, StackProps } from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

class HelloCdkStack extends Stack {
  constructor(scope: App, id: string, props?: StackProps) {
    super(scope, id, props);a

    new s3.Bucket(this, 'MyFirstBucket', {
      versioned: true
    }); // construct
  }
}

const app = new App();
new HelloCdkStack(app, &quot;HelloCdkStack&quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As can be seen in this example, the construct in this case is an S3 bucket. 
The scope of S3 bucket is this, referring to the current object (&lt;code&gt;new HelloCdkStack()&lt;/code&gt;). 
The id of a construct is a string that uniquely identifies the construct, in this case ‘MyFirstBucket’. 
The props is a set of key-value pairs that defines the constructs’ initial configuration. 
This is optional, since some defaults are pre-defined. But if you want to specify this, check out the API reference for AWS CDK to take a closer look (all the props available for S3 buckets could be found (all the props available for S3 buckets could be found &lt;a href=&quot;https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-s3.Bucket.html#construct-props&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;
Constructs must be defined within the scope of a stack. Stacks are the unit of deployment that AWS CloudFormation instantiates (implemented through CloudFormation stacks). Stacks contain sets or sets of AWS resources that are created and managed as a single unit. An AWS CDK App can have one or more stacks.  
&lt;/p&gt;
&lt;p&gt;
Stacks must be defined within the scope of an App. An App could have one or more stacks. The App construct represents the entire CDK app, which serves as the root of the construct tree.  
&lt;/p&gt;

&lt;h5 id=&quot;how-to-generate-an-infra-stack-using-aws-cdk&quot;&gt;How to generate an Infra Stack using AWS CDK&lt;/h5&gt;
&lt;p&gt;
 After talking about the concept used in CDK code, the next question would be how to generate an infra stack using AWS CDK? We can use the code snippet above to see how to generate a S3 bucket using AWS CDK. Before diving deeper, please make sure you have an AWS account. 
&lt;/p&gt;
&lt;p&gt;
 To get started, we first need to install AWS CDK. This can be done by &lt;code&gt;npm install -g aws-cdk&lt;/code&gt; (a &lt;code&gt;sudo&lt;/code&gt; is needed for my case). 
  You can check if you have got CDK CLI installed by using &lt;code&gt;cdk --version&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
 The first thing we need to do to create an AWS CDK app is to initialize it in a directory with the language we intend to use (in this case TypeScript). Currently the most commonly used language to build CDK applications is TypeScript. Most documents and examples provided by AWS are also using TypeScript.
 Run the following commands in terminal: &lt;code&gt;mkdir HelloCdk &amp;amp;&amp;amp; cd HelloCdk&lt;/code&gt; and &lt;code&gt;cdk init --language typescript&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
 &lt;code&gt;cdk init&lt;/code&gt; is a CDK CLI command that could create a new CDK project in the current directory using a template (more info could be found via &lt;code&gt;cdk init --help&lt;/code&gt;). 
 &lt;/p&gt;
&lt;p&gt;
 Then, go into the &lt;code&gt;hello_cdk_stack.ts&lt;/code&gt; under lib directory and copy the source code into the file:
  &lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-TypeScript&quot;&gt;import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';

export class HelloCdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // The code that defines your stack goes here
    new s3.Bucket(this, 'MyFirstBucket', {
      versioned: true
    }); // construct
  }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;
  You can run &lt;code&gt;npm run watch&lt;/code&gt; inside HelloCdk directory to watch the code updates and check for potential errors.  
&lt;/p&gt;
&lt;p&gt;
 After modifying code, run &lt;code&gt;cdk diff&lt;/code&gt; to take a look at what changes we are about to deploy. 
&lt;/p&gt;
&lt;p&gt;
  Then, let’s synthesize the CloudFormation template by using &lt;code&gt;cdk synth&lt;/code&gt;. The template will be printed out when you run the command.
 &lt;/p&gt;
&lt;p&gt;
 To explain more about what happens in this step, let’s use an analogy of running a compiled language like C++. In this case, CDK CLI acts like the compiler that turns your source code into assembly language (i.e. the machine instructions), and &lt;code&gt;cdk synth&lt;/code&gt; is the command used to generate the “assembly language” - the CloudFormation template.
&lt;/p&gt;
&lt;p&gt;
  After “compiling” the source code, the next step would be to execute the compiled code, in this case is to deploy our infra stack to the cloud. Before doing this, we need to run an extra command, &lt;code&gt;cdk bootstrap&lt;/code&gt;, to bootstrap our cloud environment (i.e. to specify the account and region that the infra stack will be deployed). We can specify the environment in this command, but in our case a default environment will be used (which we have specified in &lt;code&gt;~/.aws/credentials&lt;/code&gt;).
&lt;/p&gt;
&lt;p&gt;
  Finally, let’s deploy our infra stack using &lt;code&gt;cdk deploy&lt;/code&gt; and check it out on AWS console!
 &lt;/p&gt;
&lt;p&gt;
 Go to S3 bucket, we can see the S3 bucket created using cdk code
 We can also see the stack we deployed in CloudFormation. Note that the CDKToolKit stack is created by default and can be re-used by many CDK applications.
 &lt;/p&gt;

&lt;p&gt;
 That’s it, we can now clean up the environment by running &lt;code&gt;cdk destroy&lt;/code&gt; to avoid any unnecessary billing when we are done with the experiment. 
 &lt;/p&gt;

&lt;h4 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h4&gt;
&lt;p&gt;
 In this post, we have talked about the concept of Infrastructure as Code (IaC) and why we should consider using IaC to manage our cloud infrastructures. In a nutshell, IaC makes the provision, management and deployment of our cloud infra in a more repeatable, reliable and consistent manner.
&lt;/p&gt;
&lt;p&gt;
 While I believe all the mainstream cloud service providers have their own service to support IaC, I have used CDK as an example to show how IaC is done at AWS. According to AWS, AWS CDK can provide higher level abstraction (using the construct, stack and app), easy to share and have all the benefits of AWS CloudFormation. 
&lt;/p&gt;
&lt;h4 id=&quot;references&quot;&gt;References&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.redhat.com/en/topics/automation/what-is-infrastructure-as-code-iac&quot;&gt;RedHat intro on IaC&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/cdk/&quot;&gt;AWS CDK page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/cdk/v2/guide/home.html&quot;&gt;AWS CDK developer guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><category term="Cloud" /><category term="IaC" /><category term="AWS" /><summary type="html">One interesting new concept I learnt when I was an intern at AWS is Infrastructure as Code (IaC). IaC refers to provision, management and deployment of cloud infrastructure, i.e. the cloud application resources, through code. This would replace the old practice of manually managing cloud resources (e.g. using AWS console) or writing scripts (e.g. using AWS CLI). IaC provides the smoothness of managing cloud infrastructure by writing code, and managing the code using the same way as we do to application codes. You can write unit and integration tests to test the code defining your infra stack to make it more robust, and you can also do code reviews and use source version control systems like Git to manage the code. The benefit of using IaC is that it will make the whole process of managing cloud infrastructure to be repeatable, reliable and consistent.</summary></entry></feed>