Skip to content

Commit

Permalink
feat: deploy-time build for SOCI indices (#7)
Browse files Browse the repository at this point in the history
* feat: deploy-time build for SOCI indices

* Update README.md
  • Loading branch information
tmokmss authored Aug 4, 2023
1 parent 7589d62 commit 4261cba
Show file tree
Hide file tree
Showing 33 changed files with 4,725 additions and 7,450 deletions.
2 changes: 1 addition & 1 deletion .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
});
// Bundle custom resource handler Lambda code
project.projectBuild.compileTask.prependExec('npm ci && npm run build', {
cwd: 'lambda/nodejs-build',
cwd: 'lambda/trigger-codebuild',
});
// Run integ-test
project.projectBuild.testTask.exec('yarn tsc -p tsconfig.dev.json && yarn integ-runner');
Expand Down
174 changes: 174 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Install from npm:
```sh
npm i deploy-time-build
```

Then write CDK code as below:
### Build Node.js apps
Use the following code to build Node.js apps like React frontend:

```ts
import { NodejsBuild } from 'deploy-time-build';
Expand Down Expand Up @@ -70,6 +70,22 @@ You can also override the path where assets are extracted by `extractPath` prope

Please also check [the example directory](./example/) for a complete example.

### Build SOCI index for a container image
[Seekable OCI (SOCI)](https://docs.aws.amazon.com/AmazonECS/latest/userguide/container-considerations.html) is a way to help start tasks faster for Amazon ECS tasks on Fargate 1.4.0. You can build and push a SOCI index to use the feature by the following CDK code:

```ts
import { SociIndexBuild } from 'deploy-time-build;

const asset = new DockerImageAsset(this, 'Image', { directory: 'example-image' });
new SociIndexBuild(this, 'Index', { imageTag: asset.assetHash, repository: asset.repository });
// or using a utility method
SociIndexBuild.fromImageAsset(this, 'Index', asset);

// Use the asset for ECS Fargate tasks
import { AssetImage } from 'aws-cdk-lib/aws-ecs';
const assetImage = AssetImage.fromDockerImageAsset(asset);
```

## Motivation - why do we need this construct?
Previously, there are a few different ways to deploy frontend applications from CDK (1 and 2 below). But none is perfect with different pros and cons. This construct adds another option to deploy frontend apps.

Expand Down
5 changes: 5 additions & 0 deletions example/example-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM nginx:1.25.1
# create dummy file to change the size of a image
ARG DUMMY_FILE_SIZE_MB="10"
# RUN fallocate -l ${DUMMY_FILE_SIZE_MB} dummy.img
RUN dd if=/dev/zero of=dummy.img bs=1M count=${DUMMY_FILE_SIZE_MB}
18 changes: 15 additions & 3 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Stack, StackProps, App, RemovalPolicy, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { MockIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
import { NodejsBuild } from '../src/nodejs-build';
import { NodejsBuild, SociIndexBuild } from '../src/';
import { BlockPublicAccess, Bucket, BucketEncryption } from 'aws-cdk-lib/aws-s3';
import { OriginAccessIdentity, CloudFrontWebDistribution } from 'aws-cdk-lib/aws-cloudfront';
import { DockerImageAsset } from 'aws-cdk-lib/aws-ecr-assets';

class TestStack extends Stack {
class NodejsTestStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props);

Expand Down Expand Up @@ -74,11 +75,22 @@ class TestStack extends Stack {
}
}

class SociIndexTestStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props);

const asset = new DockerImageAsset(this, 'Image', { directory: 'example-image' });

new SociIndexBuild(this, 'Index', { imageTag: asset.assetHash, repository: asset.repository });
}
}

class TestApp extends App {
constructor() {
super();

new TestStack(this, 'TestStack');
new NodejsTestStack(this, 'NodejsTestStack');
new SociIndexTestStack(this, 'SociIndexTestStack');
}
}

Expand Down
Loading

0 comments on commit 4261cba

Please sign in to comment.