AWS CDK: How to rename existing stacks without losing the ability to update? - amazon-web-services

I've created and deployed a production stack:
const app = new cdk.App();
const stack = new cdk.Stack(app, 'myorg-production-api');
Now It's important to change the naming schema, and I need this stack to be myorg-api-production-root.
Of course, I can do it by manually destroying the old one, and deploying the new one (both can't exist at the same time, because of the "singleton" resources like DNS records), but this means a huge downtime, which I need to avoid at all cost.
Can I somehow intervene into CDK's stack identity determination process and change stack names without the downtime?

No, it's not possible to change an existing stack's name, either with or without the CDK.
Under the hood, cdk deploy sets the stack name with the CloudFormation CreateChangeSet API (then ExecuteChangeSet) on create. The stack name is a unique, immutable id in CloudFormation.

Related

When updating a CloudFormation stack, what happens to any code or data that's stored within the stack?

I'm currently learning AWS CloudFormation, and I came across the update feature that CloudFormation has. My understanding is that since CloudFormation is infrastructure as a code, updating a stack in CloudFormation will update the infrastructure within the stack (i.e. changing an EC2 instance from T2.nano to T2.micro). However, I couldn't find any information on what happens to anything that's stored in the stack. For example, if you have a bucket stored in S3 Standard and you update the stack to have the bucket in S3 Infrequent Access instead, does this move the bucket into Infrequent Access or does this make a copy of the bucket in the new Infrequent Access and delete the original? Would this be similar to other services within the stack (such as changing an EC2 instance from T2.nano to T2.micro affecting any configurations done within the EC2 instance)?
Change of InstanceType will lead to "some interruptions" as explained in the AWS::EC2::Instance.
This means that your instance will be stopped, its type will be changed and started again. There is no replacement, so all your data will be preserved.

CloudFormation resources not updated on regular deployment

Our team had an issue where someone manually modified an IAM role for an operational event. We hoped that a CloudFormation stack redeployment would revert the state of the IAM role, however, the manual change was still there.
My working theory is that since the IAM role arn is the same, that CloudFormation does not delete and recreate it. Is that accurate? And if so, how do we ensure all relevant resources are torn down during a deployment?
My working theory is that since the IAM role arn is the same, that CloudFormation does not delete and recreate it. Is that accurate?
CloudFormation (CFN) does not check for any changes made outside of its control. You could remove the role, and CFN would still "think" that the role is there.
If you change a resource created by CFN manually outside of CFN (bad practice), you have so called a stack drift. CFN by itself is not aware of any changes made to resources it creates that occurred outside its control. But, CFN provides special tools which you have to explicitly call to detect the drift:
Detect drift on an entire CloudFormation stack
Not all resources support drift detection, but AWS::IAM::Role is one which does.
And if so, how do we ensure all relevant resources are torn down during a deployment?
Not sure what do you mean here. But you have to manually fix the drift. You have four choices:
Change the role back to its original state,
Update template to reflect the external changes,
Use import to import the changed role to stack,
Delete the entire stack, and create new one from the original template.
The last choice ensures that the modified role is also deleted and recreated in its original form.

AWS CloudFormation for update (rerun?) of downstream nested Stacks

I'm trying to do the following: the parent Stacks launches the first child Stack which creates a fully configured EC2 instance. Once that is completed, the parent Stack kicks off a second Stack that uses a Lambda function to create an AMI, which is then used for an AutoScaling setup even further downstream. This is working perfectly.
Now the challenge: when I update the metadata for the EC2 instance from the first child Stack I would really like the second Stack to be triggered. In other words: I want to be able to change the seed instance and have the CloudFormation Stack update, creating a new AMI.
I'm able to get the seed instance to update, but the second child Stack isn't triggered :-(
I've Google everything I could think of, but Update Policy doesn't apply, manually kicking off the second child is defying the point of having nested Stacks and I'm pretty sure I'm missing some obvious feature or clever trick, so I'm asking you guys to help me out. Please.
Have you tried using Lambda-backed custom resource? You can have the service token of resource as the Lambda and use DependsOn with the first nested stack. It will kick off whenever the CF script runs or updates.
You can also lookup the stack itself from the Lambda function to determine any changes if you want.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html

Moving resources between CloudFormation stacks

Lets imagine that i have a cloudformation stack with 2 EC2 instances 1 S3 bucket and 1 dynamoDb table.
Everything is good with this stack until we decide to do some refactoring and split this stack into two stacks. Stack A that has only 2 EC2 instances and Stack B that has 1 S3 bucket and 1 dynamoDb table.
My question is: what is the best way to do that without destroying and re creating the resources or doing migration? (i am looking for a solution similar to terraform move state).
Could you please share how you organise your stacks and how you refactor them.
Moving resources between stacks is now supported for some resources including DynamoDb, EC2 instances and S3 buckets. The aws docs at
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/refactor-stacks.html give the full detail and instructions starting with:
Using the resource import feature, you can move resources between, or refactor, stacks. You need to first add a Retain deletion policy to the resource you want to move to ensure that the resource is preserved when you remove it from the source stack and import it to the target stack.
Important
Not all resources support import operations. See Resources that
Support Import Operations before you remove a resource from your
stack. If you remove a resource that doesn't support import operations
from your stack, you can't import the resource into another stack or
bring it back into the source stack.
Currently, there is no mechanism in CloudFormation to move objects/resources from 1 stack to another.
If you had used Terraform to deploy your resources, then yes, you could use terraform state mv to move resources from one state file to a different state file. A Terraform state file is associated with a single Terraform deployment, so you you could think of it as being analogous to how CloudFormation stores its version of what's deployed in a stack.
Note that Terraform state moves are not the simplest things in the world and AFAIK there is no GUI interface to assist with moves. Make backups and set aside time for testing.
There is, however, a project called Terraboard that is a GUI that can help you inspect the state file.

Is it possible to re-create AWS resources using CloudFormation?

Lets say an AWS stack was created using CloudFormation.
Now one of those resources was modified outside CloudFormation.
1) Is it possible to have CloudFormation specifically create those resources? Based on my understanding, we can't do that because CloudFormation does not identify a difference, and so does not create the modified resources. Is my observation correct?
2) Also, what options do I have to revert a stack to its original state, if modified outside CloudFormation?
This is one possible hack you could use without deleting the entire stack.
From the template remove the specific resource which got deleted accidentally.
Now update the stack which makes your stack and resources in your account in sync.
Revert the template to its state before step1 and update again which will create the resource which got deleted accidentally.
Unfortunately the answer for both your questions is NO.
If you modify the resources in the stack after stack creation status is COMPLETE, there is nothing CF can do since it doesn't keep track of modification to resources
You have no option other than deleting the current stack and create a new one
First, beware that modifying CloudFormation-created resources outside of CloudFormation is explicitly discouraged, according to AWS CloudFormation Best Practices:
Manage All Stack Resources Through AWS CloudFormation
After you launch a stack, use the AWS CloudFormation console, API, or AWS CLI to update resources in your stack. Do not make changes to stack resources outside of AWS CloudFormation. Doing so can create a mismatch between your stack's template and the current state of your stack resources, which can cause errors if you update or delete the stack.
However, if you've modified a CloudFormation-managed resource accidentally and need to recover, you may have some limited options beyond simply deleting and re-creating the stack altogether (which may not be an acceptable option):
It is not possible for CloudFormation to automatically update its internal state based on the current state of an externally-modified resource.
However, depending on the exact resource type, in some cases you can manually update CloudFormation afterwards by applying a stack update that matches the current state of the resource.
Similarly, it is not possible for CloudFormation to automatically revert an externally-modified resource back to its original unmodified CloudFormation state.
However, depending on the exact resource type, in some cases you can either:
Revert a resource by manually updating the resource back to its original state;
Update the resource by applying a stack update, bringing both the CloudFormation stack and the managed resource to an altogether new state that will once again be in sync.
To force the EC2 re-creating, I do use a simple trick, when I'm deploying, I jump between AMI's IDs (I took two similar AMI's ID), that had helped me when I'm testing user data or things that I want to test during the EC2 bootstrap. Again, it just works for EC2.
Unfortunately, the answer is NO
if you made changes in the stack after the creation, Cloudformation can't track those changes.
if you need to revert those changes, you must delete the stack and rebuild.