Infrastructure as Code ( IAC ) 

Section 4: Infrastructure as Code (IaC)

Lesson 1: Introduction to Infrastructure as Code

Infrastructure as Code (IaC) is a key DevOps practice involving the use of code to automate the provisioning and management of infrastructure. This approach brings several benefits, including improved scalability, version control, and consistency in infrastructure configuration.

(Terraform - main.tf):

hcl

# Terraform Configuration for AWS EC2 Instance


provider "aws" {

  region = "us-east-1"

}


resource "aws_instance" "example" {

  ami           = "ami-0c55b159cbfafe1f0"

  instance_type = "t2.micro"

  

  tags = {

    Name = "example-instance"

  }

}

In this Terraform configuration, an AWS EC2 instance is defined with specified attributes such as the Amazon Machine Image (AMI), instance type, and tags.


Lesson 2: Configuration Management

Configuration Management involves automating the setup and maintenance of server configurations. Tools like Chef or Puppet are commonly used for this purpose, ensuring that servers remain in the desired state.

(Chef - recipe.rb):

ruby

# Chef Recipe for Installing and Configuring Nginx


package 'nginx' do

  action :install

end


service 'nginx' do

  action [:enable, :start]

end


template '/etc/nginx/nginx.conf' do

  source 'nginx.conf.erb'

  notifies :restart, 'service[nginx]'

end

In this Chef recipe, Nginx is installed, configured, and the service is started. The nginx.conf.erb file is a template that can be customized based on specific requirements.

This concludes Section 4 on Infrastructure as Code (IaC) and Configuration Management. The next section will cover Monitoring and Logging in a DevOps environment.