Setting the file. One moment.
Create Cluster · Aurora Dsql · aws/agent-toolkit-for-aws · Skills Docs
ContentsBack to the top of the page 10
Setup DevOps Agent
33
AWS Deployment
(opens in a new tab)
scripts/ create-cluster.sh
Shell · 147 lines · 5 KB
15
set
-euo
pipefail
16
17 # create-cluster.sh - Create an Aurora DSQL cluster
18 #
19 # Usage: ./create-cluster.sh --created-by MODEL_ID [--region REGION] [--tags KEY=VALUE,...]
20 #
21 # Examples:
22 # ./create-cluster.sh --created-by claude-opus-4-6
23 # ./create-cluster.sh --created-by claude-opus-4-6 --region us-east-1
24 # ./create-cluster.sh --created-by claude-opus-4-6 --region us-west-2 --tags Environment=dev,Project=myapp
25
26 REGION = "${ REGION :- ${ AWS_REGION :- us-east-1 }}"
27 TAGS = ""
28 CREATED_BY = ""
29 DELETION_PROTECTION = true
30
31 # Parse arguments
32 while [[ $# -gt 0 ]]; do
33 case $1 in
34 --region )
35 REGION = " $2 "
36 shift 2
37 ;;
38 --tags )
39 TAGS = " $2 "
40 shift 2
41 ;;
42 --created-by )
43 CREATED_BY = " $2 "
44 shift 2
45 ;;
46 --no-deletion-protection )
47 DELETION_PROTECTION = false
48 shift
49 ;;
50 -h | --help )
51 echo "Usage: $0 --created-by MODEL_ID [--region REGION] [--tags KEY=VALUE,...] [--no-deletion-protection]"
52 echo ""
53 echo "Creates an Aurora DSQL cluster in the specified region."
54 echo "Deletion protection is enabled by default."
55 echo ""
56 echo "Options:"
57 echo " --region REGION AWS region (default: \$ AWS_REGION or us-east-1)"
58 echo " --tags TAGS Comma-separated tags (e.g., Env=dev,Project=app)"
59 echo " --created-by ID Model/agent identifier added as a 'created_by' cluster tag"
60 echo " --no-deletion-protection Disable deletion protection (default: enabled)"
61 echo " -h, --help Show this help message"
62 exit 0
63 ;;
64 *)
65 echo "Unknown option: $1 "
66 exit 1
67 ;;
68 esac
69 done
70
71 echo "Creating Aurora DSQL cluster in $REGION ..."
72
73 # Prepend created_by tag if --created-by was provided
74 if [[ -n " $CREATED_BY " ]]; then
75 # Validate: allow only alphanumeric, hyphens, underscores, and dots (e.g. claude-opus-4-6)
76 if [[ ! " $CREATED_BY " =~ ^[a-zA-Z0-9._-]+$ ]]; then
77 echo "Error: --created-by must contain only alphanumeric characters, hyphens, underscores, and dots." >&2
78 exit 1
79 fi
80 if [[ -n " $TAGS " ]]; then
81 TAGS = "created_by=${ CREATED_BY },${ TAGS }"
82 else
83 TAGS = "created_by=${ CREATED_BY }"
84 fi
85 fi
86
87 # Build the AWS CLI command as an array to avoid eval and shell injection
88 CMD = ( aws dsql create-cluster --region " $REGION " )
89
90 # Enable deletion protection by default (use --no-deletion-protection to opt out)
91 if [[ " $DELETION_PROTECTION " == "true" ]]; then
92 CMD += ( --deletion-protection-enabled )
93 else
94 CMD += ( --no-deletion-protection-enabled )
95 fi
96
97 # Add tags if provided
98 if [[ -n " $TAGS " ]]; then
99 # Convert comma-separated tags to JSON format using jq for safe escaping
100 TAG_JSON = $( printf '%s\n' " $TAGS " | tr ',' '\n' | jq -Rn '
101 [inputs | split("=") | {(.[0]): .[1:] | join("=")}] | add // {}
102 ' ) || {
103 echo "Error: Failed to convert tags to JSON." >&2
104 exit 1
105 }
106 if [[ -z " $TAG_JSON " || " $TAG_JSON " == "{}" ]]; then
107 echo "Error: Tags produced empty JSON. Check format: KEY=VALUE,..." >&2
108 exit 1
109 fi
110 CMD += ( --tags " $TAG_JSON " )
111 fi
112
113 # Execute the command directly (no eval)
114 TEMP_FILE = $( mktemp )
115 trap 'rm -f "$TEMP_FILE"' EXIT
116 "${ CMD [ @ ]}" > " $TEMP_FILE "
117
118 # Extract cluster identifier and endpoint
119 CLUSTER_ID = $( jq -r '.identifier' " $TEMP_FILE " )
120 CLUSTER_ARN = $( jq -r '.arn' " $TEMP_FILE " )
121
122 if [[ -z " $CLUSTER_ID " || " $CLUSTER_ID " == "null" ]]; then
123 echo "Error: Failed to extract cluster identifier from response." >&2
124 echo "Response:" >&2
125 cat " $TEMP_FILE " >&2
126 exit 1
127 fi
128
129 CLUSTER_ENDPOINT = "${ CLUSTER_ID }.dsql.${ REGION }.on.aws"
130
131 echo ""
132 echo "✓ Cluster created successfully!"
133 echo ""
134 echo "Cluster Identifier: $CLUSTER_ID "
135 echo "Cluster Endpoint: $CLUSTER_ENDPOINT "
136 echo "Cluster ARN: $CLUSTER_ARN "
137 echo "Region: $REGION "
138 echo ""
139 echo "Export these environment variables for use with psql-connect.sh and other scripts:"
140 echo ""
141 echo "export CLUSTER= $CLUSTER_ID "
142 echo "export REGION= $REGION "
143 echo ""
144 echo "To connect with psql:"
145 echo "./scripts/psql-connect.sh"
146
147 # Clean up handled by trap