Skip to content

Commit

Permalink
Merge pull request #816 from ministryofjustice/version-s3
Browse files Browse the repository at this point in the history
Add versioning and simple lifecycle to S3 buckets.
  • Loading branch information
Nicholas Tollervey authored Jun 23, 2020
2 parents 1c85f31 + 42f236d commit c12272d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
32 changes: 30 additions & 2 deletions controlpanel/api/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,46 @@ def delete_role(name):


def create_bucket(bucket_name, is_data_warehouse=False):
s3_resource = boto3.resource("s3")
s3_client = boto3.client('s3')
try:
bucket = boto3.resource('s3').create_bucket(
bucket = s3_resource.create_bucket(
Bucket=bucket_name,
ACL='private',
CreateBucketConfiguration={
'LocationConstraint': settings.BUCKET_REGION,
},
)
# Enable versioning by default.
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html?highlight=s3#S3.BucketVersioning
versioning = bucket.Versioning()
versioning.enable()
# Set bucket lifecycle. Send non-current versions of files to glacier
# storage after 30 days.
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.put_bucket_lifecycle_configuration
lifecycle_id = f"{bucket_name}_lifecycle_configuration"
lifecycle_conf = s3_client.put_bucket_lifecycle_configuration(
Bucket=bucket_name,
LifecycleConfiguration={
"Rules": [
{
"ID": lifecycle_id,
"Status": "Enabled",
"Prefix": "",
"NoncurrentVersionTransitions": [
{
'NoncurrentDays': 30,
'StorageClass': 'GLACIER',
},
]
},
]
}
)
if is_data_warehouse:
_tag_bucket(bucket, {"buckettype": "datawarehouse"})

except bucket.meta.client.exceptions.BucketAlreadyOwnedByYou:
except s3_resource.meta.client.exceptions.BucketAlreadyOwnedByYou:
log.warning(f'Skipping creating Bucket {bucket_name}: Already exists')
return

Expand Down
11 changes: 11 additions & 0 deletions tests/api/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ def test_create_bucket(logs_bucket, s3):

aws.create_bucket(bucket_name, is_data_warehouse=True)

# Check versioning.
assert bucket.Versioning().status == "Enabled"

# Check lifecycle.
versioning = bucket.LifecycleConfiguration()
rule = versioning.rules[0]
assert rule["ID"].endswith("_lifecycle_configuration")
assert rule["Status"] == "Enabled"
assert rule["NoncurrentVersionTransitions"][0]["NoncurrentDays"] == 30
assert rule["NoncurrentVersionTransitions"][0]["StorageClass"] == "GLACIER"

# Check logging
assert bucket.Logging().logging_enabled['TargetBucket'] == settings.LOGS_BUCKET_NAME
# Check tagging
Expand Down

0 comments on commit c12272d

Please sign in to comment.