Skip to main content

AwsCloudFrontServiceModule

AwsCloudFrontServiceModule creates one CloudFront distribution serving buckets and Lambda functions over HTTPS from the edge, with its origins, routes, cache and header policies, functions, and your own domain (aliases, certificateArn).

It is given its origins, the models it serves, and its routes, the paths that pick an origin, the way CloudFront itself is configured. For each source it creates an overlay, and grants the distribution access to it through an origin access control: for a bucket from AwsS3StorageServiceModule, a statement in the bucket's policy; for a Lambda function from AwsLambdaExecutionModule, a permission of its own admitting this distribution to call the function through its URL, even a function of an app this one extends. An ALB from AwsEcsAlbServiceModule is reached over HTTP through its DNS name, and is granted nothing: it still answers anyone who calls it directly. It sits under the account. To drop the edges' copies of a source's files when they change, add AwsCloudFrontInvalidationServiceModule.

It creates a CloudFront Function from each file in functions, which a route runs by name.

A route whose origin is a Lambda function or an ALB defaults to every method, no caching, and every viewer header but Host. Behind a Lambda function, a client sending a body - a POST or a PUT - must also send its SHA-256 in the x-amz-content-sha256 header, because CloudFront signs the request it forwards to the function. CloudFront waits 60 seconds for the function, so a function with a longer timeout is rejected.

@cost

CloudFront bills data delivered to viewers (about $0.085 per GB in North America and Europe) and requests (about $0.01 per 10,000 HTTPS requests), after an always-free 1 TB and 10 million requests a month. A CloudFront Function bills about $0.10 per million invocations after 2 million free. priceClass PriceClass_100 serves only from North America and Europe, the cheapest edges. Transfer from an S3 origin to CloudFront is free.

@downtime

None: a change updates the distribution in place, and every edge serves the old configuration until the new one reaches it, which takes a few minutes.

@example

TypeScript

import { type Account, type Service, stub } from '@quadnix/octo';
import { AwsCloudFrontServiceModule } from '@quadnix/octo-aws-cdk/modules/service/aws-cloudfront-service';

octo.loadModule(AwsCloudFrontServiceModule, 'my-distribution-module', {
account: stub<Account>('${{my-account-module.model.account}}'),
aliases: ['www.example.com'],
certificateArn: 'arn:aws:acm:us-east-1:123456789012:certificate/0a1b2c3d-4e5f-6a7b-8c9d-0e1f2a3b4c5d',
defaultRootObject: 'index.html',
distributionName: 'my-website',
errorResponses: [{ errorCode: 403, responseCode: 404, responsePagePath: '/error.html' }],
origins: [
{ originId: 'site', source: stub<Service>('${{my-content-module.model.service}}') },
],
routes: [
{ originId: 'site', pathPattern: '/assets/*' },
{ originId: 'site' },
],
});
@group

Modules/Service/AwsCloudFrontService

@limit

CloudFront waits at most 60 seconds for an origin to answer, so a Lambda function with a longer timeout is rejected. A body sent to a Lambda function must carry its SHA-256 in x-amz-content-sha256.

@limit

certificateArn must name an ACM certificate in us-east-1, whatever region the origins are in, and it is required with aliases.

@limit

A CloudFront Function's code is at most 10 KB, and it can neither reach the network nor read a request body.

@limit

No WAF web ACL can be attached, so the distribution cannot block requests by rule, address or rate.

@limit

The distribution serves every country: no geographic restriction can be set.

@limit

No origin has a failover origin: when an origin fails, the request fails.

@limit

An S3 origin is a bucket of this app: a bucket an extended or stored app holds cannot be served.

@limit

Custom error responses apply to every route of the distribution, so a route cannot have an error page of its own.

@limit

The distribution writes no access logs.

@limit

With certificateArn, viewers connect over SNI only, with TLS 1.2 as the oldest protocol; a client without SNI cannot connect.

@losesData

Nothing: a distribution holds no data, only cached copies of its origins.

@replaces

Changing account replaces the distribution and its address. Every other input changes it in place.

@see

Hierarchy

Index

Constructors

constructor

  • Parameters

    • moduleId: string

      The ID this module is loaded under.

    Returns AwsCloudFrontServiceModule

Properties

readonlyinheritedmoduleId

moduleId: string

The ID this module was loaded under. It scopes everything the module produces, so the same module class can be loaded more than once.

staticreadonlyinheritedMODULE_PACKAGE

MODULE_PACKAGE: string

The package the module is registered under, set by Module.

staticreadonlyinheritedMODULE_SCHEMA

MODULE_SCHEMA: any

The schema of the module's inputs, set by Module.

Methods

onInit

  • onInit(inputs: AwsCloudFrontServiceModuleSchema): Promise<[AwsCloudFrontService, ...(AwsCloudFrontAlbOriginOverlay | AwsCloudFrontLambdaOriginOverlay | AwsCloudFrontS3OriginOverlay)[]]>
  • Validates the origins and routes, creates the distribution's service under the account, and an overlay for each origin's source.


    Parameters

    Returns Promise<[AwsCloudFrontService, ...(AwsCloudFrontAlbOriginOverlay | AwsCloudFrontLambdaOriginOverlay | AwsCloudFrontS3OriginOverlay)[]]>

inheritedregisterHooks

  • registerHooks(): { postCommitHooks?: PostHookSignature[]; postModelActionHooks?: PostHookSignature[]; postResourceActionHooks?: PostHookSignature[]; preCommitHooks?: PreHookSignature[]; preModelActionHooks?: PreHookSignature[]; preResourceActionHooks?: PreHookSignature[] }
  • To hook custom behavior into Octo's lifecycle for the duration of this module. Override it to return the callbacks you want run.

    @see

    Techniques for Hooks.


    Returns { postCommitHooks?: PostHookSignature[]; postModelActionHooks?: PostHookSignature[]; postResourceActionHooks?: PostHookSignature[]; preCommitHooks?: PreHookSignature[]; preModelActionHooks?: PreHookSignature[]; preResourceActionHooks?: PreHookSignature[] }

    • optionalpostCommitHooks?: PostHookSignature[]
    • optionalpostModelActionHooks?: PostHookSignature[]
    • optionalpostResourceActionHooks?: PostHookSignature[]
    • optionalpreCommitHooks?: PreHookSignature[]
    • optionalpreModelActionHooks?: PreHookSignature[]
    • optionalpreResourceActionHooks?: PreHookSignature[]

registerMetadata

  • registerMetadata(inputs: AwsCloudFrontServiceModuleSchema): Promise<{ awsAccountId: string; indexFunctions: { code: string; name: string; originId: string }[] }>
  • Resolves the account this distribution is created in.


    Parameters

    Returns Promise<{ awsAccountId: string; indexFunctions: { code: string; name: string; originId: string }[] }>