Problem statement
I have my main CDK stack in eu-west-1
, but an EdgeFunction
is created in us-east-1
.
I have noticed two weird things with EdgeFunction
:
- Even if declared in a
NestedStack
they appear incdk ls
asedge-lambda-stack-nnnnnnnnn
(unless given an explicit name). - When removing the main stack, let's call it
primary
, it does not remove the lambda stack. Probably because (1) above tells me it's not a part of theNestedStack
.
I have tried putting the EdgeFunction
in a separate stack created in us-east-1
explicitly and then cross-referencing it from primary
but that fails with "Cannot use resource in a cross-environment fashion" (amongst others).
Questions
- Why doesn't
cloudfront.experimental.EdgeFunction
respect theNestedStack
boundary? - Can I somehow build a
Stack
that exists inus-east-1
and cross-reference the lambda into my main stack ineu-west-1
? - Can I at least make it so that deleting the
primary
stack also automatically deletes the lambda stack.
The reason I'm asking is because I have a number of environments and a number of lambdas and the combinatorial explosion of stacks makes things a bit unwieldly.
Example
CDK version 1.116
import "source-map-support/register";
import * as cdk from "@aws-cdk/core";
import * as cloudfront from "@aws-cdk/aws-cloudfront";
import * as lambda from "@aws-cdk/aws-lambda";
import * as path from "path";
class PrimaryStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new SecondaryStack(this, "secondary-stack");
}
}
class SecondaryStack extends cdk.NestedStack {
constructor(scope: cdk.Construct, id: string) {
super(scope, id);
new cloudfront.experimental.EdgeFunction(this, "my-lambda", {
runtime: lambda.Runtime.NODEJS_14_X,
functionName: "my-lambda",
handler: "index.handler",
code: lambda.Code.fromAsset(
path.join(__dirname, "..", "lib", "my-lambda"),
),
});
}
}
const app = new cdk.App();
new PrimaryStack(app, "primary", {
env: { account: "123", region: "eu-west-1" },
});
cdk ls
output:
edge-lambda-stack-cnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
primary