Ubuntu服务器CodeBuild项目RDS克隆命令格式错误排查求助
Let's break down the issues with your loop command and surrounding configuration that's causing your CodeBuild task to fail:
1. Undefined Variables ($DBNAME and $DBIDENTIFIER)
Looking at your pre_build phase, you define $TARGETDBENV as your new cluster name, but later reference $DBNAME and $DBIDENTIFIER in the loop and endpoint retrieval commands. These variables are not set anywhere in your script, so the AWS CLI commands will fail because they're trying to describe a cluster with an empty identifier.
2. Unnecessary HTML Entities (") in Shell Commands
In your original loop command (and some other script sections), you're using "—an HTML entity for double quotes. Shell commands don't recognize these; you should use regular double quotes (") instead.
3. Brittle JSON Parsing with grep/awk
Parsing AWS CLI output with grep and awk is error-prone, as JSON formatting can vary slightly. The AWS CLI has a built-in --query parameter that lets you extract values directly without relying on text manipulation tools, which is far more reliable.
Corrected pre_build Phase Commands
Here's the fixed version of your pre_build phase with all issues addressed:
pre_build: commands: - pip install --upgrade pip - pip3 install awscli --upgrade --user - export SOURCEDBENV=mysql-dev - export DATE=`date +%Y%m%d` - export TARGETDBENV=$SOURCEDBENV-$DATE - echo $TARGETDBENV # Get latest snapshot ARN directly with AWS CLI query (no need for tr later) - export ARNSNAP=$(aws rds describe-db-cluster-snapshots --db-cluster-identifier $SOURCEDBENV --query "reverse(sort_by(DBClusterSnapshots, &SnapshotCreateTime))[0].DBClusterSnapshotArn" --output text) - echo $ARNSNAP - aws rds restore-db-cluster-from-snapshot --snapshot-identifier $ARNSNAP --db-cluster-identifier $TARGETDBENV --engine aurora-mysql - aws rds create-db-instance --db-instance-identifier $TARGETDBENV --db-instance-class db.t3.medium --db-subnet-group-name db_subnet_grp_2019 --engine aurora-mysql --db-cluster-identifier $TARGETDBENV # Fixed loop using correct variable and reliable status check via AWS CLI query - while [ $(aws rds describe-db-clusters --db-cluster-identifier $TARGETDBENV --query "DBClusters[0].Status" --output text) != "available" ]; do echo "sleep 60s"; sleep 60; done - echo "Temp db ready" # Get endpoint cleanly with AWS CLI query instead of grep/awk - export ENDPOINT=$(aws rds describe-db-cluster-endpoints --db-cluster-identifier $TARGETDBENV --query "DBClusterEndpoints[?contains(Endpoint, '-ro-') == false].Endpoint" --output text) - echo $ENDPOINT
Key Improvements Explained
- Variable Consistency: We use
$TARGETDBENVeverywhere instead of undefined variables, ensuring all commands reference the correct cluster name. - HTML Entity Removal: Replaced
"with standard double quotes so the shell can interpret commands correctly. - Reliable Status Check: The loop now uses
aws rds describe-db-clusterswith a--queryparameter to directly fetch the cluster status, avoiding grep counts that can break from unexpected JSON formatting. - Clean Endpoint Retrieval: Endpoint extraction uses a query to filter out read-only endpoints and return the exact string, eliminating fragile grep/awk chains.
- Simplified Snapshot ARN Fetch: Added
--output textto the snapshot ARN command, so we don't needtr -d '"'to strip quotes later.
After making these changes, your CodeBuild task should successfully wait for the RDS cluster to become available before proceeding with build steps.
内容的提问来源于stack exchange,提问作者John Fox

