Certainly! To migrate an AWS Glue table, you typically want to:
Extract the table definition from the source Glue catalog.
Create the same table in the target Glue catalog (possibly in a different database or AWS account).
Below is a simple bash script that uses AWS CLI to:
Get the table definition from the source Glue database.
Modify the database name (or other parameters if needed).
Create the table in the target Glue database.
Prerequisites:
AWS CLI installed and configured with appropriate permissions.
jq installed for JSON processing.
Source and target Glue databases exist.
Example Bash Script: migrate_glue_table.sh
bash
#!/bin/bash
# Usage: ./migrate_glue_table.sh <source_database> <table_name> <target_database> [target_region]
set -e
SOURCE_DB=$1
TABLE_NAME=$2
TARGET_DB=$3
TARGET_REGION=${4:-us-east-1} # default region if not provided
if [[ -z "$SOURCE_DB" || -z "$TABLE_NAME" || -z "$TARGET_DB" ]]; then
echo "Usage: $0 <source_database> <table_name> <target_database> [target_region]"
exit 1
fi
echo "Fetching table definition for $TABLE_NAME from database $SOURCE_DB..."
# Get the table definition JSON from source Glue catalog
TABLE_JSON=$(aws glue get-table --database-name "$SOURCE_DB" --name "$TABLE_NAME" --query 'Table' --output json)
if [[ -z "$TABLE_JSON" ]]; then
echo "Failed to get table definition for $TABLE_NAME in $SOURCE_DB"
exit 1
fi
# Modify the database name in the JSON to the target database
# Remove fields that should not be included in CreateTable API call
# (like CreateTime, UpdateTime, DatabaseName, CatalogId, VersionId, etc.)
CLEANED_TABLE_JSON=$(echo "$TABLE_JSON" | jq --arg target_db "$TARGET_DB" '
.DatabaseName = $target_db
| del(.CreateTime, .UpdateTime, .DatabaseName, .CatalogId, .VersionId, .CreatedBy, .IsRegisteredWithLakeFormation)
')
echo "Creating table $TABLE_NAME in target database $TARGET_DB..."
# Create the table in the target Glue catalog
aws glue create-table --database-name "$TARGET_DB" --table-input "$CLEANED_TABLE_JSON" --region "$TARGET_REGION"
echo "Table $TABLE_NAME migrated successfully from $SOURCE_DB to $TARGET_DB."
This script assumes the source and target Glue catalogs are in the same AWS account. If migrating across accounts, you need to configure AWS CLI profiles or roles accordingly.
The script does not migrate the underlying data, only the Glue table metadata.
You can extend the script to handle partitions or other Glue resources if needed.
If you want me to help with a more complex migration (e.g., cross-account, data migration, partitions), just ask!