1 Answer
- Newest
- Most votes
- Most comments
1
The error "Connection was closed before we received a valid response from endpoint URL" typically indicates a network connectivity issue or authentication problem when uploading to S3. Here's an improved deployment script with better error handling and alternative upload methods:
#!/bin/bash set -euo pipefail # ========================= # Configuration # ========================= APPLICATION_NAME="app" DEPLOYMENT_GROUP_NAME="appr" S3_BUCKET="codedeploy" AWS_REGION="us-east-1" ZIP_NAME="app.zip" S3_KEY="${ZIP_NAME}" # ========================= # Create Deployment Bundle # ========================= echo "Creating deployment bundle..." rm -f "${ZIP_NAME}" zip -r "${ZIP_NAME}" \ appspec.yml \ deployment \ managers \ manager_utils \ -x "*.git*" \ -x "*__pycache__*" \ -x "*.pyc" if [ ! -f "${ZIP_NAME}" ]; then echo "❌ ZIP file was not created" exit 1 fi FILE_SIZE=$(stat -f%z "${ZIP_NAME}" 2>/dev/null || stat -c%s "${ZIP_NAME}" 2>/dev/null) echo "✅ ZIP file created: ${ZIP_NAME} (${FILE_SIZE} bytes)" # ========================= # Upload to S3 with retry logic # ========================= echo "Uploading to S3: s3://${S3_BUCKET}/${S3_KEY}..." MAX_RETRIES=3 RETRY_COUNT=0 while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do echo "Upload attempt $((RETRY_COUNT + 1)) of ${MAX_RETRIES}..." # Try using standard s3 cp command first (handles multipart automatically) if aws s3 cp "${ZIP_NAME}" "s3://${S3_BUCKET}/${S3_KEY}" \ --region "${AWS_REGION}" \ --no-progress 2>&1; then echo "✅ Upload successful" break else RETRY_COUNT=$((RETRY_COUNT + 1)) if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then echo "⚠️ Upload failed, retrying in 5 seconds..." sleep 5 else echo "❌ Upload failed after ${MAX_RETRIES} attempts" echo "Troubleshooting steps:" echo "1. Verify AWS credentials: aws sts get-caller-identity" echo "2. Check bucket exists: aws s3 ls s3://${S3_BUCKET} --region ${AWS_REGION}" echo "3. Verify bucket permissions and region" echo "4. Check network connectivity" exit 1 fi fi done # ========================= # Verify Upload # ========================= echo "Verifying S3 upload..." sleep 2 # Brief pause for S3 consistency if aws s3api head-object \ --bucket "${S3_BUCKET}" \ --key "${S3_KEY}" \ --region "${AWS_REGION}" &>/dev/null; then echo "✅ File successfully uploaded and verified in S3" else echo "❌ File not found in S3 after upload" exit 1 fi # ========================= # Trigger CodeDeploy Deployment # ========================= echo "Triggering CodeDeploy deployment..." DEPLOYMENT_ID=$(aws deploy create-deployment \ --application-name "${APPLICATION_NAME}" \ --deployment-group-name "${DEPLOYMENT_GROUP_NAME}" \ --s3-location bucket="${S3_BUCKET}",key="${S3_KEY}",bundleType=zip \ --ignore-application-stop-failures \ --region "${AWS_REGION}" \ --query "deploymentId" \ --output text) echo "✅ Deployment triggered: ${DEPLOYMENT_ID}" echo "Monitor at: https://console.aws.amazon.com/codesuite/codedeploy/deployments/${DEPLOYMENT_ID}?region=${AWS_REGION}" # ========================= # Monitor Deployment # ========================= echo "Monitoring deployment status..." if aws deploy wait deployment-successful \ --deployment-id "${DEPLOYMENT_ID}" \ --region "${AWS_REGION}"; then echo "✅ Deployment completed successfully" else echo "❌ Deployment failed" aws deploy get-deployment \ --deployment-id "${DEPLOYMENT_ID}" \ --region "${AWS_REGION}" \ --query "deploymentInfo.errorInformation" exit 1 fi
Key improvements:
- Retry logic: Attempts upload up to 3 times with delays
- Uses
aws s3 cp: Better handles network issues and multipart uploads automatically - Better verification: Uses
head-objectinstead oflsfor more reliable checking - File size logging: Helps identify if the file is too large
- Enhanced error messages: Provides troubleshooting steps
Before running, verify:
# Check AWS credentials aws sts get-caller-identity # Verify bucket exists and is accessible aws s3 ls s3://codedeploy --region us-east-1 # Check bucket region matches aws s3api get-bucket-location --bucket codedeploy
If the connection issue persists, it may be due to:
- Network firewall/proxy blocking S3 access
- AWS credentials expired or invalid
- Bucket in different region than specified
- IAM permissions missing for s3:PutObject
- VPC endpoint configuration if running from private subnet
Sources
Learn the basics of Amazon S3 with an AWS SDK - AWS SDK Code Examples
Learn the basics of Amazon S3 with an AWS SDK - Amazon Simple Storage Service
Relevant content
asked 8 months ago
asked 8 months ago
asked 2 years ago
asked 8 months ago
- AWS OFFICIALUpdated 4 years ago
