Skip to content

  • Home
  • Core Java
    • Write Your Own Immutable Class In Java
    • Write Your Own Singleton Class In java
    • Java Concurrent Package
    • Java Stream Revisited
    • Print odd and even numbers using thread
    • SOLID principles
    • Comparable Vs Comparator
    • Sort HashMap/TreeMap based on value
    • Deep and Shallow Copy in Java with examples
    • Find the frequency of each character in a String
    • How to avoid duplicate Employee objects in HashSet ?
  • Spring
    • Loose Coupling & Dependency Injection
    • Bean Scope
    • Spring Bean Lifecycle
    • IoC Container Vs Application Context Vs Bean Factory
    • @Component Vs @Service @Repository Vs @Controller
    • How to read properties file in Spring
    • Spring AOP (Aspect Oriented Programming)
    • @Component Vs @Bean in Spring
    • Exception Handling in SpringBoot
    • XML configuration Vs Annotations configuration in Spring
    • Spring Data JPA
    • Spring Data REST
  • Spring Security
    • Spring Security with Form, Basic and JWT
    • Security Configuration in Spring Boot Apps
    • Security Protocols & Frameworks
    • Okta OAuth 2.0 SSO with Springboot
    • Spring Boot 2.x Security using Username Password
    • Spring Boot 2.x Security using JWT
    • Spring Boot 3.x Security using Username Password
    • Spring Boot 3.x Security using JWT
  • Microservices
    • Spring Cloud Config (Server & Client)
    • Spring Boot Microservices Tutorial (Part 1 of 2)
    • Spring Boot Microservices Tutorial (Part 2 of 2)
    • Circuit Breaker – Resilience4j
    • The Twelve-Factor App Principles
  • Event Driven Microservices
    • What is Event Driven Microservice ?
    • What is Saga Design Pattern ?
    • What is CQRS Design Pattern ?
  • Spring AI
    • ChatGPT API + SpringBoot Integration
  • Hibernate & JPA
    • JPA vs JDBC
    • CRUD Example Using Spring Boot JPA + H2 + Gradle
    • MongoDB Atlas With Spring Boot Example
    • Transaction Management
    • Relationships in JPA & Hibernate
    • Hibernate First & Second Level Cache
    • Spring Boot Flyway Postgres
  • DevOps
    • What is Devops ?
    • Docker
    • Kubernetes (K8S)
    • Jenkins
    • Infrastructure As Code
  • Functional Programming
    • Functional Programming Vs Structured Programming
    • Java 8 Programs For Interview
    • Predicate, Function, Consumer and Supplier in Java 8
    • Sort a List having Employee objects using Java 8
    • Find Employee from List using Java 8
  • AWS
    • AWS S3
    • AWS EC2
    • EC2 Solutions Architecting
    • How to create an EC2 instance ?
    • How to connect to AWS EC2 instance ?
    • Deploy application to AWS
    • AWS Lambda Java Tutorial
    • Spring Cloud Functions
    • How to Start/Stop AWS ECS automatically using Java Spring?
    • Container Solution in AWS
    • AWS SQS, SNS, MQ and Kinesis
    • Databases in AWS
    • AWS VPC: Peering, Endpoint, VPN, Gateways- Internet, NAT, DX
    • Machine Learning in AWS
    • Storage Solutions in AWS
    • AWS ASG (Auto Scaling Group)
  • AWS Certifications
    • SAA-C03
      • Design Cost-Optimized Architectures
    • AWS Certified Solution Architect-Associate
      • Question 1
  • Kafka
    • Apache Kafka
    • Kafka Producer & Consumer Example Using Spring boot & Conduktor
  • Angular
    • Angular Tutorial – Part 1
    • Angular Tutorial – Part 2
  • Miscellaneous
    • How to add a project to Git/GitHub/GitLab
    • How to Clone a project from Git/GitLab using Git Bash
    • How to query Oracle tables based on some date ?
    • How to highlight text in WordPress ?
    • How to add Page Jumps in WordPress page ?
  • Interview Preparation
    • Core java interview questions
    • Java Threads Interview Questions
  • Contact Me
  • Toggle search form

Infrastructure As Code

What is Infrastructure As Code ?

The typical life cycle of any java based application would look something like below, where, you have to first provision the server, then install & configure software and then you have to deploy the app:

Just imagine there are hundred of microservices talking to each other, it would be a cumbersome and error prone if we have to manually do these steps. The solution for this is Infrastructure As Code. It says treat your infrastructure provisioning as a code.
Some famous Infrastructure As Code are – Terraform AWS, CloudFormation, which can be used for provisioning server like creating virtual servers, load balancers, databases, networking configurations, etc.. Terraform is cloud-agnostic and can manage resources on multiple cloud platforms, including AWS, Azure, and Google Cloud. CloudFormation is specific to AWS and only manages AWS resources.

Some famous Infrastructure As Code tools for managing softwares (Installing and configuring) are Ansible, Chef, Puppet.

Terraform:

First you need to install Hashicorp Terraform from this official site.
Using Terraform you can provision server to any cloud platform, in this tutorial we will be using AWS.
Create the Terraform file with .tf extension:

main.tf

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 3.0"
        }
      }
    }
     
    # Configure the AWS Provider
    provider "aws" {
      region = "us-east-1"
      # VERSION IS NOT NEEDED HERE
    }

In the above terraform file we had mentioned the cloud provider- in this example its AWS.
Now we need to execute the above terraform file. For that we have to go to the place where we have saved the same terraform file and execute the below command:

terraform init
Ansible:

It is one of the tool of Infrastructure As Code. As we have seen in the above it deals with “Provision of Server” and “Install & Configuration of Softwares”.
To work with Ansible you have to first install Python and then install Ansible in your environment.
Ansible can not be installed in Windows. One of the option for windows user is to use Azure Cloud Shell.
Next you need to create some files, one is ansible.cfg and other is ansible_hosts. The sample code in the file is given below:

ansible.cfg:

[defaults]
inventory=./ansible_hosts
#inventory=inventory/01_aws_ec2.yml
remote_user=ec2-user
private_key_file=~/aws/aws_keys/default-ec2.pem
host_key_checking=False
retry_files_enabled=False
#interpreter_python=auto_silent

ansible_hosts:

[dev]
dev1 ansible_host=3.83.104.44
dev2 ansible_host=54.81.35.168

[qa]
qa1 ansible_host=3.95.249.131

[first]
dev1
qa1

[groupofgroups:children]
dev
first

[devsubset]
dev[1:2]

Copyright © 2025 .

Powered by PressBook Blog WordPress theme