@lambda-kata/cdk 0.1.3-rc.9 → 0.1.3-rc.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ /**
2
+ * SnapStart Activator - Custom Resource Handler
3
+ *
4
+ * This module provides the Lambda handler for a CloudFormation Custom Resource
5
+ * that enables SnapStart on Lambda functions after deployment. SnapStart requires
6
+ * asynchronous waiting for snapshot creation, which cannot be done during CDK synthesis.
7
+ *
8
+ * The activation process:
9
+ * 1. Wait for function to be Active
10
+ * 2. Enable SnapStart configuration
11
+ * 3. Wait for configuration update
12
+ * 4. Publish new version (triggers snapshot creation)
13
+ * 5. Wait for snapshot to be ready (up to 3 minutes)
14
+ * 6. Create/update 'kata' alias pointing to the new version
15
+ *
16
+ * @module snapstart-activator
17
+ */
18
+ import { LambdaClient } from '@aws-sdk/client-lambda';
19
+ /**
20
+ * Custom Resource event from CloudFormation.
21
+ */
22
+ export interface CustomResourceEvent {
23
+ RequestType: 'Create' | 'Update' | 'Delete';
24
+ ServiceToken: string;
25
+ ResponseURL: string;
26
+ StackId: string;
27
+ RequestId: string;
28
+ ResourceType: string;
29
+ LogicalResourceId: string;
30
+ PhysicalResourceId?: string;
31
+ ResourceProperties: {
32
+ ServiceToken: string;
33
+ FunctionName: string;
34
+ AliasName?: string;
35
+ };
36
+ OldResourceProperties?: {
37
+ FunctionName: string;
38
+ AliasName?: string;
39
+ };
40
+ }
41
+ /**
42
+ * Custom Resource response to CloudFormation.
43
+ */
44
+ export interface CustomResourceResponse {
45
+ Status: 'SUCCESS' | 'FAILED';
46
+ Reason?: string;
47
+ PhysicalResourceId: string;
48
+ StackId: string;
49
+ RequestId: string;
50
+ LogicalResourceId: string;
51
+ Data?: {
52
+ Version?: string;
53
+ AliasName?: string;
54
+ AliasArn?: string;
55
+ OptimizationStatus?: string;
56
+ };
57
+ }
58
+ /**
59
+ * Result of a successful SnapStart activation cycle.
60
+ *
61
+ * Returned by {@link activateSnapStart} after enabling SnapStart,
62
+ * publishing a version, waiting for snapshot readiness, and creating/updating an alias.
63
+ *
64
+ * @see {@link SnapStartActivatorConfig} for configuration options
65
+ */
66
+ export interface SnapStartActivationResult {
67
+ /** The published Lambda version number (e.g. "42"). */
68
+ version: string;
69
+ /** The alias name that was created or updated (e.g. "kata"). */
70
+ aliasName: string;
71
+ /** The full ARN of the alias (e.g. "arn:aws:lambda:us-east-1:123456789012:function:my-fn:kata"). */
72
+ aliasArn: string;
73
+ /** The SnapStart optimization status of the published version ("On", "Off", or "Unknown"). */
74
+ optimizationStatus: string;
75
+ }
76
+ /**
77
+ * Configuration options for the SnapStart activation cycle.
78
+ *
79
+ * All properties are optional and fall back to sensible defaults.
80
+ *
81
+ * @see {@link activateSnapStart} for the function that consumes this config
82
+ */
83
+ export interface SnapStartActivatorConfig {
84
+ /**
85
+ * Maximum time in seconds to wait for snapshot creation before proceeding.
86
+ * If exceeded, a warning is logged and alias creation continues.
87
+ * @default 180
88
+ */
89
+ snapshotTimeoutSeconds?: number;
90
+ /**
91
+ * Interval in seconds between polling attempts for snapshot readiness.
92
+ * @default 2
93
+ */
94
+ pollingIntervalSeconds?: number;
95
+ /**
96
+ * The Lambda alias name to create or update after publishing a version.
97
+ * @default 'kata'
98
+ */
99
+ aliasName?: string;
100
+ /**
101
+ * @internal Override pre-publish delay in milliseconds (for testing only).
102
+ * @default 3000
103
+ */
104
+ _prePublishDelayMs?: number;
105
+ /**
106
+ * @internal Override retry delay in milliseconds (for testing only).
107
+ * @default 5000
108
+ */
109
+ _retryDelayMs?: number;
110
+ }
111
+ /**
112
+ * Sleep for specified milliseconds.
113
+ * @internal Exported for testability — tests can jest.spyOn to avoid real delays.
114
+ */
115
+ export declare const _testable: {
116
+ sleep(ms: number): Promise<void>;
117
+ };
118
+ /**
119
+ * Activates SnapStart on a Lambda function.
120
+ *
121
+ * This function performs the full SnapStart activation cycle:
122
+ * 1. Ensures function is Active
123
+ * 2. Enables SnapStart configuration
124
+ * 3. Waits for configuration update
125
+ * 4. Publishes new version (with retry on snapshot failure)
126
+ * 5. Waits for snapshot creation
127
+ * 6. Creates/updates alias
128
+ *
129
+ * Steps 3+4 are wrapped in a retry loop: on State: Failed, a new version
130
+ * is published (up to MAX_PUBLISH_RETRIES attempts) to handle transient
131
+ * initialization failures during snapshot creation.
132
+ *
133
+ * @param lambdaClient - AWS Lambda client
134
+ * @param functionName - Name or ARN of the Lambda function
135
+ * @param config - Optional configuration
136
+ * @returns Activation result with version and alias information
137
+ */
138
+ export declare function activateSnapStart(lambdaClient: LambdaClient, functionName: string, config?: SnapStartActivatorConfig): Promise<SnapStartActivationResult>;
139
+ /**
140
+ * Lambda handler for CloudFormation Custom Resource.
141
+ *
142
+ * This handler is invoked by CloudFormation when the custom resource
143
+ * is created, updated, or deleted.
144
+ */
145
+ export declare function handler(event: CustomResourceEvent): Promise<CustomResourceResponse>;
@@ -0,0 +1,112 @@
1
+ /**
2
+ * SnapStart Construct - CDK Custom Resource for SnapStart Activation
3
+ *
4
+ * This module provides a CDK construct that creates a Custom Resource
5
+ * to enable SnapStart on Lambda functions after deployment. The construct
6
+ * handles the asynchronous nature of SnapStart snapshot creation.
7
+ *
8
+ * @module snapstart-construct
9
+ */
10
+ import { Construct } from 'constructs';
11
+ import { CustomResource } from 'aws-cdk-lib';
12
+ import { IFunction } from 'aws-cdk-lib/aws-lambda';
13
+ /**
14
+ * Properties for the {@link SnapStartActivator} construct.
15
+ *
16
+ * @see {@link SnapStartActivator} for the construct that consumes these properties
17
+ */
18
+ export interface SnapStartActivatorProps {
19
+ /**
20
+ * The Lambda function to enable SnapStart on.
21
+ * The construct will add a dependency so the Custom Resource runs after this function is created.
22
+ */
23
+ targetFunction: IFunction;
24
+ /**
25
+ * The alias name to create or update after publishing a SnapStart-enabled version.
26
+ * @default 'kata'
27
+ */
28
+ aliasName?: string;
29
+ /**
30
+ * Maximum time in seconds to wait for SnapStart snapshot creation.
31
+ * The Custom Resource handler timeout is set to this value plus a 60-second buffer.
32
+ * @default 180
33
+ */
34
+ snapshotTimeoutSeconds?: number;
35
+ }
36
+ /**
37
+ * CDK Construct that enables SnapStart on a Lambda function after deployment.
38
+ *
39
+ * Creates a CloudFormation Custom Resource backed by a Lambda handler that
40
+ * performs the full SnapStart activation cycle during stack deployment:
41
+ *
42
+ * 1. Waits for the target function to reach Active state
43
+ * 2. Enables SnapStart configuration (`ApplyOn: PublishedVersions`)
44
+ * 3. Publishes a new version (triggers snapshot creation)
45
+ * 4. Polls until the snapshot is ready (or timeout is reached)
46
+ * 5. Creates or updates an alias pointing to the new version
47
+ *
48
+ * After deployment, the published version number and alias ARN are available
49
+ * as CloudFormation attributes via {@link versionRef} and {@link aliasArnRef}.
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const myFunction = new lambda.Function(this, 'MyFunction', { ... });
54
+ *
55
+ * const snapStart = new SnapStartActivator(this, 'SnapStart', {
56
+ * targetFunction: myFunction,
57
+ * aliasName: 'kata',
58
+ * snapshotTimeoutSeconds: 180,
59
+ * });
60
+ *
61
+ * // Reference outputs after deployment
62
+ * new CfnOutput(this, 'Version', { value: snapStart.versionRef });
63
+ * new CfnOutput(this, 'AliasArn', { value: snapStart.aliasArnRef });
64
+ * ```
65
+ *
66
+ * @see {@link SnapStartActivatorProps} for configuration options
67
+ */
68
+ export declare class SnapStartActivator extends Construct {
69
+ /**
70
+ * The alias name that was created or updated (e.g. "kata").
71
+ *
72
+ * This is a static property set at synthesis time from {@link SnapStartActivatorProps.aliasName}.
73
+ */
74
+ readonly aliasName: string;
75
+ /**
76
+ * The Custom Resource that manages SnapStart activation during deployment.
77
+ *
78
+ * Use this to add additional dependencies or access CloudFormation attributes.
79
+ */
80
+ readonly resource: CustomResource;
81
+ /**
82
+ * The version number created by SnapStart activation (CloudFormation attribute).
83
+ *
84
+ * This value is resolved at deployment time when the Custom Resource handler
85
+ * publishes a new Lambda version. Use it in `CfnOutput` or other constructs
86
+ * that need to reference the published version.
87
+ */
88
+ readonly versionRef: string;
89
+ /**
90
+ * The alias ARN created by SnapStart activation (CloudFormation attribute).
91
+ *
92
+ * This value is resolved at deployment time when the Custom Resource handler
93
+ * creates or updates the alias. Use it in `CfnOutput` or other constructs
94
+ * that need to reference the alias.
95
+ */
96
+ readonly aliasArnRef: string;
97
+ constructor(scope: Construct, id: string, props: SnapStartActivatorProps);
98
+ /**
99
+ * Creates the Lambda function that handles Custom Resource events.
100
+ * Permissions are passed via initialPolicy to ensure they are part of the
101
+ * role creation (not a separate AWS::IAM::Policy resource), preventing
102
+ * IAM propagation race conditions.
103
+ */
104
+ private createProviderFunction;
105
+ /**
106
+ * Generates the inline handler code for the Custom Resource.
107
+ *
108
+ * This is a self-contained version of the snapstart-activator logic
109
+ * that can be deployed as inline Lambda code.
110
+ */
111
+ private generateHandlerCode;
112
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Synchronous Account ID Resolution for Lambda Kata CDK Integration
3
+ *
4
+ * This module provides synchronous account ID resolution for use during
5
+ * CDK synthesis, which requires synchronous operations.
6
+ *
7
+ * Resolution strategies (in order of precedence):
8
+ * 1. CDK context value (explicit configuration)
9
+ * 2. Stack account (if not a token/unresolved)
10
+ * 3. Environment variables (AWS_ACCOUNT_ID, CDK_DEFAULT_ACCOUNT)
11
+ * 4. STS GetCallerIdentity via execSync (fallback)
12
+ *
13
+ * @module sync-account-resolver
14
+ */
15
+ import { Construct } from 'constructs';
16
+ /**
17
+ * Error thrown when account ID cannot be resolved through any strategy.
18
+ */
19
+ export declare class SyncAccountResolutionError extends Error {
20
+ constructor(message: string);
21
+ }
22
+ /**
23
+ * Result of account ID resolution, including the source of the resolution.
24
+ */
25
+ export interface SyncAccountResolutionResult {
26
+ accountId: string;
27
+ source: 'context' | 'stack' | 'env' | 'sts';
28
+ }
29
+ /**
30
+ * Options for synchronous account ID resolution.
31
+ */
32
+ export interface SyncAccountResolverOptions {
33
+ /**
34
+ * Whether to skip the STS fallback (useful for testing).
35
+ * Default: false
36
+ */
37
+ skipStsFallback?: boolean;
38
+ }
39
+ /**
40
+ * Resolves the target AWS account ID synchronously.
41
+ *
42
+ * This function attempts to determine the AWS account ID using multiple
43
+ * strategies in order of precedence:
44
+ *
45
+ * 1. **CDK Context**: Checks for `aws:cdk:account` context value
46
+ * 2. **Stack Account**: Uses the Stack's account if it's not a token
47
+ * 3. **Environment Variables**: Checks AWS_ACCOUNT_ID, CDK_DEFAULT_ACCOUNT
48
+ * 4. **STS Fallback**: Calls AWS CLI sts get-caller-identity
49
+ *
50
+ * @param scope - The CDK construct scope to resolve the account for
51
+ * @param options - Optional configuration for resolution behavior
52
+ * @returns The AWS account ID (12-digit string)
53
+ * @throws SyncAccountResolutionError if account cannot be resolved
54
+ */
55
+ export declare function resolveAccountIdSync(scope: Construct, options?: SyncAccountResolverOptions): string;
56
+ /**
57
+ * Resolves the target AWS account ID synchronously with source information.
58
+ */
59
+ export declare function resolveAccountIdSyncWithSource(scope: Construct, options?: SyncAccountResolverOptions): SyncAccountResolutionResult;
60
+ /**
61
+ * Resolves the deployment region synchronously.
62
+ */
63
+ export declare function resolveRegionSync(scope: Construct): string;
@@ -41,10 +41,19 @@ export interface LicensingResponse {
41
41
  */
42
42
  entitled: boolean;
43
43
  /**
44
- * Customer-specific Lambda Layer ARN containing the Lambda Kata runtime.
44
+ * Base Lambda Layer ARN (without version number).
45
45
  * Only present if the account is entitled.
46
+ * @deprecated Use layerVersionArn for attaching to Lambda functions
46
47
  */
47
48
  layerArn?: string;
49
+ /**
50
+ * Full Lambda Layer Version ARN (with version number).
51
+ * This is the ARN that should be used when attaching layers to Lambda functions.
52
+ * Only present if the account is entitled.
53
+ *
54
+ * @example "arn:aws:lambda:eu-central-1:113258654684:layer:lambda-kata-euc:1"
55
+ */
56
+ layerVersionArn?: string;
48
57
  /**
49
58
  * Human-readable status message
50
59
  */
@@ -67,6 +76,11 @@ export interface TransformationConfig {
67
76
  * This will be stored in the config layer.
68
77
  */
69
78
  originalHandler: string;
79
+ /**
80
+ * The original Node.js runtime (e.g., "nodejs20.x").
81
+ * Used to create the appropriate Node.js runtime layer.
82
+ */
83
+ originalRuntime?: string;
70
84
  /**
71
85
  * The target runtime for the transformed Lambda.
72
86
  * Always Runtime.PYTHON_3_12 for Lambda Kata.
@@ -74,7 +88,7 @@ export interface TransformationConfig {
74
88
  targetRuntime: Runtime;
75
89
  /**
76
90
  * The handler path for the Lambda Kata runtime.
77
- * Always "lambdakata.optimized_handler.lambda_handler"
91
+ * Always "handler.lambda_handler"
78
92
  */
79
93
  targetHandler: string;
80
94
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambda-kata/cdk",
3
- "version": "0.1.3-rc.9",
3
+ "version": "0.1.3-rc.90",
4
4
  "description": "AWS CDK integration for Lambda Kata - Node.js Lambdas running via Lambda Kata runtime",
5
5
  "main": "out/dist/index.js",
6
6
  "types": "out/tsc/src/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "docs": "yarn run docs:md && yarn run docs:html",
19
19
  "docs:md": "typedoc --options ./docs/docs.config/typedoc.md.json",
20
20
  "docs:html": "typedoc --options ./docs/docs.config/typedoc.html.json",
21
- "npm:publish": "npm publish --access public"
21
+ "npm:publish": "yarn run build && npm publish --access public"
22
22
  },
23
23
  "files": [
24
24
  "out/dist/**/*",
@@ -50,11 +50,14 @@
50
50
  },
51
51
  "peerDependencies": {
52
52
  "aws-cdk-lib": "^2.0.0",
53
- "constructs": "^10.0.0"
53
+ "constructs": "^10.0.0",
54
+ "esbuild": "^0.23.0"
54
55
  },
55
56
  "dependencies": {
56
57
  "@aws-sdk/client-lambda": "^3.500.0",
58
+ "@aws-sdk/client-s3": "^3.500.0",
57
59
  "@aws-sdk/client-sts": "^3.500.0",
60
+ "@lambda-kata/licensing": "0.1.32",
58
61
  "dotenv": "^17.2.3",
59
62
  "reflect-metadata": "^0.2.2"
60
63
  },
@@ -73,5 +76,10 @@
73
76
  "ts-jest": "^29.1.2",
74
77
  "tsc-alias": "^1.8.16",
75
78
  "typescript": "^5.3.3"
79
+ },
80
+ "peerDependenciesMeta": {
81
+ "esbuild": {
82
+ "optional": true
83
+ }
76
84
  }
77
85
  }