23. Python for Cloud & DevOps

Python is widely used in:

  • Cloud computing
  • DevOps automation
  • Infrastructure management
  • Deployment pipelines

Python helps automate:

  • Server tasks
  • Cloud services
  • CI/CD workflows
  • Container management

What is Cloud Computing?

Cloud computing provides:

  • Servers
  • Storage
  • Databases
  • Networking

over the internet.

Popular cloud providers:

  • AWS
  • Azure
  • Google Cloud

What is DevOps?

DevOps combines:

  • Development
  • Operations

to improve software delivery.

DevOps focuses on:

  • Automation
  • Continuous integration
  • Continuous deployment

AWS SDK for Python (boto3)

AWS SDK allows Python programs to interact with Amazon Web Services.

Using boto3, you can manage:

  • S3 buckets
  • EC2 servers
  • DynamoDB
  • Lambda functions

Install boto3

pip install boto3

Import boto3

import boto3

Configure AWS Credentials

Use command:

aws configure

Enter:

  • Access key
  • Secret key
  • Region

Create S3 Client

import boto3

s3 = boto3.client("s3")

List S3 Buckets

response = s3.list_buckets()

for bucket in response["Buckets"]:

print(bucket["Name"])

Upload File to S3

s3.upload_file(
"data.txt",
"my-bucket",
"data.txt"
)

print("File uploaded")

Download File from S3

s3.download_file(
"my-bucket",
"data.txt",
"downloaded.txt"
)

Create EC2 Resource

ec2 = boto3.resource("ec2")

List EC2 Instances

for instance in ec2.instances.all():

print(
instance.id,
instance.state["Name"]
)

Launch EC2 Instance

ec2.create_instances(
ImageId="ami-123456",
MinCount=1,
MaxCount=1,
InstanceType="t2.micro"
)

Docker with Python

Docker is used to package applications into containers.

Containers include:

  • Application code
  • Dependencies
  • Environment settings

Benefits of Docker

✅ Consistent environments
✅ Easy deployment
✅ Lightweight containers
✅ Faster development


Install Docker SDK for Python

pip install docker

Import Docker SDK

import docker

Connect to Docker

client = docker.from_env()

List Docker Containers

containers = client.containers.list()

for container in containers:

print(container.name)

Run Docker Container

client.containers.run(
"nginx",
detach=True
)

Stop Container

container = client.containers.get(
"container_id"
)

container.stop()

Dockerfile Example

A Dockerfile defines container instructions.

FROM python:3.11

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

CMD ["python", "app.py"]

Build Docker Image

docker build -t myapp .

Run Docker Container

docker run -p 5000:5000 myapp

CI/CD Basics

CI/CD stands for:

TermMeaning
CIContinuous Integration
CDContinuous Delivery/Deployment

Continuous Integration (CI)

CI automatically:

  • Builds code
  • Runs tests
  • Detects issues

whenever code changes.


Continuous Deployment (CD)

CD automatically deploys applications after successful testing.


CI/CD Workflow

Code → Build → Test → Deploy

Popular CI/CD Tools

  • GitHub Actions
  • Jenkins
  • GitLab CI/CD
  • CircleCI

GitHub Actions Example

.github/workflows/python.yml

name: Python CI

on:
push:
branches:
- main

jobs:

build:

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4

with:
python-version: 3.11

- name: Install dependencies
run: pip install -r requirements.txt

- name: Run tests
run: pytest

Jenkins Pipeline Example

pipeline {

agent any

stages {

stage('Build') {

steps {
sh 'pip install -r requirements.txt'
}
}

stage('Test') {

steps {
sh 'pytest'
}
}
}
}

Deploy Flask App with Docker

FROM python:3.11

WORKDIR /app

COPY . .

RUN pip install flask

CMD ["python", "app.py"]

Flask App Example

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
return "Cloud App Running"

app.run(host="0.0.0.0")

Infrastructure Automation

Python can automate:

  • Server provisioning
  • Monitoring
  • Deployment
  • Configuration management

Popular tools:

  • Ansible
  • Terraform
  • Kubernetes APIs

Example: Run Shell Command

import subprocess

result = subprocess.run(
["ls"],
capture_output=True,
text=True
)

print(result.stdout)

Monitoring Example

import psutil

cpu = psutil.cpu_percent()

memory = psutil.virtual_memory().percent

print("CPU:", cpu)

print("Memory:", memory)

Practical Example

Upload File to AWS S3

import boto3

s3 = boto3.client("s3")

file_name = "report.txt"

bucket = "my-bucket"

s3.upload_file(
file_name,
bucket,
file_name
)

print("Upload completed")

Advantages of Python in Cloud & DevOps

✅ Easy automation
✅ Strong cloud SDK support
✅ Fast scripting
✅ DevOps integration
✅ Infrastructure management


Best Practices

✅ Secure cloud credentials
✅ Use environment variables
✅ Automate testing
✅ Use containers for deployment
✅ Monitor infrastructure regularly


Summary

In this chapter you learned:

✅ AWS SDK with boto3
✅ Docker with Python
✅ Dockerfiles
✅ CI/CD basics
✅ GitHub Actions
✅ Infrastructure automation