INFRASTRUCTURE AS CODE

Getting Started | Infrastructure as Code(IaC) with examples.

Understanding IaC in Detail.

Shubham Deshmukh
AWS Tip
Published in
3 min readJul 28, 2022

--

Photo by: Nicole De Khors

Overview

Infrastructure is a vital part of software development. It helps us to create and deploy a software application. Infrastructure can be Servers, load balancers, or DB. The current generation of software development have multiple environments and requires us to create different infrastructure for individual environment. This creates complexity and increases the developer's time to create infrastructure for each environment. The traditional approach for deploying an application was to manually create infrastructure which is a very improper way when it comes to solutions like scalability and reliability. That’s why Infrastructure as Code has become an important part of developers day to day activities.

Infrastructure as Code (IaC) is managing and provisioning infrastructure through code instead of manually doing it. With IaC, configuration files are written that contain your infrastructure specifications and details, which makes it easier to create and edit the required configurations. It also ensures that every time you run a particular IaC code it will create the same exact infrastructure.

As IaC is basically a code we can maintain in version control that helps us to track every deployment and to roll back easily if we face any issues.

Important advantages of IaC are as below,

  • Version Controlling: we can keep track of the latest and older infra.
  • No manual Intervention: once configurations are written we won't require to change anything, reducing the chances of creating incorrect infra.
  • Extensive Automation: IaC can be used extensively in creating CI/CD pipelines.
  • Cost reduction: We only create as much needed, reducing redundant infra.
  • Increase in speed of deployments: As there is no manual intervention the deployments are quick and fast
  • Different Flavors: There are multiple ways to write an IaC code.

Declarative vs imperative Infrastructure as Code

  • Declarative IaC: We need to only set the desired state configuration file and the IaC tool will take care of the creation of all the Infra.
  • imperative IaC: We Need to create steps/commands and those will be executed one by one to create the whole infra.

Some IaC tools use a declarative approach and will automatically provision the desired infrastructure as per the provided configurations. If you make alterations to the configuration, the IaC tool will apply those changes for you. An imperative tool will require you to figure out how those changes should be applied.

These are a few popular choices for IaC:

Below are examples of different tools to create an S3 bucket in AWS.

  1. Terraform
resource "aws_s3_bucket" "b" {
bucket = "DOC-EXAMPLE-BUCKET"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
resource "aws_s3_bucket_acl" "example" {
bucket = aws_s3_bucket.b.id
acl = "private"
}

example: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket

2. AWS Cloudformation

AWSTemplateFormatVersion: "2010-09-09"
Resources:
S3Bucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
BucketName: DOC-EXAMPLE-BUCKET

example:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html#aws-properties-s3-bucket--examples

3. AWS CDK

new Bucket(scope, 'Bucket', {
bucketName : 'DOC-EXAMPLE-BUCKET'
removalPolicy: RemovalPolicy.RETAIN,
});

example:https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.Bucket.html

4. Serverless Framework

resources:
Resources:
ModuslandBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: DOC-EXAMPLE-BUCKET

credits: https://moduscreate.com/blog/upload-files-to-aws-s3-using-a-serverless-framework/

Thanks for reading. I hope this story was helpful. If you are interested,

check out my other articles.

you can also visit shubhamdeshmukh.com

--

--

Writer for

Software Developer | Cloud infrastructure | IaC | AWS | DevOps | Hit the follow button for Cloud-related content 😄