Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS 'Bucket already exists' - how to "migrate" existing resources to CloudFormation?

We have already created some infrastructure manually and with terraform, including some s3 buckets. In the future I would like to use pure CloudFormation to define the infrastructure as code.

So I created a CloudFormation yaml definition which references an existing bucket:

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  TheBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-existing-bucket-name

When I try to apply it, execution fails, with CloudFormation stack event:

The following resource(s) failed to update: [TheBucket].
12:33:47 UTC+0200   UPDATE_FAILED   AWS::S3::Bucket TheBucket
  my-existing-bucket-name already exists

How can I start managing existing resources with CloudFormation without recreating them? Or is it impossible by design?

like image 657
geekQ Avatar asked May 17 '17 10:05

geekQ


People also ask

How do I import existing resources in CloudFormation?

Sign in to the AWS Management Console and open the AWS CloudFormation console at https://console.aws.amazon.com/cloudformation . On the Stacks page, choose the stack you want to import resources into. Choose Stack actions, and then choose Import resources into stack.

Is it possible to create an AWS CloudFormation template from existing AWS resources in your account?

AWS CloudFormer is a template creation tool and it creates AWS CloudFormation template from our existing resources in AWS account. We can select any supported AWS resources that are running in our account, and CloudFormer creates a template in an Amazon S3 bucket.

Can CloudFormation update existing resource?

Update with No Interruption. AWS CloudFormation updates the resource without disrupting operation of that resource and without changing the resource's physical ID. For example, if you update certain properties on an AWS::CloudTrail::Trail resource, AWS CloudFormation updates the trail without disruption.

How do I update an existing stack in AWS CloudFormation?

To update a AWS CloudFormation stack (console) In the AWS CloudFormation console , from the list of stacks, select the running stack that you want to update. In the stack details pane, choose Update. If you haven't modified the stack template, select Use current template, and then choose Next.


1 Answers

You should be able to import it by using the "Import resources into stack" option:

enter image description here

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-existing-stack.html

As the documentation explains, you should add a "DeletionPolicy": "Retain" attribute to the already existing resources in your stack.

like image 174
SebaGra Avatar answered Sep 21 '22 07:09

SebaGra