Replace CloudFormation template path from local to S3

0

Hi,

I have a template created by using this command:

aws cloudformation create-stack \
  --stack-name YourStackName \
  --template-body file://path/to/your-template-file.yaml \
  --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
  --region us-east-1  # Change to your desired region

Now I want to use S3 as backend and I don't want to delete and recreate a stack, I just want to change backend. This command will show me the stack with this name already exists:

 aws cloudformation create-stack \
      --stack-name YourStackName \
      --template-url "https://s3.amazonaws.com/templates/your-template-file.yaml \
      --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
      --region eu-west-1  # Change to your desired region

Is there any way to migrate backend to S3 or do I need to recreate a stack with a different name?

Thanks

4 Answers
0
Accepted Answer

If you just want to switch the template from a local file to an object in S3 on an existing stack, use the update-stack operation instead of the create-stack operation.

profile pictureAWS
EXPERT
kentrad
answered 7 months ago
  • When using CLI I have this error: An error occurred (ValidationError) when calling the UpdateStack operation: No updates are to be performed. Maybe it will work when I will change some small part or add something.

  • Yes, if they are the exact same template, you will get that error.

0

Hello. To migrate the backend of an AWS CloudFormation stack to use an S3 bucket, you typically need to recreate the stack with a different name. CloudFormation stack names are unique within an AWS account and region, so you cannot have two stacks with the same name in the same region.

Best regards, Andrii

profile picture
EXPERT
answered 7 months ago
0

Hello,

For using the same Cloud formation stack, you can use the deletion policy in the first stack and remove the resource from your CFT and provision the stack, since the deletion policy is applied the resources created by the CFT would be detached from the stack and would remain in the AWS.

use the same copy of the CFT which was there in local in s3 and provision the resource, this would import the existing resource which was there. then you can update your s3 CFT for any update operation in the resources.

By using this method, you can shift/migrate the resources to different CFTs without deleting it.

profile picture
AGS
answered 7 months ago
0

When you use the command you listed to create a stack from the "local" file the CLI is actually first uploading your template file to S3 and then creating the stack. Therefore your current stack is already "using s3". Once a stack is deployed it does not maintain any link to the source file that created it, it keeps a copy of the template in the CloudFormation service, which is what you see when you view the Template tab in the console. It is not showing you the file locally or even the file in S3. So for now you have no need to take any action. If you find a change you need to make in the future at that point you can do a update-stack using an S3 location but there is no need until that time.

I hope this makes sense.

profile pictureAWS
danjhd
answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions