Reputation: 71
I have a global stack, where I have my predefined VPCs and subnets
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,
],
},
}
)
}
And Once I get these vpc and subnet , I pass them to our lambda , which we have an enhanced node js lambda (a wrapper around node js lambda function), where I check if vpc props are available, I will add the defined three subnets.
The constructor of my lambda function:
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,
})
}
So now when I try to test my lambda, whether the subnets are attached to it, I either get some dummy az values or it returns that the lambda isn't connected with subnets, how can I test the same?
FYR, I will attach some of my test cases below
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?
})
Also about the dummy values, the console log in the test case above returns me some dummy az values instead of my code ones:
the console log returns
[ 'dummy1a', 'dummy1b' ]
I tried to test the connection by adding the below code to the test case above
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']
),
],
}),
},
})
But it failed saying
with the following mismatches:
Expected type object but received array at /Properties/VpcConfig/SubnetIds (using objectLike matcher)
Also the subnet id's and az's are dummy values and not my intended ones.
I'm not sure why it's returning dummy values instead of my pre-defined one, and also I don't know how to test the lambda is connected with the correct subnets or no.
Upvotes: 2
Views: 692
Reputation:
Well, not sure how to do it with CDK assertions, but I was able to get it done with jest assertions:
expect(myFunction.Properties.VpcConfig.SubnetIds).toEqual('my-subnet-ids');
Upvotes: 0