#!/usr/bin/env bash set -euo pipefail # ============================================================================= # create-db2-audit-role.sh — Create the IAM policy/role and RDS option group # that let RDS for Db2 upload audit logs to your S3 bucket. # # Configurable via environment variables (all optional except where noted): # REGION AWS region for the option group / ARNs (default: us-east-1) # AUDIT_BUCKET_NAME S3 bucket that receives audit logs (default: rds-db2-enablement) # AUDIT_KMS_KEY_ARN CMK ARN for the bucket's SSE-KMS encryption. REQUIRED if the # bucket uses SSE-KMS; leave unset only for SSE-S3 (AES256) buckets. # DB_INSTANCE_ID Scope the role's trust to a single instance (default: * = any # Db2 instance in this account/region) # MAJOR_ENGINE_VERSION Db2 major engine version for the option group (default: 11.5) # ============================================================================= policy_name="db2-audit-policy" role_name="db2-audit-role" audit_bucket_name="${AUDIT_BUCKET_NAME:-rds-db2-enablement}" region="${REGION:-${AWS_REGION:-us-east-1}}" major_engine_version="${MAJOR_ENGINE_VERSION:-11.5}" instance_id="${DB_INSTANCE_ID:-*}" # Account ID is computed once, up front, so it can be interpolated safely # (command substitution does NOT expand inside single-quoted strings). account_id="$(aws sts get-caller-identity --query Account --output text)" # KMS key used for the audit bucket's SSE-KMS encryption. Scope kms:Decrypt / # kms:GenerateDataKey to THIS key only (least privilege) rather than "*". # Replace the placeholder, or export AUDIT_KMS_KEY_ARN before running. audit_kms_key_arn="${AUDIT_KMS_KEY_ARN:-arn:aws:kms:${region}:${account_id}:key/REPLACE-WITH-AUDIT-BUCKET-KMS-KEY-ID}" # --- Permissions policy (heredoc → variables expand) --- policy_document=$(cat <