#!/bin/bash set -e set -o pipefail # ----- Config ----- BUCKET_NAME=${1:-customersupport} INFRA_STACK_NAME=${2:-CustomerSupportStackInfra} COGNITO_STACK_NAME=${3:-CustomerSupportStackCognito} INFRA_TEMPLATE_FILE="prerequisite/infrastructure.yaml" COGNITO_TEMPLATE_FILE="prerequisite/cognito.yaml" REGION=$(aws configure get region) ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) FULL_BUCKET_NAME="${BUCKET_NAME}-${ACCOUNT_ID}" ZIP_FILE="lambda.zip" LAMBDA_SRC="prerequisite/lambda/python" S3_KEY="${ZIP_FILE}" # ----- 1. Create S3 bucket ----- echo "đŸĒŖ Using S3 bucket: $FULL_BUCKET_NAME" if [ "$REGION" = "us-east-1" ]; then aws s3api create-bucket \ --bucket "$FULL_BUCKET_NAME" \ 2>/dev/null || echo "â„šī¸ Bucket may already exist or be owned by you." else aws s3api create-bucket \ --bucket "$FULL_BUCKET_NAME" \ --region "$REGION" \ --create-bucket-configuration LocationConstraint="$REGION" \ 2>/dev/null || echo "â„šī¸ Bucket may already exist or be owned by you." fi # ----- 2. Zip Lambda code ----- echo "đŸ“Ļ Zipping contents of $LAMBDA_SRC into $ZIP_FILE..." cd "$LAMBDA_SRC" zip -r "../../../$ZIP_FILE" . > /dev/null cd - > /dev/null # ----- 3. Upload to S3 ----- echo "â˜ī¸ Uploading $ZIP_FILE to s3://$FULL_BUCKET_NAME/$S3_KEY..." aws s3 cp "$ZIP_FILE" "s3://$FULL_BUCKET_NAME/$S3_KEY" # ----- 4. Deploy CloudFormation ----- deploy_stack() { set +e local stack_name=$1 local template_file=$2 shift 2 local params=("$@") echo "🚀 Deploying CloudFormation stack: $stack_name" output=$(aws cloudformation deploy \ --stack-name "$stack_name" \ --template-file "$template_file" \ --capabilities CAPABILITY_NAMED_IAM \ --region "$REGION" \ "${params[@]}" 2>&1) exit_code=$? echo "$output" if [ $exit_code -ne 0 ]; then if echo "$output" | grep -qi "No changes to deploy"; then echo "â„šī¸ No updates for stack $stack_name, continuing..." return 0 else echo "❌ Error deploying stack $stack_name:" echo "$output" return $exit_code fi else echo "✅ Stack $stack_name deployed successfully." return 0 fi } # ----- Run both stacks ----- echo "🔧 Starting deployment of infrastructure stack..." deploy_stack "$INFRA_STACK_NAME" "$INFRA_TEMPLATE_FILE" --parameter-overrides LambdaS3Bucket="$FULL_BUCKET_NAME" LambdaS3Key="$S3_KEY" infra_exit_code=$? echo "🔧 Starting deployment of Cognito stack..." deploy_stack "$COGNITO_STACK_NAME" "$COGNITO_TEMPLATE_FILE" cognito_exit_code=$? echo "🔍 Fetching Knowledge Base and Data Source IDs from SSM..." # ----- 6. Create Knowledge Base ----- python prerequisite/knowledge_base.py --mode create echo "✅ Deployment complete."