简体   繁体   English

在 AWS CDK 中使用 AWS SDK 的正确方法

[英]Correct way to use AWS SDK within AWS CDK

I'm trying to use the SDK within my CDK application.我正在尝试在我的 CDK 应用程序中使用 SDK。 My stack is creating a directory, some networking stuff, then some instances which it joins to the domain using a PowerShell script which requires the AD DNS IPs, I'm currently using it like so:我的堆栈正在创建一个目录,一些网络内容,然后是一些使用需要 AD DNS IP 的 PowerShell 脚本加入域的实例,我目前正在使用它,如下所示:

const ds = new DirectoryService();

const result = new Promise(function (resolve: (value: string) => any, reject) {
        ds.describeDirectories({}, function (err, data) {
            if (err){
            reject(data)
        }else{
            try{
                if (data.DirectoryDescriptions){
                    if (data.DirectoryDescriptions[0].DnsIpAddrs){
                        resolve(data.DirectoryDescriptions[0].DnsIpAddrs.toString())
                    }
                }
            }catch (e) {
                reject("Directory doesn't exist yet.")
            }
        }
    })
});
result.then(value => {
    const subs = {
        "#{DNS_ADDRESSES}": value,
        "#{SECRET_ID}": directory.directorySecret.secretArn
    };

    Object.entries(subs).forEach(
        ([key, value]) => {
            domainJoinScript = domainJoinScript.replace(key, String(value));
        }
    );

    new  Stack(app, 'instances', networking.vpc, domainJoinScript);

}).catch(error => {
    print(error)
});

Now this works, but it's far from a clean solution.现在这可行,但这远不是一个干净的解决方案。 My new Stack has multiple resources within it which means I have to pass the result of the SDK call through several levels, instead of just directly where it's needed, and if I had to make multiple SDK calls this would get even messier.我的新堆栈中有多个资源,这意味着我必须通过多个级别传递 SDK 调用的结果,而不是直接传递到需要的地方,如果我必须进行多个 SDK 调用,这会变得更加混乱。

The core problem is the AWS SDK being purely asynchronous in JS, which means I have to use the fairly verbose pattern above to wrap it.核心问题是 AWS SDK 在 JS 中纯粹是异步的,这意味着我必须使用上面相当冗长的模式来包装它。

Does anyone have a better way to do this?有没有人有更好的方法来做到这一点?

You could implement the AWS SDK calls using a AwsCustomResource您可以使用AwsCustomResource实施 AWS 开发工具包调用

This allows calling AWS SDK functions on different Resource actions (oncreate, ondelete, onupdate)这允许在不同的资源操作(oncreate、ondelete、onupdate)上调用 AWS 开发工具包函数

The result data can be consumed by calling getData.结果数据可以通过调用getData来消费。 This does further allow the cdk created CloudFormation Template to work without a client to run it.这确实进一步允许 cdk 创建的 CloudFormation 模板在没有客户端运行的情况下工作。

One Example where I used this is the following:我使用它的一个例子如下:

const userPoolDomainDescription = new customResources.AwsCustomResource(this, 'user-pool-domain-description', {
  onCreate: {
    physicalResourceId: 'user-pool-domain-description',
    service: 'CognitoIdentityServiceProvider',
    action: 'describeUserPoolDomain',
    parameters: {
      Domain: userPoolDomain.domain
    }
  }
});

const dnsName = userPoolDomainDescription.getData('DomainDescription.CloudFrontDistribution').toString();

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

相关问题 这是在 JavaScript SDK 中使用 AWS 全局配置的正确方法吗? - Is this the correct way to use the AWS global config in the JavaScript SDK? 我们可以使用 GitLab 作为 AWS CDK 管道源代码的主机吗? - Can we use GitLab as host for source code with AWS CDK pipeline? 如何使用 AWS CDK 为带有别名的 Lambda 设置 EventBridge 规则目标 - How to use AWS CDK to set EventBridge Rule Target for Lambda with Alias 如何使用 AWS CDK 查找现有的 ApiGateway - How to use AWS CDK to look up existing ApiGateway 在AWS Javascript SDK中,是否可以测试凭证? - In the AWS Javascript SDK, is there a way to test credentials? AWS CDK 用户池授权者 - AWS CDK user pool authorizer AWS SDK JavaScript v3 / 如何在 dynamoDB 扫描命令中使用 ExpressionAttributeNames? - AWS SDK JavaScript v3 / How to use ExpressionAttributeNames within dynamoDB Scan Command? 如何获得AWS上的凭证? 我必须使用AWS Cognito SDK还是AWS SDK? - How do I get credentials on AWS? Must I use AWS Cognito SDK or AWS SDK? 是否可以在 nodeJS aws sdk 中使用正则表达式? - Is it possible to use Regex expressions in the nodeJS aws sdk? 如何安全地使用aws-sdk javascript - how to securely use aws-sdk javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM