Successfully import an S3 bucket as an AWS CloudFormation stack
Ram Kandaswamy, Amazon Web Services
Summary
This pattern helps you bring an existing Amazon Simple Storage Service (Amazon S3)
Settings that you haven't added to the template keep their current values and remain managed outside CloudFormation.
Prerequisites and limitations
Prerequisites
An active AWS account.
An existing S3 bucket (not managed by CloudFormation) and S3 bucket properties like S3 policy, encryption settings etc. For more information about this, see What S3 bucket policy should I use to comply with the AWS Config rule s3-bucket-ssl-requests-only
in the AWS Knowledge Center. An existing AWS Key Management Service (AWS KMS) key and its alias. For more information about this, see Working with aliases
in the AWS KMS documentation.
Architecture

The diagram shows the following workflow:
You create a JSON- or YAML-formatted AWS CloudFormation template.
The template creates an AWS CloudFormation stack to import the S3 bucket.
The AWS CloudFormation stack manages the S3 bucket that you specified in the template.
Technology stack
AWS CloudFormation
AWS Identity and Access Management (IAM)
AWS KMS
Amazon S3
Tools
AWS CloudFormation
– AWS CloudFormation helps you to create and provision AWS infrastructure deployments predictably and repeatedly. AWS Identity and Access Management (IAM)
– IAM is a web service for securely controlling access to AWS services. AWS KMS
– AWS Key Management Service (AWS KMS) is an encryption and key management service scaled for the cloud. Amazon S3
– Amazon Simple Storage Service (Amazon S3) is an object storage service that helps you store, protect, and retrieve any amount of data.
Epics
| Task | Description | Skills required |
|---|---|---|
Create a template to import the S3 bucket and KMS key. | On your local computer, create a template to import your S3 bucket and KMS key by using the following sample template:
The | AWS DevOps |
Create the stack. |
| AWS DevOps |
Create the KMS key alias. |
For more information about this, see AWS CloudFormation stack updates | AWS DevOps |
Update the stack to include the S3 bucket policy. |
Note This S3 bucket policy has a deny statement that blocks requests that don't use HTTPS (Secure Transport). | AWS DevOps |
Additional bucket properties. | Modify the template to add the properties that the bucket already has, such as block public access settings, versioning, and logging. | AWS administrator |
Update the key policy. |
For more information, see Key policies in AWS KMS | AWS administrator |
Add resource-level tags. |
| AWS DevOps |
Best practices
Record the current configuration before you import. Run commands such as
aws s3api get-bucket-encryption,get-public-access-block,get-bucket-versioning,get-bucket-logging, andget-bucket-policy, and save the output. Then make each layer of the template match the existing values exactly. An import operation fails if it would create, update, or delete a resource.Retain imported resources. Set
DeletionPolicy: RetainandUpdateReplacePolicy: Retainon the bucket and the AWS KMS key. Then deleting the stack, or an update that requires replacement, can't remove the bucket, its data, or the key.Add one layer for each stack update. Review the change set before you run it. Confirm that it changes only the resource or property you're adding.
Run drift detection after the import and after each layer. Drift detection reports only on properties the template declares. A result of
IN_SYNCconfirms that the template matches the bucket's current configuration.Bring security settings under AWS CloudFormation management early. Add the bucket's existing S3 Block Public Access, versioning, server access logging, and encryption settings as layers. After that, drift detection reports any change made outside of CloudFormation.
Preserve existing bucket policy statements. CloudFormation replaces the entire bucket policy, so copy every existing statement into the template before you add the
AWS::S3::BucketPolicyresource.Match the key policy exactly and keep the account root statement. Keys created in the console often have additional statements. If you remove the statement that grants the account root access, the key can become unmanageable.
Write policies as structured objects. Write
PolicyDocumentandKeyPolicyas YAML or JSON objects instead of JSON strings. This lets tools such as AWS CloudFormation Guard and cfn-lint check them before deployment.Protect the stack after import. Turn on termination protection, and consider a stack policy that prevents updates to the bucket resource.
Test in a non-production account first. Run the full import and every layer against a test bucket before you apply them to production.