DEV Community

akhil mittal
akhil mittal

Posted on • Updated on

Practical Usecase of Python for Devops&Cloud Engineer Role

Python is widely used in DevOps and cloud engineering due to its flexibility, extensive libraries, and easy integration with cloud services and automation tools. Below are some practical use cases of Python scripting for DevOps and cloud engineers, along with examples:

1. Automating Cloud Operations (AWS, Azure, GCP)

Cloud engineers often need to automate the creation, monitoring, and management of cloud resources. Python, with SDKs like Boto3 for AWS, Azure SDK for Python, and Google Cloud SDK, is commonly used for cloud automation.

Example: Automate AWS EC2 Instance Creation Using Boto3
This script creates a new EC2 instance in AWS using the Boto3 SDK.

import boto3
from botocore.exceptions import NoCredentialsError, ClientError

# Create a session using a specific AWS profile
session = boto3.Session(profile_name='default')  # Replace 'default' with your AWS profile name if needed

# Create EC2 client
ec2_client = session.client('ec2', region_name='us-west-2')  # Replace with your preferred AWS region


def list_instances():
    """List all EC2 instances."""
    try:
        response = ec2_client.describe_instances()
        instances = response['Reservations']

        if not instances:
            print("No EC2 instances found.")
            return

        for reservation in instances:
            for instance in reservation['Instances']:
                instance_id = instance['InstanceId']
                state = instance['State']['Name']
                print(f"Instance ID: {instance_id}, State: {state}")

    except NoCredentialsError:
        print("AWS credentials not found.")
    except ClientError as e:
        print(f"Error: {e}")


def start_instance(instance_id):
    """Start an EC2 instance by instance ID."""
    try:
        ec2_client.start_instances(InstanceIds=[instance_id])
        print(f"Starting instance {instance_id}...")

    except ClientError as e:
        print(f"Failed to start instance: {e}")


def stop_instance(instance_id):
    """Stop an EC2 instance by instance ID."""
    try:
        ec2_client.stop_instances(InstanceIds=[instance_id])
        print(f"Stopping instance {instance_id}...")

    except ClientError as e:
        print(f"Failed to stop instance: {e}")


def get_instance_status(instance_id):
    """Get the status of an EC2 instance by instance ID."""
    try:
        response = ec2_client.describe_instance_status(InstanceIds=[instance_id])
        if not response['InstanceStatuses']:
            print(f"No status found for instance {instance_id}. It might be stopped or terminated.")
            return

        status = response['InstanceStatuses'][0]
        print(f"Instance ID: {instance_id}, State: {status['InstanceState']['Name']}, "
              f"System Status: {status['SystemStatus']['Status']}, "
              f"Instance Status: {status['InstanceStatus']['Status']}")

    except ClientError as e:
        print(f"Error retrieving status: {e}")


# Menu-driven program for instance management
def main():
    while True:
        print("\nAWS EC2 Instance Management")
        print("1. List all instances")
        print("2. Start an instance")
        print("3. Stop an instance")
        print("4. Get instance status")
        print("5. Exit")

        choice = input("Enter your choice: ")

        if choice == '1':
            list_instances()
        elif choice == '2':
            instance_id = input("Enter the Instance ID to start: ")
            start_instance(instance_id)
        elif choice == '3':
            instance_id = input("Enter the Instance ID to stop: ")
            stop_instance(instance_id)
        elif choice == '4':
            instance_id = input("Enter the Instance ID to check status: ")
            get_instance_status(instance_id)
        elif choice == '5':
            print("Exiting...")
            break
        else:
            print("Invalid choice. Please try again.")


if __name__ == "__main__":
    main()


Enter fullscreen mode Exit fullscreen mode

Use Case: Cloud engineers can automate the provisioning of instances and other cloud resources, reducing manual effort and time spent on infrastructure setup.

2. CI/CD Pipeline Scripting

Python can be used to create custom scripts that handle CI/CD pipeline tasks such as deployment, testing, and version control. Tools like Jenkins, GitLab CI, or CircleCI often run Python scripts as part of the pipeline to automate tasks.

Example: Python Script for Rolling Deployment on Kubernetes

from kubernetes import client, config

# Load Kubernetes config
config.load_kube_config()

# Initialize API client
v1 = client.AppsV1Api()

# Define the rolling update function
def rolling_update(deployment_name, namespace):
    deployment = v1.read_namespaced_deployment(deployment_name, namespace)

    # Increase replica count by 1 for the rolling update
    deployment.spec.replicas += 1
    v1.patch_namespaced_deployment(deployment_name, namespace, deployment)

    print(f"Rolling update started for deployment: {deployment_name}")

# Perform rolling update
rolling_update('my-deployment', 'default')

Enter fullscreen mode Exit fullscreen mode

Use Case: DevOps engineers can script deployment strategies (e.g., blue/green or rolling updates) and integrate them into CI/CD pipelines for seamless, automated deployments.

3. Infrastructure as Code (IaC)

Python can be used in Infrastructure as Code (IaC) scenarios, either directly via Python-based tools like Pulumi, or by calling cloud APIs. Python helps engineers write reusable, modular scripts to deploy and manage infrastructure.

Example: Infrastructure Deployment with Pulumi (Python SDK)

import pulumi
import pulumi_aws as aws

# Define an AWS EC2 instance
instance = aws.ec2.Instance('my-instance',
                            instance_type='t2.micro',
                            ami='ami-0c55b159cbfafe1f0')

# Export the public IP of the instance
pulumi.export('instance_ip', instance.public_ip)

Enter fullscreen mode Exit fullscreen mode

Use Case: DevOps and cloud engineers use Python to deploy, manage, and update cloud infrastructure in an automated, version-controlled manner.

4. Monitoring and Logging Automation

Python scripts can be used to automate monitoring and logging for systems and cloud resources. By using SDKs and libraries like Prometheus, Datadog, and cloud provider logging APIs, Python helps in fetching, processing, and analyzing logs.

Example: Fetch CloudWatch Logs for AWS Lambda Using Boto3

import boto3

client = boto3.client('logs', region_name='us-west-2')

def get_logs(log_group_name):
    response = client.describe_log_streams(logGroupName=log_group_name, orderBy='LastEventTime', descending=True)
    log_stream = response['logStreams'][0]['logStreamName']

    log_events = client.get_log_events(logGroupName=log_group_name, logStreamName=log_stream)

    for event in log_events['events']:
        print(event['message'])

# Fetch logs for Lambda function
get_logs('/aws/lambda/my-lambda-function')

Enter fullscreen mode Exit fullscreen mode

Use Case: Automating log fetching and analysis saves engineers time by automatically pulling relevant logs for troubleshooting, without needing to manually query the logs each time.

5. Backup and Disaster Recovery

Python can automate backup processes and disaster recovery for cloud environments. Python scripts can be scheduled to regularly back up databases, EBS volumes, S3 buckets, etc., and can also be integrated with third-party backup services.

Example: Backup AWS RDS Databases Using Boto3

import boto3

# Initialize a session using Amazon RDS
rds = boto3.client('rds')

def create_db_snapshot(db_instance_identifier, snapshot_identifier):
    response = rds.create_db_snapshot(
        DBInstanceIdentifier=db_instance_identifier,
        DBSnapshotIdentifier=snapshot_identifier
    )
    print(f"Created snapshot: {response['DBSnapshot']['DBSnapshotIdentifier']}")

# Create a snapshot of RDS instance
create_db_snapshot('mydbinstance', 'mydbinstance-snapshot')

Enter fullscreen mode Exit fullscreen mode

Use Case: Cloud engineers can automate the backup of critical infrastructure like RDS databases, ensuring data availability in case of disasters.

6. Security Audits and Compliance

Python scripts can help automate security audits, check compliance, and enforce security policies. By combining cloud SDKs and security tools like AWS Config, GuardDuty, or Open Policy Agent (OPA), Python helps monitor resources for misconfigurations or security vulnerabilities.

Example: Check for Unencrypted S3 Buckets Using Boto3

import boto3

s3 = boto3.client('s3')

def check_s3_encryption():
    buckets = s3.list_buckets()

    for bucket in buckets['Buckets']:
        bucket_name = bucket['Name']
        try:
            enc = s3.get_bucket_encryption(Bucket=bucket_name)
            print(f"Bucket {bucket_name} is encrypted.")
        except Exception as e:
            print(f"Bucket {bucket_name} is not encrypted: {str(e)}")

# Run the check
check_s3_encryption()

Enter fullscreen mode Exit fullscreen mode

Use Case: Security audits can be automated to ensure that no cloud resources (e.g., S3 buckets) are left unencrypted or misconfigured, enhancing the security posture.

7. Server Health Checks and Auto-Healing

Python scripts can be used to automate health checks and trigger auto-healing actions for cloud services or server infrastructure. For example, detecting unhealthy EC2 instances and replacing them.

Example: Auto-Healing Unhealthy EC2 Instances

import boto3

ec2 = boto3.client('ec2', region_name='us-west-2')

def check_instance_health(instance_id):
    status = ec2.describe_instance_status(InstanceIds=[instance_id])
    instance_status = status['InstanceStatuses'][0]['InstanceState']['Name']

    if instance_status != 'running':
        print(f"Instance {instance_id} is not healthy. Terminating and starting a new one.")
        ec2.terminate_instances(InstanceIds=[instance_id])
        # Launch a new instance (could be automated here)

# Check the health of an EC2 instance
check_instance_health('i-1234567890abcdef0')

Enter fullscreen mode Exit fullscreen mode

Use Case: Automating health checks and auto-healing ensures high availability of critical infrastructure by proactively addressing issues.

Summary of Use Cases

Automating Cloud Operations: Automate resource creation, scaling, and management.
CI/CD Pipeline Scripting: Automate deployment strategies within CI/CD workflows.
Infrastructure as Code (IaC): Deploy cloud infrastructure programmatically.
Monitoring and Logging: Automate log collection and analysis.
Backup and Disaster Recovery: Regularly back up critical infrastructure.
Security Audits and Compliance: Ensure security and compliance with automated checks.
Server Health Checks and Auto-Healing: Proactively monitor and repair infrastructure.
Python’s versatility makes it ideal for automating various tasks that cloud and DevOps engineers handle daily, increasing productivity and operational efficiency.

Top comments (0)