Skip to content

Commit

Permalink
feat(type-safe-api): allow customized output spec bucket (aws#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Cozzi committed Jul 24, 2024
1 parent 7f24295 commit 8ae14aa
Show file tree
Hide file tree
Showing 3 changed files with 1,511 additions and 4 deletions.
16 changes: 13 additions & 3 deletions packages/type-safe-api/src/construct/type-safe-rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Runtime,
} from "aws-cdk-lib/aws-lambda";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { IBucket } from "aws-cdk-lib/aws-s3";
import { Asset } from "aws-cdk-lib/aws-s3-assets";
import {
CfnIPSet,
Expand Down Expand Up @@ -77,6 +78,12 @@ export interface TypeSafeRestApiProps
* @default - Compression is disabled.
*/
readonly minCompressionSize?: Size;

/**
* By default, the spec is prepared and outputted into the CDK assets bucket. If this is undesired,
* use this option to specify the output bucket.
*/
readonly outputSpecBucket?: IBucket;
}

/**
Expand Down Expand Up @@ -113,13 +120,16 @@ export class TypeSafeRestApi extends Construct {
operationLookup,
defaultAuthorizer,
corsOptions,
outputSpecBucket,
...options
} = props;

// Upload the spec to s3 as an asset
const inputSpecAsset = new Asset(this, "InputSpec", {
path: specPath,
});

const prepareSpecOutputBucket = outputSpecBucket ?? inputSpecAsset.bucket;
// We'll output the prepared spec in the same asset bucket
const preparedSpecOutputKeyPrefix = `${inputSpecAsset.s3ObjectKey}-prepared`;

Expand Down Expand Up @@ -165,7 +175,7 @@ export class TypeSafeRestApi extends Construct {
resources: [
// The output file will include a hash of the prepared spec, which is not known until deploy time since
// tokens must be resolved
inputSpecAsset.bucket.arnForObjects(
prepareSpecOutputBucket.arnForObjects(
`${preparedSpecOutputKeyPrefix}/*`
),
],
Expand Down Expand Up @@ -340,7 +350,7 @@ export class TypeSafeRestApi extends Construct {
key: inputSpecAsset.s3ObjectKey,
},
outputSpecLocation: {
bucket: inputSpecAsset.bucket.bucketName,
bucket: prepareSpecOutputBucket.bucketName,
key: preparedSpecOutputKeyPrefix,
},
...prepareSpecOptions,
Expand All @@ -361,7 +371,7 @@ export class TypeSafeRestApi extends Construct {
apiDefinition: this.node.tryGetContext("type-safe-api-local")
? ApiDefinition.fromInline(this.extendedApiSpecification)
: ApiDefinition.fromBucket(
inputSpecAsset.bucket,
prepareSpecOutputBucket,
prepareSpecCustomResource.getAttString("outputSpecKey")
),
deployOptions: {
Expand Down
Loading

0 comments on commit 8ae14aa

Please sign in to comment.