Skip to content

Commit

Permalink
fix(type-safe-api): ensure lambda function names do not exceed max le…
Browse files Browse the repository at this point in the history
…ngth

Truncate the lambda function names for the custom resource to ensure they are below the max length
of 64 characters.

Fixes #615
  • Loading branch information
cogwirrel committed Oct 30, 2023
1 parent d1babf7 commit 687bca2
Show file tree
Hide file tree
Showing 3 changed files with 3,377 additions and 3,339 deletions.
15 changes: 9 additions & 6 deletions packages/type-safe-api/src/construct/type-safe-rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ export class TypeSafeRestApi extends Construct {

const stack = Stack.of(this);

const prepareSpecLambdaName = `${PDKNag.getStackPrefix(stack)
// Lambda name prefix is truncated to 48 characters (16 below the max of 64)
const lambdaNamePrefix = `${PDKNag.getStackPrefix(stack)
.split("/")
.join("-")}PrepareSpec${this.node.addr.slice(-8).toUpperCase()}`;
.join("-")
.slice(0, 40)}${this.node.addr.slice(-8).toUpperCase()}`;
const prepareSpecLambdaName = `${lambdaNamePrefix}PrepSpec`;
const prepareSpecRole = new Role(this, "PrepareSpecRole", {
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
inlinePolicies: {
Expand Down Expand Up @@ -210,7 +213,7 @@ export class TypeSafeRestApi extends Construct {
);

// Create a custom resource for preparing the spec for deployment (adding integrations, authorizers, etc)
const prepareSpec = new LambdaFunction(this, "PrepareSpec", {
const prepareSpec = new LambdaFunction(this, "PrepareSpecHandler", {
handler: "index.handler",
runtime: Runtime.NODEJS_18_X,
code: Code.fromAsset(
Expand All @@ -221,7 +224,7 @@ export class TypeSafeRestApi extends Construct {
functionName: prepareSpecLambdaName,
});

const providerFunctionName = `${prepareSpecLambdaName}-Provider`;
const providerFunctionName = `${lambdaNamePrefix}PrepSpecProvider`;
const providerRole = new Role(this, "PrepareSpecProviderRole", {
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
inlinePolicies: {
Expand All @@ -244,7 +247,7 @@ export class TypeSafeRestApi extends Construct {
},
});

const provider = new Provider(this, "PrepareSpecResourceProvider", {
const provider = new Provider(this, "PrepareSpecProvider", {
onEventHandler: prepareSpec,
role: providerRole,
providerFunctionName,
Expand Down Expand Up @@ -345,7 +348,7 @@ export class TypeSafeRestApi extends Construct {

const prepareSpecCustomResource = new CustomResource(
this,
"PrepareSpecResource",
"PrepareSpecCustomResource",
{
serviceToken: provider.serviceToken,
properties: prepareApiSpecCustomResourceProperties,
Expand Down
Loading

0 comments on commit 687bca2

Please sign in to comment.