UNPKG

22.4 kBTypeScriptView Raw
1import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
2import { IProfilingGroup } from '@aws-cdk/aws-codeguruprofiler';
3import * as ec2 from '@aws-cdk/aws-ec2';
4import * as iam from '@aws-cdk/aws-iam';
5import * as kms from '@aws-cdk/aws-kms';
6import * as logs from '@aws-cdk/aws-logs';
7import * as sns from '@aws-cdk/aws-sns';
8import * as sqs from '@aws-cdk/aws-sqs';
9import { Duration, Size } from '@aws-cdk/core';
10import { Construct } from 'constructs';
11import { Architecture } from './architecture';
12import { Code, CodeConfig } from './code';
13import { ICodeSigningConfig } from './code-signing-config';
14import { EventInvokeConfigOptions } from './event-invoke-config';
15import { IEventSource } from './event-source';
16import { FileSystem } from './filesystem';
17import { FunctionAttributes, FunctionBase, IFunction } from './function-base';
18import { LambdaInsightsVersion } from './lambda-insights';
19import { Version, VersionOptions } from './lambda-version';
20import { ILayerVersion } from './layers';
21import { Runtime } from './runtime';
22import { LogRetentionRetryOptions } from './log-retention';
23/**
24 * X-Ray Tracing Modes (https://docs.aws.amazon.com/lambda/latest/dg/API_TracingConfig.html)
25 */
26export declare enum Tracing {
27 /**
28 * Lambda will respect any tracing header it receives from an upstream service.
29 * If no tracing header is received, Lambda will call X-Ray for a tracing decision.
30 */
31 ACTIVE = "Active",
32 /**
33 * Lambda will only trace the request from an upstream service
34 * if it contains a tracing header with "sampled=1"
35 */
36 PASS_THROUGH = "PassThrough",
37 /**
38 * Lambda will not trace any request.
39 */
40 DISABLED = "Disabled"
41}
42/**
43 * Non runtime options
44 */
45export interface FunctionOptions extends EventInvokeConfigOptions {
46 /**
47 * A description of the function.
48 *
49 * @default - No description.
50 */
51 readonly description?: string;
52 /**
53 * The function execution time (in seconds) after which Lambda terminates
54 * the function. Because the execution time affects cost, set this value
55 * based on the function's expected execution time.
56 *
57 * @default Duration.seconds(3)
58 */
59 readonly timeout?: Duration;
60 /**
61 * Key-value pairs that Lambda caches and makes available for your Lambda
62 * functions. Use environment variables to apply configuration changes, such
63 * as test and production environment configurations, without changing your
64 * Lambda function source code.
65 *
66 * @default - No environment variables.
67 */
68 readonly environment?: {
69 [key: string]: string;
70 };
71 /**
72 * A name for the function.
73 *
74 * @default - AWS CloudFormation generates a unique physical ID and uses that
75 * ID for the function's name. For more information, see Name Type.
76 */
77 readonly functionName?: string;
78 /**
79 * The amount of memory, in MB, that is allocated to your Lambda function.
80 * Lambda uses this value to proportionally allocate the amount of CPU
81 * power. For more information, see Resource Model in the AWS Lambda
82 * Developer Guide.
83 *
84 * @default 128
85 */
86 readonly memorySize?: number;
87 /**
88 * The size of the function’s /tmp directory in MiB.
89 *
90 * @default 512 MiB
91 */
92 readonly ephemeralStorageSize?: Size;
93 /**
94 * Initial policy statements to add to the created Lambda Role.
95 *
96 * You can call `addToRolePolicy` to the created lambda to add statements post creation.
97 *
98 * @default - No policy statements are added to the created Lambda role.
99 */
100 readonly initialPolicy?: iam.PolicyStatement[];
101 /**
102 * Lambda execution role.
103 *
104 * This is the role that will be assumed by the function upon execution.
105 * It controls the permissions that the function will have. The Role must
106 * be assumable by the 'lambda.amazonaws.com' service principal.
107 *
108 * The default Role automatically has permissions granted for Lambda execution. If you
109 * provide a Role, you must add the relevant AWS managed policies yourself.
110 *
111 * The relevant managed policies are "service-role/AWSLambdaBasicExecutionRole" and
112 * "service-role/AWSLambdaVPCAccessExecutionRole".
113 *
114 * @default - A unique role will be generated for this lambda function.
115 * Both supplied and generated roles can always be changed by calling `addToRolePolicy`.
116 */
117 readonly role?: iam.IRole;
118 /**
119 * VPC network to place Lambda network interfaces
120 *
121 * Specify this if the Lambda function needs to access resources in a VPC.
122 *
123 * @default - Function is not placed within a VPC.
124 */
125 readonly vpc?: ec2.IVpc;
126 /**
127 * Where to place the network interfaces within the VPC.
128 *
129 * Only used if 'vpc' is supplied. Note: internet access for Lambdas
130 * requires a NAT gateway, so picking Public subnets is not allowed.
131 *
132 * @default - the Vpc default strategy if not specified
133 */
134 readonly vpcSubnets?: ec2.SubnetSelection;
135 /**
136 * What security group to associate with the Lambda's network interfaces.
137 * This property is being deprecated, consider using securityGroups instead.
138 *
139 * Only used if 'vpc' is supplied.
140 *
141 * Use securityGroups property instead.
142 * Function constructor will throw an error if both are specified.
143 *
144 * @default - If the function is placed within a VPC and a security group is
145 * not specified, either by this or securityGroups prop, a dedicated security
146 * group will be created for this function.
147 *
148 * @deprecated - This property is deprecated, use securityGroups instead
149 */
150 readonly securityGroup?: ec2.ISecurityGroup;
151 /**
152 * The list of security groups to associate with the Lambda's network interfaces.
153 *
154 * Only used if 'vpc' is supplied.
155 *
156 * @default - If the function is placed within a VPC and a security group is
157 * not specified, either by this or securityGroup prop, a dedicated security
158 * group will be created for this function.
159 */
160 readonly securityGroups?: ec2.ISecurityGroup[];
161 /**
162 * Whether to allow the Lambda to send all network traffic
163 *
164 * If set to false, you must individually add traffic rules to allow the
165 * Lambda to connect to network targets.
166 *
167 * @default true
168 */
169 readonly allowAllOutbound?: boolean;
170 /**
171 * Enabled DLQ. If `deadLetterQueue` is undefined,
172 * an SQS queue with default options will be defined for your Function.
173 *
174 * @default - false unless `deadLetterQueue` is set, which implies DLQ is enabled.
175 */
176 readonly deadLetterQueueEnabled?: boolean;
177 /**
178 * The SQS queue to use if DLQ is enabled.
179 * If SNS topic is desired, specify `deadLetterTopic` property instead.
180 *
181 * @default - SQS queue with 14 day retention period if `deadLetterQueueEnabled` is `true`
182 */
183 readonly deadLetterQueue?: sqs.IQueue;
184 /**
185 * The SNS topic to use as a DLQ.
186 * Note that if `deadLetterQueueEnabled` is set to `true`, an SQS queue will be created
187 * rather than an SNS topic. Using an SNS topic as a DLQ requires this property to be set explicitly.
188 *
189 * @default - no SNS topic
190 */
191 readonly deadLetterTopic?: sns.ITopic;
192 /**
193 * Enable AWS X-Ray Tracing for Lambda Function.
194 *
195 * @default Tracing.Disabled
196 */
197 readonly tracing?: Tracing;
198 /**
199 * Enable profiling.
200 * @see https://docs.aws.amazon.com/codeguru/latest/profiler-ug/setting-up-lambda.html
201 *
202 * @default - No profiling.
203 */
204 readonly profiling?: boolean;
205 /**
206 * Profiling Group.
207 * @see https://docs.aws.amazon.com/codeguru/latest/profiler-ug/setting-up-lambda.html
208 *
209 * @default - A new profiling group will be created if `profiling` is set.
210 */
211 readonly profilingGroup?: IProfilingGroup;
212 /**
213 * Specify the version of CloudWatch Lambda insights to use for monitoring
214 * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights.html
215 *
216 * When used with `DockerImageFunction` or `DockerImageCode`, the Docker image should have
217 * the Lambda insights agent installed.
218 * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-Getting-Started-docker.html
219 *
220 * @default - No Lambda Insights
221 */
222 readonly insightsVersion?: LambdaInsightsVersion;
223 /**
224 * A list of layers to add to the function's execution environment. You can configure your Lambda function to pull in
225 * additional code during initialization in the form of layers. Layers are packages of libraries or other dependencies
226 * that can be used by multiple functions.
227 *
228 * @default - No layers.
229 */
230 readonly layers?: ILayerVersion[];
231 /**
232 * The maximum of concurrent executions you want to reserve for the function.
233 *
234 * @default - No specific limit - account limit.
235 * @see https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html
236 */
237 readonly reservedConcurrentExecutions?: number;
238 /**
239 * Event sources for this function.
240 *
241 * You can also add event sources using `addEventSource`.
242 *
243 * @default - No event sources.
244 */
245 readonly events?: IEventSource[];
246 /**
247 * The number of days log events are kept in CloudWatch Logs. When updating
248 * this property, unsetting it doesn't remove the log retention policy. To
249 * remove the retention policy, set the value to `INFINITE`.
250 *
251 * @default logs.RetentionDays.INFINITE
252 */
253 readonly logRetention?: logs.RetentionDays;
254 /**
255 * The IAM role for the Lambda function associated with the custom resource
256 * that sets the retention policy.
257 *
258 * @default - A new role is created.
259 */
260 readonly logRetentionRole?: iam.IRole;
261 /**
262 * When log retention is specified, a custom resource attempts to create the CloudWatch log group.
263 * These options control the retry policy when interacting with CloudWatch APIs.
264 *
265 * @default - Default AWS SDK retry options.
266 */
267 readonly logRetentionRetryOptions?: LogRetentionRetryOptions;
268 /**
269 * Options for the `lambda.Version` resource automatically created by the
270 * `fn.currentVersion` method.
271 * @default - default options as described in `VersionOptions`
272 */
273 readonly currentVersionOptions?: VersionOptions;
274 /**
275 * The filesystem configuration for the lambda function
276 *
277 * @default - will not mount any filesystem
278 */
279 readonly filesystem?: FileSystem;
280 /**
281 * Lambda Functions in a public subnet can NOT access the internet.
282 * Use this property to acknowledge this limitation and still place the function in a public subnet.
283 * @see https://stackoverflow.com/questions/52992085/why-cant-an-aws-lambda-function-inside-a-public-subnet-in-a-vpc-connect-to-the/52994841#52994841
284 *
285 * @default false
286 */
287 readonly allowPublicSubnet?: boolean;
288 /**
289 * The AWS KMS key that's used to encrypt your function's environment variables.
290 *
291 * @default - AWS Lambda creates and uses an AWS managed customer master key (CMK).
292 */
293 readonly environmentEncryption?: kms.IKey;
294 /**
295 * Code signing config associated with this function
296 *
297 * @default - Not Sign the Code
298 */
299 readonly codeSigningConfig?: ICodeSigningConfig;
300 /**
301 * DEPRECATED
302 * @default [Architecture.X86_64]
303 * @deprecated use `architecture`
304 */
305 readonly architectures?: Architecture[];
306 /**
307 * The system architectures compatible with this lambda function.
308 * @default Architecture.X86_64
309 */
310 readonly architecture?: Architecture;
311}
312export interface FunctionProps extends FunctionOptions {
313 /**
314 * The runtime environment for the Lambda function that you are uploading.
315 * For valid values, see the Runtime property in the AWS Lambda Developer
316 * Guide.
317 *
318 * Use `Runtime.FROM_IMAGE` when when defining a function from a Docker image.
319 */
320 readonly runtime: Runtime;
321 /**
322 * The source code of your Lambda function. You can point to a file in an
323 * Amazon Simple Storage Service (Amazon S3) bucket or specify your source
324 * code as inline text.
325 */
326 readonly code: Code;
327 /**
328 * The name of the method within your code that Lambda calls to execute
329 * your function. The format includes the file name. It can also include
330 * namespaces and other qualifiers, depending on the runtime.
331 * For more information, see https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-features.html#gettingstarted-features-programmingmodel.
332 *
333 * Use `Handler.FROM_IMAGE` when defining a function from a Docker image.
334 *
335 * NOTE: If you specify your source code as inline text by specifying the
336 * ZipFile property within the Code property, specify index.function_name as
337 * the handler.
338 */
339 readonly handler: string;
340}
341/**
342 * Deploys a file from inside the construct library as a function.
343 *
344 * The supplied file is subject to the 4096 bytes limit of being embedded in a
345 * CloudFormation template.
346 *
347 * The construct includes an associated role with the lambda.
348 *
349 * This construct does not yet reproduce all features from the underlying resource
350 * library.
351 */
352export declare class Function extends FunctionBase {
353 /**
354 * Returns a `lambda.Version` which represents the current version of this
355 * Lambda function. A new version will be created every time the function's
356 * configuration changes.
357 *
358 * You can specify options for this version using the `currentVersionOptions`
359 * prop when initializing the `lambda.Function`.
360 */
361 get currentVersion(): Version;
362 get resourceArnsForGrantInvoke(): string[];
363 /** @internal */
364 static _VER_PROPS: {
365 [key: string]: boolean;
366 };
367 /**
368 * Record whether specific properties in the `AWS::Lambda::Function` resource should
369 * also be associated to the Version resource.
370 * See 'currentVersion' section in the module README for more details.
371 * @param propertyName The property to classify
372 * @param locked whether the property should be associated to the version or not.
373 */
374 static classifyVersionProperty(propertyName: string, locked: boolean): void;
375 /**
376 * Import a lambda function into the CDK using its name
377 */
378 static fromFunctionName(scope: Construct, id: string, functionName: string): IFunction;
379 /**
380 * Import a lambda function into the CDK using its ARN
381 */
382 static fromFunctionArn(scope: Construct, id: string, functionArn: string): IFunction;
383 /**
384 * Creates a Lambda function object which represents a function not defined
385 * within this stack.
386 *
387 * @param scope The parent construct
388 * @param id The name of the lambda construct
389 * @param attrs the attributes of the function to import
390 */
391 static fromFunctionAttributes(scope: Construct, id: string, attrs: FunctionAttributes): IFunction;
392 /**
393 * Return the given named metric for this Lambda
394 */
395 static metricAll(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
396 /**
397 * Metric for the number of Errors executing all Lambdas
398 *
399 * @default sum over 5 minutes
400 */
401 static metricAllErrors(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
402 /**
403 * Metric for the Duration executing all Lambdas
404 *
405 * @default average over 5 minutes
406 */
407 static metricAllDuration(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
408 /**
409 * Metric for the number of invocations of all Lambdas
410 *
411 * @default sum over 5 minutes
412 */
413 static metricAllInvocations(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
414 /**
415 * Metric for the number of throttled invocations of all Lambdas
416 *
417 * @default sum over 5 minutes
418 */
419 static metricAllThrottles(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
420 /**
421 * Metric for the number of concurrent executions across all Lambdas
422 *
423 * @default max over 5 minutes
424 */
425 static metricAllConcurrentExecutions(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
426 /**
427 * Metric for the number of unreserved concurrent executions across all Lambdas
428 *
429 * @default max over 5 minutes
430 */
431 static metricAllUnreservedConcurrentExecutions(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
432 /**
433 * Name of this function
434 */
435 readonly functionName: string;
436 /**
437 * ARN of this function
438 */
439 readonly functionArn: string;
440 /**
441 * Execution role associated with this function
442 */
443 readonly role?: iam.IRole;
444 /**
445 * The runtime configured for this lambda.
446 */
447 readonly runtime: Runtime;
448 /**
449 * The principal this Lambda Function is running as
450 */
451 readonly grantPrincipal: iam.IPrincipal;
452 /**
453 * The DLQ (as queue) associated with this Lambda Function (this is an optional attribute).
454 */
455 readonly deadLetterQueue?: sqs.IQueue;
456 /**
457 * The DLQ (as topic) associated with this Lambda Function (this is an optional attribute).
458 */
459 readonly deadLetterTopic?: sns.ITopic;
460 /**
461 * The architecture of this Lambda Function (this is an optional attribute and defaults to X86_64).
462 */
463 readonly architecture: Architecture;
464 /**
465 * The timeout configured for this lambda.
466 */
467 readonly timeout?: Duration;
468 readonly permissionsNode: import("@aws-cdk/core").ConstructNode;
469 protected readonly canCreatePermissions = true;
470 private readonly layers;
471 private _logGroup?;
472 /**
473 * Environment variables for this function
474 */
475 private environment;
476 private readonly currentVersionOptions?;
477 private _currentVersion?;
478 private _architecture?;
479 constructor(scope: Construct, id: string, props: FunctionProps);
480 /**
481 * Adds an environment variable to this Lambda function.
482 * If this is a ref to a Lambda function, this operation results in a no-op.
483 * @param key The environment variable key.
484 * @param value The environment variable's value.
485 * @param options Environment variable options.
486 */
487 addEnvironment(key: string, value: string, options?: EnvironmentOptions): this;
488 /**
489 * Adds one or more Lambda Layers to this Lambda function.
490 *
491 * @param layers the layers to be added.
492 *
493 * @throws if there are already 5 layers on this function, or the layer is incompatible with this function's runtime.
494 */
495 addLayers(...layers: ILayerVersion[]): void;
496 /**
497 * Add a new version for this Lambda
498 *
499 * If you want to deploy through CloudFormation and use aliases, you need to
500 * add a new version (with a new name) to your Lambda every time you want to
501 * deploy an update. An alias can then refer to the newly created Version.
502 *
503 * All versions should have distinct names, and you should not delete versions
504 * as long as your Alias needs to refer to them.
505 *
506 * @param name A unique name for this version.
507 * @param codeSha256 The SHA-256 hash of the most recently deployed Lambda
508 * source code, or omit to skip validation.
509 * @param description A description for this version.
510 * @param provisionedExecutions A provisioned concurrency configuration for a
511 * function's version.
512 * @param asyncInvokeConfig configuration for this version when it is invoked
513 * asynchronously.
514 * @returns A new Version object.
515 *
516 * @deprecated This method will create an AWS::Lambda::Version resource which
517 * snapshots the AWS Lambda function *at the time of its creation* and it
518 * won't get updated when the function changes. Instead, use
519 * `this.currentVersion` to obtain a reference to a version resource that gets
520 * automatically recreated when the function configuration (or code) changes.
521 */
522 addVersion(name: string, codeSha256?: string, description?: string, provisionedExecutions?: number, asyncInvokeConfig?: EventInvokeConfigOptions): Version;
523 /**
524 * The LogGroup where the Lambda function's logs are made available.
525 *
526 * If either `logRetention` is set or this property is called, a CloudFormation custom resource is added to the stack that
527 * pre-creates the log group as part of the stack deployment, if it already doesn't exist, and sets the correct log retention
528 * period (never expire, by default).
529 *
530 * Further, if the log group already exists and the `logRetention` is not set, the custom resource will reset the log retention
531 * to never expire even if it was configured with a different value.
532 */
533 get logGroup(): logs.ILogGroup;
534 /** @internal */
535 _checkEdgeCompatibility(): void;
536 /**
537 * Configured lambda insights on the function if specified. This is acheived by adding an imported layer which is added to the
538 * list of lambda layers on synthesis.
539 *
540 * https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Lambda-Insights-extension-versions.html
541 */
542 private configureLambdaInsights;
543 private renderEnvironment;
544 /**
545 * If configured, set up the VPC-related properties
546 *
547 * Returns the VpcConfig that should be added to the
548 * Lambda creation properties.
549 */
550 private configureVpc;
551 private isQueue;
552 private buildDeadLetterQueue;
553 private buildDeadLetterConfig;
554 private buildTracingConfig;
555 private validateProfiling;
556}
557/**
558 * Environment variables options
559 */
560export interface EnvironmentOptions {
561 /**
562 * When used in Lambda@Edge via edgeArn() API, these environment
563 * variables will be removed. If not set, an error will be thrown.
564 * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-requirements-lambda-function-configuration
565 *
566 * @default false - using the function in Lambda@Edge will throw
567 */
568 readonly removeInEdge?: boolean;
569}
570export declare function verifyCodeConfig(code: CodeConfig, props: FunctionProps): void;