Skip to main content

AwsS3StorageServiceModule

AwsS3StorageServiceModule creates a private S3 bucket for files, such as user uploads or a site CloudFront serves, with the key prefixes a server is granted through AwsServerModule (accessPrefixes), CORS and lifecycle rules, and optionally a local directory uploaded into it.

The bucket is closed to the public by default. To serve its files, pass it as an origin to AwsCloudFrontServiceModule, which grants its distribution access. For a bucket that serves a website on its own, use AwsS3StaticWebsiteServiceModule. The bucket lives in a region created by any of the AWS region modules.

@cost

S3 bills what the bucket holds and serves: storage per GB-month (about $0.023 for S3 Standard in us-east-1), each request (about $0.005 per 1,000 writes and $0.0004 per 1,000 reads), and data sent to the internet beyond the 100 GB a month AWS gives free. Served through AwsCloudFrontServiceModule, the bucket sends to CloudFront for free and CloudFront bills the delivery instead. An empty bucket costs nothing.

@downtime

None for a change made in place: access prefixes, public access blocking, CORS rules, lifecycle rules and the uploaded files all update without interrupting the bucket. A replacement first deletes the bucket, which cannot happen while it holds an object octo did not upload.

@example

TypeScript

import { type Region, stub } from '@quadnix/octo';
import { AwsS3StorageServiceModule } from '@quadnix/octo-aws-cdk/modules/service/aws-s3-storage-service';

octo.loadModule(AwsS3StorageServiceModule, 'my-storage-module', {
accessPrefixes: ['uploads', 'documents', 'images'],
bucketName: 'my-app-storage',
corsRules: [{ allowHeaders: ['Range'], allowMethods: ['GET'], allowOrigins: ['https://app.example.com'] }],
directoryPath: 'website',
filter: (filePath) => !filePath.includes('.DS_Store'),
lifecycleRules: [{ expirationInDays: 1, tags: { upload: 'pending' } }],
region: stub<Region>('${{my-region-module.model.region}}')
});
@group

Modules/Service/AwsS3StorageService

@limit

A bucket name is global across every AWS account: a name anyone already holds is refused. It is 3 to 63 lowercase letters, numbers, dots and hyphens.

@limit

S3 serves at least 3,500 writes and 5,500 reads a second for each key prefix, and one object holds up to 5 TB.

@limit

Objects are not versioned and the bucket is not replicated: an object overwritten or deleted, by octo or by an app, cannot be recovered.

@losesData

Deleting the module, or a change that replaces the bucket, deletes every object in it, which octo cannot put back. The files uploaded from directoryPath are uploaded again; anything else written to the bucket is gone. Terraform refuses to delete a bucket that still holds such an object, so the apply fails until it is emptied by hand.

@replaces

Changing bucketName or region replaces the bucket, because S3 can neither rename nor move one. Every other input changes it in place.

@see

Hierarchy

Index

Constructors

constructor

  • Parameters

    • moduleId: string

      The ID this module is loaded under.

    Returns AwsS3StorageServiceModule

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

  • Creates the bucket service, registers the local files it uploads, and registers each access prefix in it that other modules may be granted access to.


    Parameters

    Returns Promise<AwsS3StorageService>

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

  • Resolves the account and region this bucket is created in. The region may be networked or serverless; the bucket only needs its AWS region ID.


    Parameters

    Returns Promise<{ awsAccountId: string; awsRegionId: string }>