107 lines
4.1 KiB
Python
107 lines
4.1 KiB
Python
"""Create the Nova v1.1 spike AWS state backend (idempotent).
|
|
|
|
- S3 bucket nova-tfstate-<account_id>-us-east-1 (versioning enabled).
|
|
- DynamoDB table nova-outbox (PAY_PER_REQUEST; PK contractId, SK
|
|
eventType#eventTs) — used for BOTH Terraform state locking AND the
|
|
evidence outbox (D-P08-1).
|
|
|
|
Run with the bootstrap root key in env:
|
|
NOVA_BOOTSTRAP_AWS_ACCESS_KEY_ID / NOVA_BOOTSTRAP_AWS_SECRET_ACCESS_KEY
|
|
(falls back to NOVA_AWS_ACCESS_KEY_ID / NOVA_AWS_SECRET_ACCESS_KEY when
|
|
the provided key is a root principal). AWS_DEFAULT_REGION (defaults
|
|
to us-east-1).
|
|
|
|
Writes terraform/bootstrap/.bootstrap_state.json (gitignored bookkeeping).
|
|
|
|
Idempotent: re-running this script against an already-bootstrapped account
|
|
exits 0 without duplicating resources. The S3 state bucket is guarded by a
|
|
head_bucket probe (skips creation if it exists), bucket versioning is
|
|
re-PUT on every run (PutBucketVersioning is itself idempotent), and the
|
|
DynamoDB outbox table is guarded by a describe_table probe (skips creation
|
|
on ResourceNotFoundException). The bootstrap-state marker file is always
|
|
overwritten with the current run's timestamp (it is bookkeeping, not a
|
|
resource).
|
|
"""
|
|
|
|
import datetime
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
import boto3
|
|
|
|
|
|
REGION = os.environ.get("AWS_DEFAULT_REGION", "us-east-1")
|
|
ACCOUNT_ID = os.environ.get("NOVA_AWS_ACCOUNT_ID", "581513795199")
|
|
STATE_BUCKET = f"nova-tfstate-{ACCOUNT_ID}-us-east-1"
|
|
OUTBOX_TABLE = "nova-outbox"
|
|
|
|
|
|
def main():
|
|
key_id = os.environ.get("NOVA_BOOTSTRAP_AWS_ACCESS_KEY_ID") or os.environ.get("NOVA_AWS_ACCESS_KEY_ID")
|
|
secret = os.environ.get("NOVA_BOOTSTRAP_AWS_SECRET_ACCESS_KEY") or os.environ.get("NOVA_AWS_SECRET_ACCESS_KEY")
|
|
if not key_id or not secret:
|
|
sys.exit("FAIL: set NOVA_BOOTSTRAP_AWS_ACCESS_KEY_ID + NOVA_BOOTSTRAP_AWS_SECRET_ACCESS_KEY (root key)")
|
|
session = boto3.Session(
|
|
aws_access_key_id=key_id,
|
|
aws_secret_access_key=secret,
|
|
region_name=REGION,
|
|
)
|
|
s3 = session.client("s3", region_name=REGION)
|
|
dyn = session.client("dynamodb", region_name=REGION)
|
|
|
|
# --- S3 state bucket (idempotent) ---
|
|
try:
|
|
s3.head_bucket(Bucket=STATE_BUCKET)
|
|
print(f"s3: bucket {STATE_BUCKET} already exists")
|
|
except s3.exceptions.ClientError as e:
|
|
error_code = e.response.get("Error", {}).get("Code", "")
|
|
if error_code in ("404", "NoSuchBucket", "NotFound"):
|
|
kwargs = {"Bucket": STATE_BUCKET}
|
|
if REGION != "us-east-1":
|
|
kwargs["CreateBucketConfiguration"] = {"LocationConstraint": REGION}
|
|
s3.create_bucket(**kwargs)
|
|
print(f"s3: created bucket {STATE_BUCKET}")
|
|
else:
|
|
raise
|
|
# Enable versioning (idempotent)
|
|
s3.put_bucket_versioning(
|
|
Bucket=STATE_BUCKET,
|
|
VersioningConfiguration={"Status": "Enabled"},
|
|
)
|
|
print(f"s3: versioning enabled on {STATE_BUCKET}")
|
|
|
|
# --- DynamoDB outbox table (idempotent) ---
|
|
try:
|
|
dyn.describe_table(TableName=OUTBOX_TABLE)
|
|
print(f"dynamodb: table {OUTBOX_TABLE} already exists")
|
|
except dyn.exceptions.ResourceNotFoundException:
|
|
dyn.create_table(
|
|
TableName=OUTBOX_TABLE,
|
|
BillingMode="PAY_PER_REQUEST",
|
|
AttributeDefinitions=[
|
|
{"AttributeName": "contractId", "AttributeType": "S"},
|
|
{"AttributeName": "eventType#eventTs", "AttributeType": "S"},
|
|
],
|
|
KeySchema=[
|
|
{"AttributeName": "contractId", "KeyType": "HASH"},
|
|
{"AttributeName": "eventType#eventTs", "KeyType": "RANGE"},
|
|
],
|
|
)
|
|
print(f"dynamodb: created table {OUTBOX_TABLE}")
|
|
dyn.get_waiter("table_exists").wait(TableName=OUTBOX_TABLE)
|
|
|
|
marker = {
|
|
"account_id": ACCOUNT_ID,
|
|
"bucket_name": STATE_BUCKET,
|
|
"table_name": OUTBOX_TABLE,
|
|
"region": REGION,
|
|
"created_at": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
}
|
|
with open(os.path.join(os.path.dirname(__file__), ".bootstrap_state.json"), "w") as fh:
|
|
json.dump(marker, fh, indent=2)
|
|
print("bootstrap state marker written:", marker)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |