Skip to content

Commit

Permalink
fix(shared_access): prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
massi-ang authored and bigadsoleiman committed Nov 14, 2023
1 parent d42a92a commit be01aff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 52 deletions.
8 changes: 3 additions & 5 deletions lib/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export class Shared extends Construct {
// Create a VPC endpoint for S3.
const s3GatewayEndpoint = vpc.addGatewayEndpoint("S3GatewayEndpoint", {
service: ec2.GatewayVpcEndpointAwsService.S3,

});

const s3vpcEndpoint = vpc.addInterfaceEndpoint("S3InterfaceEndpoint", {
Expand All @@ -101,7 +100,6 @@ export class Shared extends Construct {
service: ec2.InterfaceVpcEndpointAwsService.SAGEMAKER_RUNTIME,
open: true,
});

}

const configParameter = new ssm.StringParameter(this, "Config", {
Expand All @@ -125,9 +123,9 @@ export class Shared extends Construct {
path: path.join(__dirname, "./layers/common"),
});

this.sharedCode = new SharedAssetBundler(this, 'genai-core', [
path.join(__dirname, 'layers', 'python-sdk', 'python', 'genai_core')
])
this.sharedCode = new SharedAssetBundler(this, "genai-core", [
path.join(__dirname, "layers", "python-sdk", "python", "genai_core"),
]);

const xOriginVerifySecret = new secretsmanager.Secret(
this,
Expand Down
113 changes: 66 additions & 47 deletions lib/shared/shared-asset-bundler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AssetHashType, BundlingOutput, DockerImage, aws_s3_assets } from "aws-cdk-lib";
import {
AssetHashType,
BundlingOutput,
DockerImage,
aws_s3_assets,
} from "aws-cdk-lib";
import { Code, S3Code } from "aws-cdk-lib/aws-lambda";
import { Asset } from "aws-cdk-lib/aws-s3-assets";
import { md5hash } from "aws-cdk-lib/core/lib/helpers-internal";
Expand All @@ -7,55 +12,69 @@ import * as path from "path";
import * as fs from "fs";

function calculateHash(paths: string[]): string {
return paths.reduce((mh, p) => {
const dirs = fs.readdirSync(p);
let hash = calculateHash(dirs.filter(d => fs.statSync(path.join(p, d)).isDirectory()).map(v => path.join(p, v)))
return md5hash(mh + dirs.filter(d => fs.statSync(path.join(p,d)).isFile()).reduce((h, f) => { return md5hash(h + fs.readFileSync(path.join(p,f)))}, hash));
}, ""
)
return paths.reduce((mh, p) => {
const dirs = fs.readdirSync(p);
let hash = calculateHash(
dirs
.filter((d) => fs.statSync(path.join(p, d)).isDirectory())
.map((v) => path.join(p, v))
);
return md5hash(
mh +
dirs
.filter((d) => fs.statSync(path.join(p, d)).isFile())
.reduce((h, f) => {
return md5hash(h + fs.readFileSync(path.join(p, f)));
}, hash)
);
}, "");
}

export class SharedAssetBundler extends Construct {
private readonly sharedAssets: string[];
private readonly WORKING_PATH = '/asset-input/'
/**
* Instantiate a new SharedAssetBundler. You then invoke `bundleWithAsset(pathToAsset)` to
* bundle your asset code with the common code.
*
* For Lambda function handler assets, you can use `bundleWithLambdaAsset(pathToAsset)` as
* a drop-in replacement for `lambda.Code.fromAsset()`
*
* @param scope
* @param id
* @param commonFolders : array of common folders to bundle with your asset code
*/
constructor(scope: Construct, id: string, sharedAssets: string[]) {
super(scope, id);
this.sharedAssets = sharedAssets;
}
private readonly sharedAssets: string[];
private readonly WORKING_PATH = "/asset-input/";
/**
* Instantiate a new SharedAssetBundler. You then invoke `bundleWithAsset(pathToAsset)` to
* bundle your asset code with the common code.
*
* For Lambda function handler assets, you can use `bundleWithLambdaAsset(pathToAsset)` as
* a drop-in replacement for `lambda.Code.fromAsset()`
*
* @param scope
* @param id
* @param commonFolders : array of common folders to bundle with your asset code
*/
constructor(scope: Construct, id: string, sharedAssets: string[]) {
super(scope, id);
this.sharedAssets = sharedAssets;
}

bundleWithAsset(assetPath: string): Asset {
console.log(`Bundling asset ${assetPath}`)
const asset = new aws_s3_assets.Asset(this, md5hash(assetPath).slice(0, 6), {
path: assetPath,
bundling: {
image: DockerImage.fromBuild(path.join(__dirname, 'alpine-zip')),
command: ["zip", "-r", path.join("/asset-output", "asset.zip"), "."],
volumes: this.sharedAssets.map(f => ({
containerPath: path.join(this.WORKING_PATH, path.basename(f)),
hostPath: f
})),
workingDirectory: this.WORKING_PATH,
outputType: BundlingOutput.ARCHIVED,
},
assetHash: calculateHash([assetPath, ...this.sharedAssets]),
assetHashType: AssetHashType.CUSTOM,
})
return asset;
}
bundleWithAsset(assetPath: string): Asset {
console.log(`Bundling asset ${assetPath}`);
const asset = new aws_s3_assets.Asset(
this,
md5hash(assetPath).slice(0, 6),
{
path: assetPath,
bundling: {
image: DockerImage.fromBuild(path.join(__dirname, "alpine-zip")),
command: ["zip", "-r", path.join("/asset-output", "asset.zip"), "."],
volumes: this.sharedAssets.map((f) => ({
containerPath: path.join(this.WORKING_PATH, path.basename(f)),
hostPath: f,
})),
workingDirectory: this.WORKING_PATH,
outputType: BundlingOutput.ARCHIVED,
},
assetHash: calculateHash([assetPath, ...this.sharedAssets]),
assetHashType: AssetHashType.CUSTOM,
}
);
return asset;
}

bundleWithLambdaAsset(assetPath: string): S3Code {
const asset = this.bundleWithAsset(assetPath);
return Code.fromBucket(asset.bucket, asset.s3ObjectKey);
}
bundleWithLambdaAsset(assetPath: string): S3Code {
const asset = this.bundleWithAsset(assetPath);
return Code.fromBucket(asset.bucket, asset.s3ObjectKey);
}
}

0 comments on commit be01aff

Please sign in to comment.