Logo

dev-resources.site

for different kinds of informations.

Move aws resources from one stack to another cloudformation stack

Published at
7/2/2024
Categories
aws
guide
serverless
cloudformation
Author
bhavin03
Author
8 person written this
bhavin03
open
Move aws resources from one stack to another cloudformation stack

Why do we need this?

  • The AWS CloudFormation resource limit is currently set at 500, although this size may increase with the introduction of new features in Application.
  • To accommodate this limitation, we must distribute all resources across various stacks.
  • Our approach involves isolating Lambda functions into a separate stack, while other resources such as S3 buckets and DynamoDB tables reside in an infra stack.
  • This is the reason why we need to import resources from the main stack into the infra stack.

 

Steps to move resources from one stack to another stack

 

Prerequisites

Apply 'DeletionPolicy: Retain' to all resources of the main stack

  • Applying 'DeletionPolicy: Retain' to all resources in the main stack ensures that when these resources are deleted during stack updates or deletions, they are retained rather than being deleted permanently.
  • This is particularly useful for resources that contain valuable data or configurations that need to be preserved even if they are no longer actively used.

Consider you have two cloudformation stack(which is generated by serverless framework): main and destination, and you want to import some resources from main to destination. Here are the steps to move resources from one stack to another stack without deleting the actual resources.

  1. Copy AWS resources from the main cloudFormation stack and paste them into the destination cloudFormation stack.
  2. Remove resources from the main stack and deploy the main stack.
  3. Prepare another file named "resourcesToImport.txt" containing the AWS resource type, logical ID, and resource identifier.
  4. Run a command to create an IMPORT changeset.
  5. Execute a command to apply changeset which was created in the previous step.

 

1. Copy AWS resources from the main cloudFormation stack and paste them into the destination cloudFormation stack.

  • Copy destination stack cloudformation code into one file ( templateToImport.json)
  • Copy main stack resource’s ( which you want to import) cloudformation code and append them in destination stack code (templateToImport.json)

2. Remove resources from the main stack and deploy the main stack.

  • Now, remove all the resources which we want to import or we added into the destination stack in step 1 .
  • Redeploy main stack.

Now resources are not in any stack and also not deleted because resource’s deletionPolicy is set to Retain.

3. Prepare another file named "resourcesToImport.txt" containing the aws resource type, logical ID, and resource identifier.

Now, create One file named ‘resourcesToImport.txt’ and add ResourceType, LogicalResourceId and ResourceIdentifier for each resource which we want to import.

  • ResourceType will be the cloudformation resource type
  • LogicalResourceId will be the Logical Name of resource
  • ResourceIdentifier contains actual identifier of resource
    • If resource is S3 bucket then value will be {"BucketName": ""}
    • If resource is dynamodb table then value will be { "TableName": "ACTUAL_DYNAMODB_TABLE_NAME" }
    • If resource is rest api then value will be { "RestApiId": "REST_API_ID" }

Example File :



[
  {
    "ResourceType": "AWS::S3::Bucket",
    "LogicalResourceId": "<LOGICAL_NAME_OF_BUCKET>",
    "ResourceIdentifier": {
      "BucketName": "<ACTUAL_NAME_OF_BUCKET>"
    }
  },
  {
    "ResourceType": "AWS::DynamoDB::Table",
    "LogicalResourceId": "<LOGICAL_NAME_OF_DYNAMODB_TABLE>",
    "ResourceIdentifier": {
      "TableName": "ACTUAL_NAME_OF_DYNAMODB_TABLE"
    }
  },
  {
    "ResourceType": "AWS::ApiGateway::RestApi",
    "LogicalResourceId": "<LOGICAL_NAME_OF_RESTAPI>",
    "ResourceIdentifier": {
      "RestApiId": "REST_API_ID"
    }
  }
]


Enter fullscreen mode Exit fullscreen mode
  1. Run a command to create IMPORT changeset

below command creates import changeset of resource



aws cloudformation create-change-set --stack-name <YOUR_STACK_NAME> --change-set-name <CHANGE_SET_NAME> --change-set-type IMPORT --resources-to-import file://resourcesToImport.txt --template-body file://templateToImport.json --capabilities CAPABILITY_NAMED_IAM --description "write here description" --profile <AWS_PROFILE>


Enter fullscreen mode Exit fullscreen mode

5. Execute a command to apply the changeset.

below command executes the import changeset and resources will be move from main stack to destination stack 🥳



aws cloudformation execute-change-set --change-set-name <CHANGE_SET_NAME> --stack-name <YOUR_STACK_NAME> --profile <AWS_PROFILE>


Enter fullscreen mode Exit fullscreen mode

Image description

 

👉 NOTE : Cloudformation doesn’t allow to import all types of resources. Few resources are not supported to import.

Below link contains all the resources which are allowed to import in cloudformation stack

Resource type support - AWS CloudFormation

 

Reference

Importing existing resources into a stack - AWS CloudFormation

cloudformation Article's
30 articles in total
Favicon
Thrilled to Announce the Launch of My Book "Mastering Infrastructure as Code with AWS CloudFormation"
Favicon
[Solved] AWS Resource limit exceeded
Favicon
A Comparative Analysis of Terraform and CloudFormation
Favicon
AWS CDK Typescript Simple Project for Cloud Formation of Resources Required for Kubernetes Study
Favicon
Customize VPCs with CloudFormation Conditions
Favicon
AWS CloudFormation: Infrastructure as Code for Efficient Cloud Management
Favicon
Using CloudFormation to deploy a web app with HA
Favicon
Automated Control Rollout in AWS Control Tower
Favicon
Launch an EC2 instance in a custom-made VPC using cloud formation
Favicon
AWS Automatically Accept Transit Gateway Attachments for allowed CIDR and Account pairs
Favicon
AWS CloudFormation Git sync now allows you to review your stack changes via Pull Request (PR)
Favicon
Terraform vs. AWS CloudFormation: A Detailed Comparison
Favicon
Terraform vs CloudFormation: Choosing the Best IaC Tool
Favicon
Automating AWS Cost and Usage Report with CloudFormation
Favicon
Calling All Senior DevOps Trailblazers!
Favicon
Move aws resources from one stack to another cloudformation stack
Favicon
Amazon CloudFormation Custom Resources Best Practices with CDK and Python Examples
Favicon
Domesticate AWS nested stacks in Java: doing the chores Cloudformation doesn't do (w/ code samples)
Favicon
Please stop publishing AWS S3 buckets as static websites! Read here for a secure, fast, and free-ish approach [1st episode]
Favicon
App runner with CloudFormation AWS (json, nodejs, java )
Favicon
Introducing AWS CloudFormation
Favicon
Simple steps to create AWS EKS Cluster and Nodes
Favicon
Deep Dive on Amazon Managed Workflows for Apache Airflow Using CloudFormation
Favicon
Importing CloudFormation Resources to help fix deployments to Production
Favicon
Update Github token in Codepipeline with Cloudformation
Favicon
Integration of Chatbot(Amazon Lex) in a static website (Hosted on S3 and cloud front)
Favicon
AWS CloudFormation - Automating Cloud Infrastructure
Favicon
Creating an AWS Auto Scaling Architecture with a monitoring dashboard
Favicon
Terraform vs. AWS CloudFormation
Favicon
Use AWS StepFunctions for SSM Patching Alerts

Featured ones: