S3 Buckets y Almacenamiento Seguro
Crear, securizar y organizar buckets
Un bucket S3 es un contenedor global (nombre único worldwide) que almacena objetos. El nombre debe cumplir reglas: minúsculas, 3-63 caracteres, sin guiones bajos, sin espacios. Dentro del bucket, organizas objetos con claves (rutas): 'datasets/train.csv' es un objeto con clave 'datasets/train.csv'. No hay directorios reales — solo prefijos en el nombre. Un bucket puede almacenar ilimitados objetos de cualquier tamaño.
La seguridad de S3 empieza con: (1) buckets privados por defecto (deny-all ingress), (2) Block Public Access habilitado (incluso si configuras acceso público accidentalmente, se bloquea), (3) versionado habilitado para datos críticos, (4) encriptación con AES256 o KMS. Un bucket público sin verificación es una brecha de seguridad frecuente. Las presigned URLs son la forma segura de compartir acceso temporal sin hacer el bucket público.
Los buckets pueden tener buckets de acceso. El Access Control List (ACL) es heredado pero aún usado: privado (solo owner), public-read (cualquiera puede leer), etc. Las S3 Bucket Policies son más poderosas: especifican quién puede hacer qué (ej. solo el role SageMaker puede GetObject). Para datos sensibles, usa bucket policies restrictivas + versionado + encriptación.
# Create bucket with defaults (private, encrypted)
aws s3 mb s3://my-ml-training-data --region us-east-1
# Enable block public access (prevent accidental leaks)
aws s3api put-public-access-block \
--bucket my-ml-training-data \
--public-access-block-configuration '{
"BlockPublicAcls": true,
"IgnorePublicAcls": true,
"BlockPublicPolicy": true,
"RestrictPublicBuckets": true
}'
# Enable versioning
aws s3api put-bucket-versioning \
--bucket my-ml-training-data \
--versioning-configuration Status=Enabled
# Enable default encryption (AES256)
aws s3api put-bucket-encryption \
--bucket my-ml-training-data \
--server-side-encryption-configuration '{
"Rules": [{
"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}
}]
}'
# Attach bucket policy (allow only SageMaker role)
cat > bucket-policy.json << 'EOF'
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::123456789:role/SageMaker"},
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": ["arn:aws:s3:::my-ml-training-data", "arn:aws:s3:::my-ml-training-data/*"]
}]
}
EOF
aws s3api put-bucket-policy \
--bucket my-ml-training-data \
--policy file://bucket-policy.jsonDebugging lab
Detecta y corrige el error en el código.
- 01
aws s3 mb s3://ai-model-artifacts # Bucket created, no security configured # Anyone can potentially read/upload
- 02
# Bucket tiene datos sensibles pero sin encriptación aws s3 cp model.pkl s3://ml-models/model.pkl
- 03
# Bucket policy says public-read # But Block Public Access is enabled # What happens?
- 04
aws s3 mb s3://my-data-bucket # Later, another account tries: aws s3 mb s3://my-data-bucket # Returns: BucketAlreadyExists (global namespace conflict)
- 05
# SageMaker training job needs read-only access to bucket # Current: bucket-policy permite s3:* para el role aws s3api get-object-acl --bucket ml-data --key model.pkl # Output: Grant full control to role