gpt4 book ai didi

amazon-web-services - 使用 AwsCustomResource 获取大量资源?

转载 作者:行者123 更新时间:2023-12-03 07:22:51 24 4
gpt4 key购买 nike

我需要一个组件来创建大量 CodeCommit 用户。 CloudFormation 不支持为 IAM 用户添加公共(public) SSH key ,因此我必须创建自己的 key 。 CDK 附带 AwsCustomResource,它负责创建处理所需 CloudFormation 事件的 Lambda。换句话说,我的代码将类似于:

import { User } from 'aws-cdk-lib/aws-iam';
import { AwsCustomResource } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';

export interface CodeCommitUserProps {
userName: string;
emailAddress: string;
sshPublicKey: string;
}

export class CodeCommitUser extends Construct {
constructor(scope: Construct, id: string, props: CodeCommitUserProps) {
super(scope, id);

const user = new User(this, 'codecommit-' + props.userName, {
userName: 'codecommit-' + props.userName,
path: '/git/users'
});
}

const custom = new AwsCustomResource(this, ... );
}

现在,如果我调用 new CodeCommitUser(...) 数百次,我会假设每个用户都会有一个 CloudFormation 事件 Lambda,即使它们全部相同。如果我需要自定义资源的多个副本,是否可以重用由 AwsCustomResource 创建的 Lambda?

最佳答案

I would assume that there will be one CloudFormation event Lambda per user, even if all of them are identical.

事实上,没有。无论 CodeCommitUser 被实例化多少次,CDK 都会创建单个 lambda 函数。 CDK 如何管理这个?在幕后,CDK 使用 SingletonFunction对于 AWSCustomResource 提供商(请参阅 github source )。单例函数保证被添加到堆栈中“一次且仅一次,无论该构造被声明为堆栈的一部分多少次”

Is there a way to reuse the Lambdas created by AwsCustomResource if I need multiple copies of the custom resource?

同样,重用会自动发生。您可以通过使用定义的多个 CodeCommitUsercdk Synth-ing 堆栈来向自己证明这一点。然后在 cdk.out 目录中查找输出的 CloudFormation 模板。该模板仅定义了一个 AWS::Lambda::Function 资源(假设您的应用程序在其他地方不使用 lambda)。

关于amazon-web-services - 使用 AwsCustomResource 获取大量资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70729034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com