UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

394 lines (393 loc) 15.8 kB
import { Construct } from 'constructs'; import * as ecr from '../../aws-ecr'; import * as ecr_assets from '../../aws-ecr-assets'; import { IKey } from '../../aws-kms'; import * as s3 from '../../aws-s3'; import * as s3_assets from '../../aws-s3-assets'; import * as cdk from '../../core'; /** * Represents the Lambda Handler Code. */ export declare abstract class Code { /** * Lambda handler code as an S3 object. * @param bucket The S3 bucket * @param key The object key * @param objectVersion Optional S3 object version */ static fromBucket(bucket: s3.IBucket, key: string, objectVersion?: string): S3Code; /** * Lambda handler code as an S3 object. * @param bucket The S3 bucket * @param key The object key * @param options Optional parameters for setting the code, current optional parameters to set here are * 1. `objectVersion` to set S3 object version * 2. `sourceKMSKey` to set KMS Key for encryption of code */ static fromBucketV2(bucket: s3.IBucket, key: string, options?: BucketOptions): S3CodeV2; /** * Inline code for Lambda handler * @returns `LambdaInlineCode` with inline code. * @param code The actual handler code (the resulting zip file cannot exceed 4MB) */ static fromInline(code: string): InlineCode; /** * Loads the function code from a local disk path. * * @param path Either a directory with the Lambda code bundle or a .zip file */ static fromAsset(path: string, options?: s3_assets.AssetOptions): AssetCode; /** * Runs a command to build the code asset that will be used. * * @param output Where the output of the command will be directed, either a directory or a .zip file with the output Lambda code bundle * * For example, if you use the command to run a build script (e.g., [ 'node', 'bundle_code.js' ]), and the build script generates a directory `/my/lambda/code` * containing code that should be ran in a Lambda function, then output should be set to `/my/lambda/code` * @param command The command which will be executed to generate the output, for example, [ 'node', 'bundle_code.js' ] * @param options options for the custom command, and other asset options -- but bundling options are not allowed. */ static fromCustomCommand(output: string, command: string[], options?: CustomCommandOptions): AssetCode; /** * Loads the function code from an asset created by a Docker build. * * By default, the asset is expected to be located at `/asset` in the * image. * * @param path The path to the directory containing the Docker file * @param options Docker build options */ static fromDockerBuild(path: string, options?: DockerBuildAssetOptions): AssetCode; /** * Creates a new Lambda source defined using CloudFormation parameters. * * @returns a new instance of `CfnParametersCode` * @param props optional construction properties of `CfnParametersCode` */ static fromCfnParameters(props?: CfnParametersCodeProps): CfnParametersCode; /** * Use an existing ECR image as the Lambda code. * @param repository the ECR repository that the image is in * @param props properties to further configure the selected image */ static fromEcrImage(repository: ecr.IRepository, props?: EcrImageCodeProps): EcrImageCode; /** * Create an ECR image from the specified asset and bind it as the Lambda code. * @param directory the directory from which the asset must be created * @param props properties to further configure the selected image */ static fromAssetImage(directory: string, props?: AssetImageCodeProps): AssetImageCode; /** * Called when the lambda or layer is initialized to allow this object to bind * to the stack, add resources and have fun. * * @param scope The binding scope. Don't be smart about trying to down-cast or * assume it's initialized. You may just use it as a construct scope. */ abstract bind(scope: Construct): CodeConfig; /** * Called after the CFN function resource has been created to allow the code * class to bind to it. Specifically it's required to allow assets to add * metadata for tooling like SAM CLI to be able to find their origins. */ bindToResource(_resource: cdk.CfnResource, _options?: ResourceBindOptions): void; } /** * Result of binding `Code` into a `Function`. */ export interface CodeConfig { /** * The location of the code in S3 (mutually exclusive with `inlineCode` and `image`). * @default - code is not an s3 location */ readonly s3Location?: s3.Location; /** * Inline code (mutually exclusive with `s3Location` and `image`). * @default - code is not inline code */ readonly inlineCode?: string; /** * Docker image configuration (mutually exclusive with `s3Location` and `inlineCode`). * @default - code is not an ECR container image */ readonly image?: CodeImageConfig; /** * The ARN of the KMS key used to encrypt the handler code. * @default - the default server-side encryption with Amazon S3 managed keys(SSE-S3) key will be used. */ readonly sourceKMSKeyArn?: string; } /** * Result of the bind when an ECR image is used. */ export interface CodeImageConfig { /** * URI to the Docker image. */ readonly imageUri: string; /** * Specify or override the CMD on the specified Docker image or Dockerfile. * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`. * @see https://docs.docker.com/engine/reference/builder/#cmd * @default - use the CMD specified in the docker image or Dockerfile. */ readonly cmd?: string[]; /** * Specify or override the ENTRYPOINT on the specified Docker image or Dockerfile. * An ENTRYPOINT allows you to configure a container that will run as an executable. * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`. * @see https://docs.docker.com/engine/reference/builder/#entrypoint * @default - use the ENTRYPOINT in the docker image or Dockerfile. */ readonly entrypoint?: string[]; /** * Specify or override the WORKDIR on the specified Docker image or Dockerfile. * A WORKDIR allows you to configure the working directory the container will use. * @see https://docs.docker.com/engine/reference/builder/#workdir * @default - use the WORKDIR in the docker image or Dockerfile. */ readonly workingDirectory?: string; } /** * Lambda code from an S3 archive. */ export declare class S3Code extends Code { private key; private objectVersion?; readonly isInline = false; private bucketName; constructor(bucket: s3.IBucket, key: string, objectVersion?: string | undefined); bind(_scope: Construct): CodeConfig; } /** * Lambda code from an S3 archive. With option to set KMSKey for encryption. */ export declare class S3CodeV2 extends Code { private key; private options?; readonly isInline = false; private bucketName; constructor(bucket: s3.IBucket, key: string, options?: BucketOptions | undefined); bind(_scope: Construct): CodeConfig; } /** * Lambda code from an inline string. */ export declare class InlineCode extends Code { private code; readonly isInline = true; constructor(code: string); bind(_scope: Construct): CodeConfig; } /** * Lambda code from a local directory. */ export declare class AssetCode extends Code { readonly path: string; private readonly options; readonly isInline = false; private asset?; /** * @param path The path to the asset file or directory. */ constructor(path: string, options?: s3_assets.AssetOptions); bind(scope: Construct): CodeConfig; bindToResource(resource: cdk.CfnResource, options?: ResourceBindOptions): void; } export interface ResourceBindOptions { /** * The name of the CloudFormation property to annotate with asset metadata. * @see https://github.com/aws/aws-cdk/issues/1432 * @default Code */ readonly resourceProperty?: string; } /** * Construction properties for `CfnParametersCode`. */ export interface CfnParametersCodeProps { /** * The CloudFormation parameter that represents the name of the S3 Bucket * where the Lambda code will be located in. * Must be of type 'String'. * * @default a new parameter will be created */ readonly bucketNameParam?: cdk.CfnParameter; /** * The CloudFormation parameter that represents the path inside the S3 Bucket * where the Lambda code will be located at. * Must be of type 'String'. * * @default a new parameter will be created */ readonly objectKeyParam?: cdk.CfnParameter; /** * The ARN of the KMS key used to encrypt the handler code. * @default - the default server-side encryption with Amazon S3 managed keys(SSE-S3) key will be used. */ readonly sourceKMSKey?: IKey; } /** * Lambda code defined using 2 CloudFormation parameters. * Useful when you don't have access to the code of your Lambda from your CDK code, so you can't use Assets, * and you want to deploy the Lambda in a CodePipeline, using CloudFormation Actions - * you can fill the parameters using the `#assign` method. */ export declare class CfnParametersCode extends Code { readonly isInline = false; private _bucketNameParam?; private _objectKeyParam?; private _sourceKMSKey?; constructor(props?: CfnParametersCodeProps); bind(scope: Construct): CodeConfig; /** * Create a parameters map from this instance's CloudFormation parameters. * * It returns a map with 2 keys that correspond to the names of the parameters defined in this Lambda code, * and as values it contains the appropriate expressions pointing at the provided S3 location * (most likely, obtained from a CodePipeline Artifact by calling the `artifact.s3Location` method). * The result should be provided to the CloudFormation Action * that is deploying the Stack that the Lambda with this code is part of, * in the `parameterOverrides` property. * * @param location the location of the object in S3 that represents the Lambda code */ assign(location: s3.Location): { [name: string]: any; }; get bucketNameParam(): string; get objectKeyParam(): string; } /** * Properties to initialize a new EcrImageCode */ export interface EcrImageCodeProps { /** * Specify or override the CMD on the specified Docker image or Dockerfile. * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`. * @see https://docs.docker.com/engine/reference/builder/#cmd * @default - use the CMD specified in the docker image or Dockerfile. */ readonly cmd?: string[]; /** * Specify or override the ENTRYPOINT on the specified Docker image or Dockerfile. * An ENTRYPOINT allows you to configure a container that will run as an executable. * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`. * @see https://docs.docker.com/engine/reference/builder/#entrypoint * @default - use the ENTRYPOINT in the docker image or Dockerfile. */ readonly entrypoint?: string[]; /** * Specify or override the WORKDIR on the specified Docker image or Dockerfile. * A WORKDIR allows you to configure the working directory the container will use. * @see https://docs.docker.com/engine/reference/builder/#workdir * @default - use the WORKDIR in the docker image or Dockerfile. */ readonly workingDirectory?: string; /** * The image tag to use when pulling the image from ECR. * @default 'latest' * @deprecated use `tagOrDigest` */ readonly tag?: string; /** * The image tag or digest to use when pulling the image from ECR (digests must start with `sha256:`). * @default 'latest' */ readonly tagOrDigest?: string; } /** * Represents a Docker image in ECR that can be bound as Lambda Code. */ export declare class EcrImageCode extends Code { private readonly repository; private readonly props; readonly isInline: boolean; constructor(repository: ecr.IRepository, props?: EcrImageCodeProps); bind(_scope: Construct): CodeConfig; } /** * Properties to initialize a new AssetImage */ export interface AssetImageCodeProps extends ecr_assets.DockerImageAssetOptions { /** * Specify or override the CMD on the specified Docker image or Dockerfile. * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`. * @see https://docs.docker.com/engine/reference/builder/#cmd * @default - use the CMD specified in the docker image or Dockerfile. */ readonly cmd?: string[]; /** * Specify or override the ENTRYPOINT on the specified Docker image or Dockerfile. * An ENTRYPOINT allows you to configure a container that will run as an executable. * This needs to be in the 'exec form', viz., `[ 'executable', 'param1', 'param2' ]`. * @see https://docs.docker.com/engine/reference/builder/#entrypoint * @default - use the ENTRYPOINT in the docker image or Dockerfile. */ readonly entrypoint?: string[]; /** * Specify or override the WORKDIR on the specified Docker image or Dockerfile. * A WORKDIR allows you to configure the working directory the container will use. * @see https://docs.docker.com/engine/reference/builder/#workdir * @default - use the WORKDIR in the docker image or Dockerfile. */ readonly workingDirectory?: string; } /** * Represents an ECR image that will be constructed from the specified asset and can be bound as Lambda code. */ export declare class AssetImageCode extends Code { private readonly directory; private readonly props; readonly isInline: boolean; private asset?; constructor(directory: string, props: AssetImageCodeProps); bind(scope: Construct): CodeConfig; bindToResource(resource: cdk.CfnResource, options?: ResourceBindOptions): void; } /** * Options when creating an asset from a Docker build. */ export interface DockerBuildAssetOptions extends cdk.DockerBuildOptions { /** * The path in the Docker image where the asset is located after the build * operation. * * @default /asset */ readonly imagePath?: string; /** * The path on the local filesystem where the asset will be copied * using `docker cp`. * * @default - a unique temporary directory in the system temp directory */ readonly outputPath?: string; } /** * Options for creating `AssetCode` with a custom command, such as running a buildfile. */ export interface CustomCommandOptions extends s3_assets.AssetOptions { /** * options that are passed to the spawned process, which determine the characteristics of the spawned process. * * @default: see `child_process.SpawnSyncOptions` (https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options). */ readonly commandOptions?: { [options: string]: any; }; } /** * Optional parameters for creating code using bucket */ export interface BucketOptions { /** * Optional S3 object version */ readonly objectVersion?: string; /** * The ARN of the KMS key used to encrypt the handler code. * @default - the default server-side encryption with Amazon S3 managed keys(SSE-S3) key will be used. */ readonly sourceKMSKey?: IKey; }