gpt4 book ai didi

node.js - 将变量从 NodeJS 运行时传递回 CloudFormation

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

我有一个 CF 模板,它使用 Javascript 中的无服务器函数来实例化 AWS Connect 实例,因为 Connect 没有实际的 CF 资源。我使用环境变量将 CF 中的值传递到内联 Javascript 中。现在我需要相反的操作:从 JS 中提取其中一些值并将它们传递回 CF 模板的其他部分。那么,问题是,如何将变量从 Javascript 传递到 CloudFormation?我怀疑我需要使用 Systems Manager 参数存储之类的东西,但也许有更简单的方法?具体来说,我需要以下代码片段中的值“serviceRole”可传递给其他 CF 资源:

CreateConnectInstance:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs12.x
Description: Invoke a function to create an AWS Connect instance.
MemorySize: 128
Timeout: 8
Role: !GetAtt LambdaExecutionRole.Arn
Tracing: Active
Layers:
- !Sub "arn:aws:lambda:us-east-1:${AWS::AccountId}:layer:node_sdk:1"
Environment:
Variables:
IdentityManagementType:
Ref: IdentityManagementType
InboundCallsEnabled:
Ref: InboundCallsEnabled
InstanceAlias:
Ref: InstanceAlias
OutboundCallsEnabled:
Ref: OutboundCallsEnabled
Region:
Ref: Region
#Optional Values
ClientToken: !If
- HasClientToken
- !Ref ClientToken
- !Ref "AWS::NoValue"
DirectoryId: !If
- HasClientToken
- !Ref ClientToken
- !Ref "AWS::NoValue"

InlineCode: |
var AWS = require('aws-sdk');
var response = require('cfn-response');
var connect = new AWS.Connect({apiVersion: '2017-08-08', region: process.env.Region});
exports.handler = (event, context) => {
console.log("Request Received:\n" + JSON.stringify(event));
var instanceId;
var serviceRole;
var isInboundCallsEnabled = (process.env.InboundCallsEnabled == 'true');
var isOutboundCallsEnabled = (process.env.OutboundCallsEnabled == 'true');
var params = {
InboundCallsEnabled: isInboundCallsEnabled,
OutboundCallsEnabled: isOutboundCallsEnabled,
IdentityManagementType: process.env.IdentityManagementType,
ClientToken: process.env.ClientToken,
DirectoryId: process.env.DirectoryId,
InstanceAlias: process.env.InstanceAlias
};
connect.createInstance(params, function (err, data) {
if (err) {
let responseData = { Error: "Create Instance Failed" };
console.log(responseData.Error + ":\n", err);
response.send(event, context, "FAILED", responseData);
instanceId = data.Id;
}
else {
console.log("Connect Instance Creation Successful");
console.log(JSON.stringify(data));
response.send(event, context, "SUCCESS", data);
}
});
connect.describeInstance({InstanceId: instanceId}, function (err,data) {
console.log(JSON.stringify(data));
serviceRole = data.Instance.ServiceRole; // ***NEED TO EXTRACT THIS VALUE to CF***
});
}

它在同一模板中通过以下方式调用:

InvokeLambda:
Type: AWS::CloudFormation::CustomResource
DependsOn: CreateConnectInstance
Version: "1.0"
Properties:
ServiceToken: !Sub ${CreateConnectInstance.Arn}

最佳答案

向 CloudFormation 发送“SUCCESS”响应时,您可以使用 Data响应对象的字段,用于将服务角色作为键值对传递。这样,在自定义资源之后创建的所有资源都可以使用 Fn::GetAtt 访问此信息。 。来自 docs :

After getting a SUCCESS response, AWS CloudFormation proceeds with thestack operation. If a FAILED response or no response is returned, theoperation fails. Any output data from the custom resource is stored inthe pre-signed URL location. The template developer can retrieve thatdata by using the Fn::GetAtt function.

关于node.js - 将变量从 NodeJS 运行时传递回 CloudFormation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65313337/

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