Skip to content

unable to upload zip to s3 in code deploy

0

#!/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}" # <-- no artifacts folder

=========================

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 echo "✅ ZIP file created: ${ZIP_NAME}"

=========================

Upload to S3 (use s3api to avoid SSO/multipart issues)

=========================

echo "Uploading to S3: s3://${S3_BUCKET}/${S3_KEY}..." aws s3api put-object
--bucket "${S3_BUCKET}"
--key "${S3_KEY}"
--body "${ZIP_NAME}"
--region "${AWS_REGION}"

=========================

Verify Upload

=========================

echo "Verifying S3 upload..." if aws s3 ls "s3://${S3_BUCKET}/${S3_KEY}" --region "${AWS_REGION}" | grep "${ZIP_NAME}" > /dev/null; then echo "✅ File successfully uploaded to S3" else echo "❌ File not found in S3: ${S3_KEY}" 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}"

=========================

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" exit 1 fi

used this deployment.sh to deploy repo code on ec2 by code deploy getting this

adding: manager_utils/logger_config.py (deflated 59%) ✅ ZIP file created: app.zip Uploading to S3: s3://codedeploy/app.zip...

Connection was closed before we received a valid response from endpoint URL: "https://codedeploy.s3.us-east-1.amazonaws.com/app.zip".

please provide .sh for deployment

asked 8 months ago133 views

1 Answer
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:

  1. Retry logic: Attempts upload up to 3 times with delays
  2. Uses aws s3 cp: Better handles network issues and multipart uploads automatically
  3. Better verification: Uses head-object instead of ls for more reliable checking
  4. File size logging: Helps identify if the file is too large
  5. 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:

answered 8 months ago

EXPERT

reviewed 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.