UNPKG

@faceteer/cdk

Version:

CDK 2.0 constructs and helpers that make composing a Lambda powered service easier.

140 lines (139 loc) 5.58 kB
import * as cdk from 'aws-cdk-lib'; import * as apigwv2 from 'aws-cdk-lib/aws-apigatewayv2'; import * as iam from 'aws-cdk-lib/aws-iam'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as lambdaNodeJs from 'aws-cdk-lib/aws-lambda-nodejs'; import * as acm from 'aws-cdk-lib/aws-certificatemanager'; import * as route53 from 'aws-cdk-lib/aws-route53'; import * as events from 'aws-cdk-lib/aws-events'; import { Construct } from 'constructs'; import { ServiceQueueFunction } from './service-queue-function'; import { JwtAuthorizerConfig, LambdaAuthorizerConfig } from './api-gateway'; import { CfnAuthorizer } from 'aws-cdk-lib/aws-apigatewayv2'; import { ISecurityGroup, IVpc, SubnetSelection } from 'aws-cdk-lib/aws-ec2'; import { LogRetentionDays } from '../util/log-retention'; import { HandlerDefinition } from '../handlers'; export interface LambdaServiceProps { /** The path to the folder where the handlers are stored. * * If omitted, then the service will have no handlers. This can be useful if * you want to create a dummy service that will contain all the resources you * may need like an api gateway, which you can then pass into other actual * services. */ handlersFolder?: string; /** The API gateway that the API handlers in this service should be attached * to. * * If this is not provided and the service includes API handlers, a new API * gateway will be created. */ api?: apigwv2.CfnApi; /** The API gateway stage that the API handlers in this service should be * attached to. * * If this is not provided and the service includes API handlers, a new API * gateway stage will be created. */ stage?: apigwv2.CfnStage; /** The Authorizer to use for the API handlers. * * This can either be an actual authorizer, in which case we'll use it. Or it * can be a configuration for either a Jwt or Lambda authorizer, in which case * we'll create a new authorizer with that configuration. */ authorizer?: JwtAuthorizerConfig | LambdaAuthorizerConfig | CfnAuthorizer; /** @deprecated Please use the same value on {@link authorizer} instead. */ jwtAuthorizer?: { identitySource: string[]; audience: string[]; issuer: string; }; /** @deprecated Please use the same value on {@link authorizer} instead. */ lambdaAuthorizer?: { fn: lambda.IFunction; identitySource: string[]; enableSimpleResponses?: boolean; }; /** The default options that will apply to all handlers. * * These options apply to all handlers. * They can be overridden in the handler configuration itself. */ defaults?: { scopes?: string[]; memorySize?: number; timeout?: number; vpc?: boolean; logRetention?: 'destroy' | 'retain'; logRetentionDuration?: LogRetentionDays; runtime?: HandlerDefinition['runtime']; architecture?: HandlerDefinition['architecture']; }; /** VPC, subnet, and security groups for the lambda functions. * * If provided, all functions will be created in the VPC by default. You can * override that by setting `vpc: false`, either globally in {@link defaults} * or per-function in the function handler definition. */ network?: { /** The VPC that the Lambda handlers should run in. */ vpc: IVpc; /** The VPC subnets that the Lambda handlers should run in. * * If undefined, the Vpc default strategy is used. */ vpcSubnets?: SubnetSelection; /** The security groups that apply to the Lambda handlers. * * If undefined, */ securityGroups?: ISecurityGroup[]; }; /** @deprecated Use `defaults.scopes` */ defaultScopes?: string[]; bundlingOptions?: lambdaNodeJs.BundlingOptions; role?: iam.IRole; layers?: lambda.ILayerVersion[]; domain?: { certificate: acm.ICertificate; domainName: string; route53Zone?: route53.IHostedZone; }; /** * Use the key to reference the appropriate event bus in your Event Handler definition. */ eventBuses?: { [key: string]: events.IEventBus; }; } export declare class LambdaService extends Construct implements iam.IGrantable { readonly api: apigwv2.CfnApi; readonly stage: apigwv2.CfnStage; readonly grantPrincipal: iam.IPrincipal; readonly authorizer?: apigwv2.CfnAuthorizer; /** Maps queue names to the queue handlers of this service, if any. */ queues: Map<string, ServiceQueueFunction>; functions: lambda.Function[]; private environmentVariables; private snsTopics; constructor(scope: Construct, id: string, { handlersFolder, authorizer, jwtAuthorizer, lambdaAuthorizer, bundlingOptions, role, defaults, defaultScopes, domain, eventBuses, api, stage, layers, network, }: LambdaServiceProps); /** * Add an environment variable to the service * @param key * @param value */ addEnvironment(key: string, value: string): void; /** Allows this service to send messages to the queue handled by this * function. * * This is only necessary if you are sending messages across services. * The service always has access to its own queues. */ grantSendToQueue(queueFn: ServiceQueueFunction): void; /** * Retrieves an SNS topic by it's name * @param topicName */ getSnsTopic(topicName: string): cdk.aws_sns.Topic; }