0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Scripting Cloud Resources via CLI and jq

Drive cloud provider CLIs idempotently and parse JSON responses to provision and tear down resources.

Why Idempotent Cloud Scripting Matters

At C2 level, cloud scripting is not about clicking buttons — it is about writing code that can run safely multiple times without creating duplicate resources or failing on the second run.

An idempotent script checks whether a resource already exists before creating it. This is the cornerstone of reliable infrastructure automation.

  • Cloud CLIs (AWS, GCP, Azure) return JSON — parsing that output is essential.
  • jq is the standard Unix tool for slicing, filtering, and transforming JSON from shell scripts.
  • Combining CLI + jq + conditional logic lets you write robust, repeatable provisioning scripts.

Throughout this lesson you will provision S3 buckets, EC2 instances, and IAM roles using AWS CLI as the reference, with patterns that transfer directly to gcloud and az.

Installing and Verifying Cloud CLIs

Before scripting, confirm the right tools are present. Always version-pin in CI to avoid drift between environments.

The snippet below checks for AWS CLI v2, jq, and the GCP SDK, installing only what is missing — a pattern useful in bootstrap scripts for fresh VMs or containers.

#!/usr/bin/env bash
set -euo pipefail

check_or_install() {
  local cmd="$1"
  local install_cmd="$2"
  if ! command -v "$cmd" &>/dev/null; then
    echo "[INFO] $cmd not found — installing..."
    eval "$install_cmd"
  else
    echo "[OK]   $cmd $("$cmd" --version 2>&1 | head -1)"
  fi
}

# AWS CLI v2
check_or_install aws \
  'curl -fsSL https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o /tmp/awscliv2.zip && unzip -q /tmp/awscliv2.zip -d /tmp && sudo /tmp/aws/install'

# jq
check_or_install jq \
  'sudo apt-get install -y jq 2>/dev/null || sudo yum install -y jq'

# gcloud (optional)
check_or_install gcloud \
  'echo "Install gcloud SDK manually from https://cloud.google.com/sdk"'

echo "All prerequisites satisfied."

All lessons in this course

  1. Writing Lean Dockerfiles and Shell Entrypoints
  2. Templating Configs with envsubst and heredocs
  3. Scripting Cloud Resources via CLI and jq
  4. Health Probes, Readiness Gates, and Wait Loops
← Back to Linux Command Line & Bash Scripting Mastery