AWS 无服务器功能:无法访问 aws-sdk 对象的方法..."is not a function"
AWS Serverless Function: Can't access methods of aws-sdk objects..."is not a function"
我在 CloudFormation 模板中有一个无服务器函数,用于实例化 AWS Connect 实例。但是,我无法访问 aws-sdk 对象上的任何方法,抛出的错误是“connect.createInstance 不是函数”。当我在对象上调用 getOwnPropertyNames 时,它 returns 以下内容,而不是我尝试访问的方法:
[
'config',
'isGlobalEndpoint',
'endpoint',
'_事件',
'MONITOR_EVENTS_BUBBLE',
'CALL_EVENTS_BUBBLE',
'_originalConfig',
'_clientId'
]
作为测试,我实例化了一个 S3 对象并对该 returns 调用 getOwnPropertyNames 完全相同。我究竟做错了什么?我在这里遵循规范形式:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Connect.html#createInstance-property
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Parameters:
IdentityManagementType:
Description: The type of identity management for your Amazon Connect users.
Type: String
AllowedValues: ["SAML", "CONNECT_MANAGED", "EXISTING_DIRECTORY"]
Default: "SAML"
InboundCallsEnabled:
Description: Whether your contact center handles incoming contacts.
Type: String
AllowedValues: [true, false]
Default: true
InstanceAlias:
Description: The name for your instance.
Type: String
MaxLength: 62
OutboundCallsEnabled:
Description: Whether your contact center allows outbound calls.
Type: String
AllowedValues: [true, false]
Default: true
DirectoryId:
Description: Optional. The identifier for the directory, if using this type of Identity Management.
Type: String
ClientToken:
Description: Optional. The idempotency token. Used for concurrent deployments
Type: String
MaxLength: 500
Region:
Description: Region to place the AWS Connect Instance
Type: String
Default: us-east-1
#Handler for optional values
Conditions:
HasClientToken: !Not
- !Equals
- ""
- !Ref ClientToken
HasDirectoryId: !Not
- !Equals
- ""
- !Ref DirectoryId
Resources:
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: AWSConnect
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- connect:*
Resource: arn:aws:connect:*:*
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
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
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 params = {
IdentityManagementType: process.env.IdentityManagementType,
InboundCallsEnabled: process.env.InboundCallsEnabled,
OutboundCallsEnabled: process.env.OutboundCallsEnabled,
ClientToken: process.env.ClientToken,
DirectoryId: process.env.DirectoryId,
InstanceAlias: process.env.InstanceAlias,
};
var connect = new AWS.Connect({apiVersion: '2017-08-08'});
console.log(Object.getOwnPropertyNames(connect)); // ***Not what's expected***
var createConnectRequest = connect.createInstance(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
}); // ***connect.createInstance is not a function***
InvokeLambda:
Type: AWS::CloudFormation::CustomResource
DependsOn: CreateConnectInstance
Version: "1.0"
Properties:
ServiceToken: !Sub ${CreateConnectInstance.Arn}
AWS Connect createInstance
方法在当前与 AWS Lambda 一起部署的 AWS SDK 中不可用 Node.js runtime (v2.771.0 at the time of writing). It looks like the createInstance API 处于预览版中。
该功能是在 v2.797.0 中引入的。您可以在本地(在 Lambda 之外)验证这一点。我使用二分法验证了这一点,以测试 2.771.0 和当前 (2.809.0) 之间的 AWS SDK 版本以及以下简单代码:
const AWS = require('aws-sdk');
const connect = new AWS.Connect({apiVersion: '2017-08-08'});
connect.createInstance('fred');
changelog 显示 v2.797.0(除其他外):
feature: Connect: This release adds a set of Amazon Connect APIs to
programmatically control instance creation, modification, description
and deletion.
您可以upload a compatible version of the SDK(在您的 Lambda 程序包中或通过 Lambda 层)访问此函数。
我在 CloudFormation 模板中有一个无服务器函数,用于实例化 AWS Connect 实例。但是,我无法访问 aws-sdk 对象上的任何方法,抛出的错误是“connect.createInstance 不是函数”。当我在对象上调用 getOwnPropertyNames 时,它 returns 以下内容,而不是我尝试访问的方法: [ 'config', 'isGlobalEndpoint', 'endpoint', '_事件', 'MONITOR_EVENTS_BUBBLE', 'CALL_EVENTS_BUBBLE', '_originalConfig', '_clientId' ]
作为测试,我实例化了一个 S3 对象并对该 returns 调用 getOwnPropertyNames 完全相同。我究竟做错了什么?我在这里遵循规范形式:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Connect.html#createInstance-property
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Parameters:
IdentityManagementType:
Description: The type of identity management for your Amazon Connect users.
Type: String
AllowedValues: ["SAML", "CONNECT_MANAGED", "EXISTING_DIRECTORY"]
Default: "SAML"
InboundCallsEnabled:
Description: Whether your contact center handles incoming contacts.
Type: String
AllowedValues: [true, false]
Default: true
InstanceAlias:
Description: The name for your instance.
Type: String
MaxLength: 62
OutboundCallsEnabled:
Description: Whether your contact center allows outbound calls.
Type: String
AllowedValues: [true, false]
Default: true
DirectoryId:
Description: Optional. The identifier for the directory, if using this type of Identity Management.
Type: String
ClientToken:
Description: Optional. The idempotency token. Used for concurrent deployments
Type: String
MaxLength: 500
Region:
Description: Region to place the AWS Connect Instance
Type: String
Default: us-east-1
#Handler for optional values
Conditions:
HasClientToken: !Not
- !Equals
- ""
- !Ref ClientToken
HasDirectoryId: !Not
- !Equals
- ""
- !Ref DirectoryId
Resources:
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: AWSConnect
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- connect:*
Resource: arn:aws:connect:*:*
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
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
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 params = {
IdentityManagementType: process.env.IdentityManagementType,
InboundCallsEnabled: process.env.InboundCallsEnabled,
OutboundCallsEnabled: process.env.OutboundCallsEnabled,
ClientToken: process.env.ClientToken,
DirectoryId: process.env.DirectoryId,
InstanceAlias: process.env.InstanceAlias,
};
var connect = new AWS.Connect({apiVersion: '2017-08-08'});
console.log(Object.getOwnPropertyNames(connect)); // ***Not what's expected***
var createConnectRequest = connect.createInstance(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
}); // ***connect.createInstance is not a function***
InvokeLambda:
Type: AWS::CloudFormation::CustomResource
DependsOn: CreateConnectInstance
Version: "1.0"
Properties:
ServiceToken: !Sub ${CreateConnectInstance.Arn}
AWS Connect createInstance
方法在当前与 AWS Lambda 一起部署的 AWS SDK 中不可用 Node.js runtime (v2.771.0 at the time of writing). It looks like the createInstance API 处于预览版中。
该功能是在 v2.797.0 中引入的。您可以在本地(在 Lambda 之外)验证这一点。我使用二分法验证了这一点,以测试 2.771.0 和当前 (2.809.0) 之间的 AWS SDK 版本以及以下简单代码:
const AWS = require('aws-sdk');
const connect = new AWS.Connect({apiVersion: '2017-08-08'});
connect.createInstance('fred');
changelog 显示 v2.797.0(除其他外):
feature: Connect: This release adds a set of Amazon Connect APIs to programmatically control instance creation, modification, description and deletion.
您可以upload a compatible version of the SDK(在您的 Lambda 程序包中或通过 Lambda 层)访问此函数。