我有一个全局堆栈,其中有预定义的VPCs和子网。
export class GlobalStack extends InternalStack {
/**
* Shared VPC Instance
*/
public readonly vpc = Vpc.fromLookup(this, 'vpc', {
vpcId: process.env.VPC_ID!,
})
/**
* Private Subnet 1
*/
public privateSubnet1 = new Subnet(this, 'subnet-1', {
vpcId: this.vpc.vpcId,
cidrBlock: 'xx.x.x.x/xx',
availabilityZone: `${this.region}-1`,
})
/**
* Private Subnet 2
*/
public privateSubnet2 = new Subnet(this, 'subnet-2', {
vpcId: this.vpc.vpcId,
cidrBlock: 'xx.x.x.x/xx',
availabilityZone: `${this.region}-2`,
})
/**
* Private Subnet 3
*/
public privateSubnet3 = new Subnet(this, 'subnet-3', {
vpcId: this.vpc.vpcId,
cidrBlock: 'xx.x.x.x/xx',
availabilityZone: `${this.region}-3`,
})
public readonly apiGatewayVpcEndpoint = this.vpc.addInterfaceEndpoint(
'ApiGateway',
{
service: InterfaceVpcEndpointAwsService.APIGATEWAY,
subnets: {
subnets: [
this.privateSubnet1,
this.privateSubnet2,
this.privateSubnet3,
],
},
}
)
}一旦我得到这些vpc和子网,我就把它们传递给我们的lambda,它有一个增强的节点js lambda (一个围绕节点js lambda函数的包装器),在这里我检查vpc支持程序是否可用,我将添加定义的三个子网。
我的lambda函数的构造函数:
constructor(scope: Construct, id: string, props: EnhancedNodeJsLambdaProps) {
super(scope, id, {
...props,
...(props.vpc && {
vpcSubnets: props.vpc.selectSubnets({
subnetFilters: [
SubnetFilter.containsIpAddresses(['xx.x.x.x/xx', 'xx.x.x.x/xx', 'xx.x.x.x/xx']
),
],
}),
}),
runtime: props.runtime || Runtime.NODEJS_12_X,
tracing: Tracing.ACTIVE,
})
}所以现在,当我尝试测试我的lambda时,无论子网是否附加到它,我要么得到一些虚拟的az值,要么它返回lambda与子网没有连接,我如何测试相同的子网?
FYR,我会在下面附上一些测试用例
it('testing vpc subnets ', async () => {
const app = new cdk.App()
const topicsStack = new cdk.Stack(app, 'TopicsStack')
const globalStack = await new GlobalStack(app, 'global-stack', {
stackName: 'global-stack',
description: 'Global Resources (Shared at the account level)',
env: {
account: '11111111',
region: 'us-east-1',
},
envName: 'test',
})
let newLambda = new EnhancedNodeJsLambda(topicsStack, 'test-lambda', {
entry,
connectionReuseEnabled: true,
vpc: globalStack.vpc,
})
console.log(
globalStack.vpc.selectSubnets({
subnetFilters: [
SubnetFilter.containsIpAddresses(['xx.x.x.x/xx', 'xx.x.x.x/xx', 'xx.x.x.x/xx']),
],
}).availabilityZones
)
//how to test subnets are properly linked?
})同样关于虚拟值,上面测试用例中的控制台日志将返回一些虚拟的az值,而不是我的代码值:
控制台日志返回
[ 'dummy1a', 'dummy1b' ]我试图通过将下面的代码添加到上面的测试用例来测试连接
const othertemp = Template.fromStack(topicsStack)
othertemp.hasResourceProperties('AWS::Lambda::Function', {
VpcConfig: {
SubnetIds: globalStack.vpc.selectSubnets({
subnetFilters: [
SubnetFilter.containsIpAddresses(['xx.x.x.x/xx', 'xx.x.x.x/xx', 'xx.x.x.x/xx']
),
],
}),
},
})但它没能说
with the following mismatches:
Expected type object but received array at /Properties/VpcConfig/SubnetIds (using objectLike matcher)另外,子网id和az的是虚拟值,而不是我想要的值。
我不知道它为什么返回虚拟值而不是预定义的值,而且我也不知道如何测试lambda是否与正确的子网相关联。
发布于 2022-07-22 15:10:46
嗯,我不知道如何用CDK断言来完成它,但是我能够用开玩笑的断言来完成它:
expect(myFunction.Properties.VpcConfig.SubnetIds).toEqual('my-subnet-ids');https://stackoverflow.com/questions/71016240
复制相似问题