Successfully import an S3 bucket as an AWS CloudFormation stack - AWS Prescriptive Guidance
Services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the AWS European Sovereign Cloud Region, see the AWS European Sovereign Cloud User Guide.

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) bucket under infrastructure as code (IaC) management without changing its current configuration. Importing a bucket together with its related resources in a single action can fail, because an import operation can't also create or update resources. Instead, you build the AWS CloudFormation stack in layers. You first import the bucket and its AWS Key Management Service (AWS KMS) key. Each stack update after that adds one more part of the bucket's configuration to the template, such as the key alias, bucket policy, key policy, and tags.

Settings that you haven't added to the template keep their current values and remain managed outside CloudFormation.

Prerequisites and limitations

Prerequisites 

Architecture

Workflow to use CloudFormation template to create a CloudFormation stack to import an S3 bucket.

The diagram shows the following workflow:

  1. You create a JSON- or YAML-formatted AWS CloudFormation template.

  2. The template creates an AWS CloudFormation stack to import the S3 bucket.

  3. 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

TaskDescriptionSkills 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:

AWSTemplateFormatVersion: 2010-09-09 Parameters: bucketName: Type: String Resources: S3Bucket: Type: 'AWS::S3::Bucket' DeletionPolicy: Retain Properties: BucketName: !Ref bucketName BucketEncryption: ServerSideEncryptionConfiguration: - ServerSideEncryptionByDefault: SSEAlgorithm: 'aws:kms' KMSMasterKeyID: !GetAtt - KMSS3Encryption - Arn KMSS3Encryption: Type: 'AWS::KMS::Key' DeletionPolicy: Retain Properties: Enabled: true KeyPolicy: !Sub |- { "Id": "key-consolepolicy-3", "Version": "2012-10-17", "Statement": [ { "Sid": "Enable IAM User Permissions", "Effect": "Allow", "Principal": { "AWS": ["arn:aws:iam::${AWS::AccountId}:root"] }, "Action": "kms:*", "Resource": "*" } ] } EnableKeyRotation: true

The kms:* statement for the account root. This statement matches the AWS default key policy, and it enables IAM policies to grant permission to use the key. If you remove or restrict this access, make sure that your current user still has access to the key. Removing the key policy entirely can lock you out of the key.

AWS DevOps

Create the stack.

  1. Sign in to the AWS Management Console, open the AWS CloudFormation console, choose View stack, choose Create stack, and then choose With existing resources (import resources).

  2. Choose Upload a template file and then upload the template file that you created earlier.

  3. Enter a name for your stack and configure the remaining options according to your requirements.

  4. Choose Create stack and wait for the stack’s status to change to IMPORT_COMPLETE.

AWS DevOps

Create the KMS key alias.

  1. On the AWS CloudFormation console, choose Stacks, choose the name of the stack that you created earlier, choose the Template pane, and then choose View in Designer.

  2. Add the following snippet to the Resource section of your template, and then choose Create stack and complete the wizard:

KMSS3EncryptionAlias: Type: 'AWS::KMS::Alias' DeletionPolicy: Retain Properties: AliasName: alias/S3BucketKey TargetKeyId: !Ref KMSS3Encryption

For more information about this, see AWS CloudFormation stack updates in the AWS CloudFormation documentation. 

AWS DevOps

Update the stack to include the S3 bucket policy.

  1. On the AWS CloudFormation console, choose Stacks, choose the name of the stack that you created earlier, choose the Template pane, and then choose View in Designer.

  2. Add the following snippet to the Resource section of the template, and then choose Create stack and complete the wizard:

S3BucketPolicy: Type: 'AWS::S3::BucketPolicy' Properties: Bucket: !Ref S3Bucket PolicyDocument: !Sub |- { "Version": "2008-10-17", "Id": "restricthttp", "Statement": [ { "Sid": "denyhttp", "Effect": "Deny", "Principal": { "AWS": "*" }, "Action": "s3:*", "Resource": ["arn:aws:s3:::${S3Bucket}","arn:aws:s3:::${S3Bucket}/*"], "Condition": { "Bool": { "aws:SecureTransport": "false" } } } ] }
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.

  1. On the AWS CloudFormation console, choose Stacks, choose the name of the stack that you created earlier, choose the Template pane, and then choose View in Designer.

  2. Modify the template’s KMS resource to include the key policy that allows administrators to administer the KMS key.

  3. Choose Create stack, choose Next, and then complete the wizard according to your requirements.

For more information, see Key policies in AWS KMS in the AWS KMS documentation.

AWS administrator

Add resource-level tags.

  1. On the AWS CloudFormation console, choose Stacks, choose the name of the stack that you created earlier, choose the Template pane, and then choose View in Designer.

  2. Add the following snippet to the Amazon S3 resource Properties section of the template, and then choose Create stack and complete the wizard:

Tags: - Key: createdBy Value: Cloudformation
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, and get-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: Retain and UpdateReplacePolicy: Retain on 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_SYNC confirms 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::BucketPolicy resource.

  • 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 PolicyDocument and KeyPolicy as 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.

Related resources