Ross Coundon
08/16/2021, 7:44 PMthdxr
08/16/2021, 7:44 PMRoss Coundon
08/16/2021, 7:45 PMRoss Coundon
08/16/2021, 7:48 PMthdxr
08/16/2021, 7:54 PMconst vpc = new ec2.Vpc(this, "vpc")
This creates the classic public/private subnets and I passed that to the vpc paramFrank
const vpc = ec2.Vpc.fromLookup(this, "iVpc", {
vpcId: "...",
})
Frank
Ross Coundon
08/16/2021, 7:59 PMRoss Coundon
08/16/2021, 8:34 PMexport default function main(app: <http://sst.App|sst.App>): void {
// Set default runtime for all functions
app.setDefaultFunctionProps((stack) => ({
runtime: 'nodejs14.x',
vpc: Vpc.fromLookup(stack, 'default-vpc', {
isDefault: true,
}),
securityGroups: [SecurityGroup.fromSecurityGroupId(stack, 'default-sg', 'sg-0083d5584b79deac3')],
}));
new OmwOfscBeStack(app, `OmwOfscBeStack-${stage}`);
}
The build fails with
Error: There is already a Construct with name 'default-vpc' in OmwOfscBeStack [ttgb-prod-omw-ofsc-be-OmwOfscBeStack-ttgb-prod]
Ross Coundon
08/16/2021, 8:44 PMFrank
setDefaultFunctionProps
gets invoked for each Lambda in the stack… and only the first import works b/c you can only import something under the name onceFrank
const vpcsByStackName = {};
const securityGroupsByStackName = {};
app.setDefaultFunctionProps((stack) => {
// Get already imported VPC and SG
let vpc = vpcsByStackName[stack];
let securityGroup = securityGroupsByStackName[stack];
// If VPC and SG have not been imported, then import now
if (!vpc) {
vpc = Vpc.fromLookup(stack, 'default-vpc', {
isDefault: true,
});
securityGroup = SecurityGroup.fromSecurityGroupId(stack, 'default-sg', 'sg-0083d5584b79deac3');
vpcsByStackName[stack] = vpc;
securityGroupsByStackName[stack] = securityGroup;
}
// Return the function definition
return {
runtime: 'nodejs14.x',
vpc,
securityGroups: [securityGroup],
};
});
Frank
Ross Coundon
08/16/2021, 8:56 PMthdxr
08/16/2021, 8:58 PMsetDefaultFunctionProps
took a function 😮Frank
Frank
Frank
Ross Coundon
08/16/2021, 9:01 PMFrank
stack.name
should workthdxr
08/16/2021, 9:04 PMFrank
thdxr
08/16/2021, 9:08 PMmain
and not inside a stack. Which means it doesn't have access to stack
and gets access via a callback. So it's used when you want to define common behavior across stacks that reference stack informationFrank
Ross Coundon
08/16/2021, 9:12 PMFrank
Ross Coundon
08/16/2021, 9:14 PMFrank
CoreStack
and create them there.Ross Coundon
08/16/2021, 9:16 PMFrank
Frank
Ross Coundon
08/16/2021, 9:19 PMRoss Coundon
08/16/2021, 9:22 PMtype VpcsByStackName = {
[key: string]: IVpc;
};
type SecGroupsByStackName = {
[key: string]: ISecurityGroup;
};
Frank
Ixxxx
is the imported version of xxxx
constructs in CDKRoss Coundon
08/16/2021, 9:46 PM