简体   繁体   English

将现有 AWS 资源合并到 CloudFormation 堆栈中

[英]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?有没有办法将在 CloudFormation 之外创建的现有 AWS 资源合并到现有的 CloudFormation 堆栈中? 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.我想这样做而不必在 CloudFormation 堆栈中添加新资源并将现有资源的数据迁移到该新资源。 I see that AWS now has drift detection for CloudFormation stacks.我看到 AWS 现在对 CloudFormation 堆栈进行了漂移检测。 I'm wondering if that might be able to be leveraged to incorporate existing resources into a stack.我想知道是否可以利用这将现有资源合并到堆栈中。

The ability to import/adopt resources into an existing CloudFormation stack is the #1 ask from CloudFormation customers.将资源导入/采用到现有 CloudFormation 堆栈的能力是 CloudFormation 客户的第一要求。 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.由于我们不会在服务之外的任何地方公开堆栈状态信息供您修改,因此在我们提供采用功能之前您可以采取的唯一方法是将有关资源的元数据存储在参数存储中,或者使用自定义资源作为包装器来检索有关底层资源的信息,然后通过 Fn::GetAtt 将其显示到您的堆栈中。

You can do this by passing existing resource information to your stack via Parameters .您可以通过将现有资源信息通过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.查看 Eric Hammond 的这篇博客文章,其中描述了如何将这些参数合并到堆栈的其余部分中。 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.在这种情况下,我认为 Drift Detection 不会帮助您,因为它会显示已部署资源与堆栈中描述的配置之间的差异。 Resources defined/created outside of the stack won't be checked.不会检查在堆栈外定义/创建的资源。

Amazons CDK (currently in the stage of developer preview as of writing) offers a way to do that: Amazons CDK (目前在撰写本文时处于开发人员预览阶段)提供了一种方法:

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.如果您需要引用在 CDK 应用程序外部定义的资源,例如 Amazon S3 存储桶或 VPC,您可以使用 AWS 构造中提供的 Xxxx.import(...) 静态方法。 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.比如可以通过Bucket.import()方法获取一个BucketRef对象,在大部分需要bucket的地方都可以使用。 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来源: 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它还允许导入现有的 CloudFormation 模板: https ://docs.aws.amazon.com/CDK/latest/userguide/use_cfn_template.html

Importing existing resources to stacks is now supported by CloudFormation : CloudFormation 现在支持将现有资源导入堆栈:

Announcement from AWS : AWS CloudFormation Launches Resource Import AWS 公告: AWS CloudFormation 推出资源导入

Instructions Via an example : HERE说明通过一个例子: 这里

Cloudformer might help you to create a new stack from existing resources and then you can add more resources to the stack. Cloudformer可能会帮助您从现有资源创建一个新堆栈,然后您可以向该堆栈添加更多资源。 But don't know of a way to "merge" an existing stack with existing resources outside the stack.但是不知道有什么方法可以将现有堆栈与堆栈外的现有资源“合并”。

Im my case I needed to import an ARN value from an existing SAM output in my account, so that I could add the proper invoke policy in my new stack.在我的情况下,我需要从我账户中现有的 SAM 输出中导入一个 ARN 值,以便我可以在我的新堆栈中添加正确的调用策略。

I was looking for an equivalent of SAM's Fn::ImportValue , and found out that the core module has a static Fn.importValue method you can use as such:我正在寻找等效于 SAM 的Fn::ImportValue ,并发现核心模块有一个静态Fn.importValue方法,您可以这样使用:

const cdk = require('@aws-cdk/core');
const lambda = require('@aws-cdk/aws-lambda')

class MyStack extends cdk.Stack {
    constructor(scope, id, props) {
        super(scope, id, props);

        // The below line did the trick
        const arn = cdk.Fn.importValue(`your-sam-function-export-name`)
        const myLambda = lambda.Function.fromFunctionArn(this, 'myLambda', arn)
        // ...
    }
}


Reference: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Fn.html参考: https : //docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.Fn.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM