UNPKG

@scloud/cdk-patterns

Version:

Serverless CDK patterns for common infrastructure needs

89 lines (88 loc) 4.44 kB
import { Construct } from 'constructs'; import { DnsValidatedCertificate } from 'aws-cdk-lib/aws-certificatemanager'; import { Bucket } from 'aws-cdk-lib/aws-s3'; import { RestApiOrigin } from 'aws-cdk-lib/aws-cloudfront-origins'; import { Distribution, DistributionProps, ErrorResponse } from 'aws-cdk-lib/aws-cloudfront'; import { CognitoUserPoolsAuthorizer, LambdaRestApi, LambdaRestApiProps } from 'aws-cdk-lib/aws-apigateway'; import { Function } from 'aws-cdk-lib/aws-lambda'; import { UserPool } from 'aws-cdk-lib/aws-cognito'; import { IHostedZone } from 'aws-cdk-lib/aws-route53'; /** * @param zone The DNS zone for this web app. By default the domain name is set to the zone name * The type IHostedZone enables lookup of the zone (IHostedZone) as well as a zone creatd in the stack (HostedZone) * @param domain Optional: by default the zone name will be used as the DNS name for the Cloudfront distribution (e.g. 'example.com') but you can specify a different domain here (e.g. 'subdomain.example.com'). * @param defaultIndex Default: true. Maps a viewer request for '/' to a request for /index.html. * @param wwwRedirect Default: true. Redirects requests for www. to the bare domain name, e.g. www.example.com->example.com, www.sub.example.com->sub.example.com. * @param distributionProps Any properties for the distribution you'd like to add or override * @param functionAssociation A Cloudfront function to be associated with the default behavior (s3 static content) * @param distributionProps Optional: If you want to add additional properties to the Cloudfront distribution, you can pass them here. * @param errorResponses Optional: If you want to add custom error responses to the Cloudfront distribution, you can pass them here. */ export interface WebRoutesProps { zone: IHostedZone; domainName?: string; defaultIndex?: boolean | string; redirectWww?: boolean; distributionProps?: Partial<DistributionProps>; errorResponses?: ErrorResponse[]; } /** * Builds a web application, backed by Lambda functions that serve specific routes (https://github.com/cdk-patterns/serverless/blob/main/the-lambda-trilogy/README.md) * * NB This will create an s3 bucket to serve static content and the bucket will be automatically deleted, including all contents, when this construct is deleted, on the basis the contents are assumed to be produced by a CI build. * * This construct can also be used for a Web API. * * NB us-east-1 is required for Cloudfront certificates: * https://docs.aws.amazon.com/cdk/api/v1/docs/aws-cloudfront-readme.html * */ export declare class WebRoutes extends Construct { lambda: Function; bucket: Bucket; distribution: Distribution; certificate: DnsValidatedCertificate; routes: { [pathPattern: string]: Function; }; origins: { [id: string]: RestApiOrigin; }; apis: { [id: string]: LambdaRestApi; }; cognitoPool: UserPool; authorizer: CognitoUserPoolsAuthorizer; constructor(scope: Construct, id: string, props: WebRoutesProps); /** * Add multiple routes to the web app. * * NB AWS has a soft limit of 25 origins per distribution. * If you need more than this you'll need to request a quota increate wia the AWS console. */ addRoutes(routes: { [path: string]: Function; }): void; /** * Add a route to the web app. * * NB AWS has a soft limit of 25 origins per distribution. * If you need more than this you'll need to request a quota increate wia the AWS console. */ addRoute(pathPattern: string, handler: Function, lambdaRestApiProps?: Partial<LambdaRestApiProps>): void; /** * Not yet implemented! */ addAuthorizer(cognitoPool: UserPool): void; /** * Builds a WebRoutes construct with multiple routes, based on a set of pre-built Lambda functions. * * This is useful if your routes use different runtimes, environment variables an/or function properties. * * @param webRoutesProps Properties to configure the WebRoutes construct * @param lambdaRestApiProps (optional) Properties to configure the LambdaRestApis that will route to your functions */ static routes(scope: Construct, id: string, routes: { [pathPattern: string]: Function; }, webRoutesProps: WebRoutesProps, lambdaRestApiProps?: Partial<LambdaRestApiProps>): WebRoutes; }