Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorporate existing AWS resources into a CloudFormation stack

Is there a way to incorporate existing AWS resources that were created outside of CloudFormation into an existing CloudFormation stack? I'd like to do this without having to add a new resource in the CloudFormation stack and migrate the existing resource's data over to that new resource. I see that AWS now has drift detection for CloudFormation stacks. I'm wondering if that might be able to be leveraged to incorporate existing resources into a stack.

like image 634
BrianP Avatar asked Jan 29 '19 18:01

BrianP


People also ask

How do you create a CloudFormation from an existing resource?

Create a stack from existing resources using the AWS Management Console. 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 Create stack, and then choose With existing resources (import resources).

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.


4 Answers

The ability to import/adopt resources into an existing CloudFormation stack is the #1 ask from CloudFormation customers. We've been thinking about ways to do it for a while, but haven't hit upon the mechanism that both fits customer needs and works at the scale the service operates.

Since we don't expose stack state info anywhere outside the service for you to modify, the only approach you can take until we offer an adoption feature is to either store metadata about the resources in a parameter store, or use a custom resource as a wrapper to retrieve the information about the underlying resource and then surface it to your stack via Fn::GetAtt.

like image 59
Chuck Meyer Avatar answered Oct 19 '22 23:10

Chuck Meyer


Now you finally can do it with Resource Import feature, references:

https://github.com/aws/aws-sdk-js/blob/master/CHANGELOG.md

https://twitter.com/shortjared/status/1193985448164691970?s=21

like image 37
gvasquez Avatar answered Oct 19 '22 23:10

gvasquez


You can do this by passing existing resource information to your stack via Parameters. Here is an example of how to pass these parameters to the stack.

Check out this blog post from Eric Hammond describing how you can incorporate these parameters into the rest of the stack. The use-case described is a bit different in that they are optionally creating new resources if they aren't passed in, but the overall structure applies to the case you've described.

In this case I don't think Drift Detection will help you, since it will show differences between deployed resources and the configuration described in a stack. Resources defined/created outside of the stack won't be checked.

like image 3
bwest Avatar answered Oct 19 '22 21:10

bwest


Amazons CDK (currently in the stage of developer preview as of writing) offers a way to do that:

If you need to reference a resource, such as an Amazon S3 bucket or VPC, that's defined outside of your CDK app, you can use the Xxxx.import(...) static methods that are available on AWS constructs. For example, you can use the Bucket.import() method to obtain a BucketRef object, which can be used in most places where a bucket is required. This pattern enables treating resources defined outside of your app as if they are part of your app.

Source: https://docs.aws.amazon.com/CDK/latest/userguide/aws_construct_lib.html

It also allows to import existing CloudFormation templates: https://docs.aws.amazon.com/CDK/latest/userguide/use_cfn_template.html

like image 3
karfau Avatar answered Oct 19 '22 22:10

karfau