Photo by Shahadat Rahman on Unsplash
Getting Started | Infrastructure as Code(IaC) with examples.
Understanding IaC in Detail.
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
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.
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: registry.terraform.io/providers/hashicorp/a..
AWS Cloudformation
AWSTemplateFormatVersion: "2010-09-09"
Resources:
S3Bucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
BucketName: DOC-EXAMPLE-BUCKET
example:docs.aws.amazon.com/AWSCloudFormation/lates..
AWS CDK
new Bucket(scope, 'Bucket', {
bucketName : 'DOC-EXAMPLE-BUCKET'
removalPolicy: RemovalPolicy.RETAIN,
});
example:docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk..
Serverless Framework
resources:
Resources:
ModuslandBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: DOC-EXAMPLE-BUCKET
credits: moduscreate.com/blog/upload-files-to-aws-s3..
Thanks for reading. I hope this story was helpful. If you are interested, check out my other articles.
you can also visit shubhamdeshmukh.com