Encriptación y Seguridad de Datos
Proteger datos en reposo y en tránsito
S3 ofrece dos modos de encriptación: (1) Server-Side Encryption (SSE) — AWS maneja las claves, datos se encriptan en S3. Opciones: SSE-S3 (AWS gestiona claves, AES256), SSE-KMS (usar AWS KMS para control granular de claves), (2) Client-Side Encryption — encriptas antes de enviar a S3, S3 solo almacena blob. Para la mayoría de casos, SSE-S3 es suficiente y gratuito. SSE-KMS cuesta pero permite control: quién puede desencriptar, cuándo, rotar claves.
El default en buckets nuevos es UNENCRYPTED en reposo (aunque en tránsito siempre es HTTPS). Habilitar encriptación es trivial: una llamada API. Para datos sensibles (API keys, modelos entrenados), encriptación es mandatorio. El costo es negligible ($0.03 por 10k operaciones KMS).
Complemento: versionado + encriptación es la defensa de datos. Versionado te permite recuperar versiones anteriores si algo se corrompe. Encriptación te protege de acceso no autorizado. Juntos, cubren corrupción accidental y robo de datos.
# Enable default encryption (AES256, free)
s3_client.put_bucket_encryption(
Bucket='ml-data',
ServerSideEncryptionConfiguration={
'Rules': [
{
'ApplyServerSideEncryptionByDefault': {
'SSEAlgorithm': 'AES256'
}
}
]
}
)
# Upload object with encryption explicitly
s3_client.put_object(
Bucket='ml-data',
Key='model.pkl',
Body=model_data,
ServerSideEncryption='AES256'
)
# Enable KMS encryption (AWS manages rotation)
s3_client.put_bucket_encryption(
Bucket='ml-data',
ServerSideEncryptionConfiguration={
'Rules': [
{
'ApplyServerSideEncryptionByDefault': {
'SSEAlgorithm': 'aws:kms',
'KMSMasterKeyID': 'arn:aws:kms:us-east-1:123456789:key/12345678-1234-1234-1234-123456789012'
}
}
]
}
)
# Check encryption status of bucket
response = s3_client.get_bucket_encryption(Bucket='ml-data')
print(response['ServerSideEncryptionConfiguration'])Debugging lab
Detecta y corrige el error en el código.
- 01
# Bucket with sensitive model files # No encryption configured response = s3_client.get_bucket_encryption(Bucket='models') # Output: ServerSideEncryptionConfiguration does not exist
- 02
# Regulatory requirement: data must be encrypted with customer-managed keys # Current: AES256 (AWS manages keys) # Need: KMS with customer key rotation
- 03
# Upload critical backup without specifying encryption s3_client.put_object( Bucket='backups', # Bucket has AES256 enabled Key='backup.tar.gz', Body=backup_data
- 04
# Migrate 1TB of unencrypted data to encrypted bucket # Data is already in S3 Standard unencrypted # How to encrypt?
- 05
# Download encrypted object from S3 response = s3_client.get_object(Bucket='models', Key='model.pkl') model = response['Body'].read() # Is this decrypted?