12-18-2024, 10:03 AM
![[Image: drivemaker-s3-ftp-sftp-drive-map-mobile.png]](https://doctorpapadopoulos.com/images/drivemaker-s3-ftp-sftp-drive-map-mobile.png)
Automating file archiving in S3 Glacier is totally doable and can save you a ton of time and hassle. I’m going to share some specific approaches I use, and I think you’ll find them really useful. The first step you’ll want to consider is deciding what files to archive and on what schedule. You don’t want to fill Glacier with everything you have; focus on files that you don’t need to access regularly. I generally suggest considering things like user uploads, logs that age out after a certain period, or even backup data that doesn’t have to be immediately available.
Once you’ve identified the data to archive, you can tackle the automation aspect. I love using AWS Lambda functions for this purpose. Lambda lets you run your code without provisioning servers, which is a real game-changer. You can create a function that gets triggered on a certain schedule or due to events, like new files landing in your S3 bucket. I often use CloudWatch Events to manage a schedule. For instance, if you want to move files that are older than 30 days into Glacier every day at midnight, you can set this up seamlessly.
To get the automation rolling, start by crafting a Lambda function in Python or Node.js; I usually stick with Python because I find it has a cleaner syntax. You will want to use the Boto3 library, which is the AWS SDK for Python. This library gives you a direct line to interact with S3 and Glacier.
Your function would first list the objects in the specified S3 bucket. You can use the "list_objects_v2" method from Boto3. This will return a dictionary with the contents of your bucket. Loop through this list and check the "LastModified" attribute of each object. If it’s older than your threshold—say, 30 days—you’ll definitely want to archive it.
Next, to transition these files to Glacier, you would call the "copy_object" method and specify "StorageClass='GLACIER'". I find that copying is a handy way to keep a version in both classes until I’m sure everything is functioning correctly. Alternatively, if space isn't a huge concern and you want to manage costs, you may choose to directly delete the original object after confirmation.
Here’s a snippet of code to illustrate what I mean:
import boto3
from datetime import datetime, timedelta
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket_name = 'your-bucket-name'
threshold_date = datetime.now() - timedelta(days=30)
# List all objects in the specified S3 bucket
response = s3.list_objects_v2(Bucket=bucket_name)
if 'Contents' in response:
for obj in response['Contents']:
last_modified = obj['LastModified']
if last_modified < threshold_date:
# Move to Glacier
copy_source = {'Bucket': bucket_name, 'Key': obj['Key']}
s3.copy_object(CopySource=copy_source, Bucket=bucket_name, Key=obj['Key'], StorageClass='GLACIER')
s3.delete_object(Bucket=bucket_name, Key=obj['Key'])
This code is fairly straightforward. It checks every object and moves those older than 30 days to Glacier while deleting the original. Adjust the days in "timedelta(days=30)" depending on your retention policy, of course.
Another cool aspect to consider is lifecycle policies. If you prefer a more AWS-native way of handling this, using S3 Lifecycle configurations is a strong option. You can log into the AWS Management Console, navigate to your S3 bucket, and set a lifecycle rule that automatically transitions files to Glacier based on object age or even delete them after a specific period. It’s super simple to set up through the UI, but it lacks the flexibility that Lambda gives you regarding custom conditions.
But if you love the control you get from code, definitely go with that Lambda approach. Just keep in mind you’ll also need to think through IAM roles and permissions. Your Lambda function will need the right policies attached to access the S3 bucket, so don’t forget about that. You can create a specific role, say "lambda-s3-glacier-role", that has permissions including "s3:ListBucket", "s3:GetObject", "s3


You might also want to add notifications. Fine-tuning your Lambda function to alert you is always a good idea. I often hook it up to SNS, which allows me to receive a message whenever files are archived or if something goes wrong. This little addition takes just a few more lines of code, and you can configure it to send email or SMS notifications based on your preference.
It might be worthwhile considering how often you need to access the archived data. If it’s not a common occurrence, moving objects directly to S3 Glacier Deep Archive can save even more costs. You can do this the exact same way you are with Glacier, just with a different storage class during your "copy_object" call. Think about how much data retrieval costs, though; going the Deep Archive route means you’ll want to make sure you really don’t need the files in a hurry.
Here’s the adjustment to the relevant line in the Python function:
s3.copy_object(CopySource=copy_source, Bucket=bucket_name, Key=obj['Key'], StorageClass='DEEP_ARCHIVE')
This might appear to complicate things a bit, but it’s worth it for the cost savings if the data is genuinely long-term archival.
Finally, remember to monitor your Glacier storage through the S3 console. I often check the metrics to ensure that my files are being transitioned as expected and that there aren’t any unexpected costs piling up due to frequent retrievals. Using CloudWatch to keep an eye on your functions can also help identify any failures that might occur during execution, which is a good safety net to have in place.
In conclusion, automating your file archiving in S3 Glacier can definitely streamline your process, whether through Lambda or lifecycle policies. The flexibility of code versus the simplicity of configuration gives you options tailored to your workflow. I'm sharing these thoughts because I know it can make a big difference in organizing your storage effectively and managing costs. Just remember to adjust the thresholds and setups according to your specific needs, and you’ll set yourself up for smooth sailing in the cloud.