Skip to content

Commit

Permalink
feat: make Code compatible to @aws-cdk/aws-synthetics (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Momo Kornher authored Oct 7, 2021
1 parent b86ad3b commit f840300
Show file tree
Hide file tree
Showing 5 changed files with 801 additions and 25 deletions.
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install @mrgrain/cdk-esbuild
⚠️ When using an older version of npm (4-6), the required peer dependencies have to be installed manually. Use this command instead:

```
npm install @mrgrain/cdk-esbuild @aws-cdk/core @aws-cdk/aws-lambda @aws-cdk/aws-s3-assets @aws-cdk/aws-s3-deployment
npm install @mrgrain/cdk-esbuild @aws-cdk/core @aws-cdk/aws-lambda @aws-cdk/aws-s3-assets @aws-cdk/aws-s3-deployment @aws-cdk/aws-synthetics
```

### Lambda function
Expand All @@ -39,7 +39,7 @@ import { TypeScriptCode } from "@mrgrain/cdk-esbuild";

const bundledCode = new TypeScriptCode("src/index.ts");

const fn = new lambda.Function(this, "MyFunction", {
const fn = new lambda.Function(stack, "MyFunction", {
runtime: lambda.Runtime.NODEJS_14_X,
handler: "index.handler",
code: bundledCode,
Expand All @@ -59,26 +59,51 @@ import { TypeScriptSource } from "@mrgrain/cdk-esbuild";

const websiteBundle = new TypeScriptSource("src/index.tsx");

const websiteBucket = new s3.Bucket(this, "WebsiteBucket", {
const websiteBucket = new s3.Bucket(stack, "WebsiteBucket", {
autoDeleteObjects: true,
publicReadAccess: true,
removalPolicy: RemovalPolicy.DESTROY,
websiteIndexDocument: "index.html",
});

new s3deploy.BucketDeployment(this, "DeployWebsite", {
new s3deploy.BucketDeployment(stack, "DeployWebsite", {
destinationBucket: websiteBucket,
sources: [websiteBundle],
});
```

### Amazon CloudWatch Synthetics

> ⚠️ **Status: Experimental** \
> Expect the interface to change. Please report any issues!
Synthetics runs a canary to produce traffic to an application for monitoring purposes. Use `TypeScriptCode` as the `code` of a Canary test:

```ts
import * as synthetics from "@aws-cdk/aws-synthetics";
import { TypeScriptCode } from "@mrgrain/cdk-esbuild";

const bundledCode = new TypeScriptCode("src/index.ts");

const canary = new synthetics.Canary(stack, "MyCanary", {
runtime: synthetics.SyntheticsRuntime.SYNTHETICS_NODEJS_PUPPETEER_3_2,
test: synthetics.Test.custom({
code: bundledCode,
handler: "index.handler",
});
});
```

# Documentation

The package exports various different constructs for use with existing CDK features. A major guiding design principal for this package is to _extend, don't replace_. Expect constructs that you can provide as props, not complete replacements.

For use in **lambda functions**, the following classes implement `lambda.Code` ([reference](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Code.html)):
For use in **Lambda Functions** and **Synthetic Canaries**, the following classes implement `lambda.Code` ([reference](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Code.html)) and `synthetics.Code` ([reference](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-synthetics.Code.html)):

- `TypeScriptCode` & `JavaScriptCode`

Inline code is only supported by **Lambda**:

- `InlineTypeScriptCode` & `InlineJavaScriptCode`
- `InlineTsxCode` & `InlineJsxCode`

Expand Down
28 changes: 16 additions & 12 deletions lib/code.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {
Code as LambdaCode,
CodeConfig,
CodeConfig as LambdaCodeConfig,
ResourceBindOptions,
} from "@aws-cdk/aws-lambda";
import {
Code as SyntheticsCode,
CodeConfig as SyntheticsCodeConfig,
} from "@aws-cdk/aws-synthetics";
import { CfnResource, Construct, Stack } from "@aws-cdk/core";
import {
EsbuildAssetProps,
Expand All @@ -22,12 +26,12 @@ type TypeScriptCodeProps = CodeProps;

abstract class Code<
Props extends CodeProps,
Asset extends JSAsset | TSAsset
> extends LambdaCode {
Asset extends JSAsset | TSAsset,
> implements LambdaCode, SyntheticsCode {
protected abstract AssetClass: new (
scope: Construct,
id: string,
props: EsbuildAssetProps
props: EsbuildAssetProps,
) => Asset;

protected props: EsbuildAssetProps;
Expand All @@ -42,11 +46,9 @@ abstract class Code<
* @param props - Asset properties.
*/
constructor(entryPoints: EsbuildAssetProps["entryPoints"], props: Props) {
super();

const defaultOptions: Partial<BuildOptions> = {
...(!props.buildOptions?.platform ||
props.buildOptions?.platform === "node"
props.buildOptions?.platform === "node"
? { platform: "node", target: "node" + nodeMajorVersion() }
: {}),
};
Expand All @@ -61,19 +63,21 @@ abstract class Code<
};
}

bind(scope: Construct): CodeConfig {
bind(
scope: Construct,
): LambdaCodeConfig & SyntheticsCodeConfig {
// If the same AssetCode is used multiple times, retain only the first instantiation.
if (!this.asset) {
this.asset = new this.AssetClass(
scope,
this.constructor.name,
this.props
this.props,
);
} else if (Stack.of(this.asset) !== Stack.of(scope)) {
throw new Error(
`Asset is already associated with another stack '${
Stack.of(this.asset).stackName
}'. ` + "Create a new Asset instance for every stack."
}'. ` + "Create a new Asset instance for every stack.",
);
}

Expand All @@ -100,7 +104,7 @@ export class JavaScriptCode extends Code<JavaScriptCodeProps, JSAsset> {

constructor(
entryPoints: EsbuildAssetProps["entryPoints"],
props: JavaScriptCodeProps = {}
props: JavaScriptCodeProps = {},
) {
super(entryPoints, props);
}
Expand All @@ -110,7 +114,7 @@ export class TypeScriptCode extends Code<TypeScriptCodeProps, TSAsset> {

constructor(
entryPoints: EsbuildAssetProps["entryPoints"],
props: TypeScriptCodeProps = {}
props: TypeScriptCodeProps = {},
) {
super(entryPoints, props);
}
Expand Down
Loading

0 comments on commit f840300

Please sign in to comment.