Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): higher level construct for Node.js #5532

Merged
merged 22 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"@aws-cdk/assets/minimatch",
"@aws-cdk/assets/minimatch/**",
"@aws-cdk/cx-api/semver",
"@aws-cdk/cx-api/semver/**"
"@aws-cdk/cx-api/semver/**",
"@aws-cdk/cx-api/parcel-bundler",
"@aws-cdk/cx-api/parcel-bundler/**"
]
}
}
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as s3 from '@aws-cdk/aws-s3';
import * as s3_assets from '@aws-cdk/aws-s3-assets';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';

export abstract class Code {
/**
Expand Down Expand Up @@ -43,6 +44,16 @@ export abstract class Code {
return new AssetCode(path, options);
}

/**
* Loads the function code from an entry file that will be bundled with parcel
* @param path Path to an entry file
*/
public static fromParcel(path: string, options?: cxapi.ParcelBundlerOptions): AssetCode {
return new AssetCode(path, {
bundler: new cxapi.ParcelBundler(options)
});
}

/**
* @deprecated use `fromAsset`
*/
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"cdk-build-tools": "1.19.0",
"cdk-integ-tools": "1.19.0",
"cfn2ts": "1.19.0",
"got": "^10.1.0",
"lodash": "^4.17.15",
"nock": "^11.7.0",
"nodeunit": "^0.11.3",
Expand Down
103 changes: 103 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/integ.parcel.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"Resources": {
"FnServiceRoleB9001A96": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"Fn9270CBC0": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParametersa65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8S3Bucket5066F17E"
},
"S3Key": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParametersa65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8S3VersionKeyA17B1FBA"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParametersa65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8S3VersionKeyA17B1FBA"
}
]
}
]
}
]
]
}
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"FnServiceRoleB9001A96",
"Arn"
]
},
"Runtime": "nodejs12.x"
},
"DependsOn": [
"FnServiceRoleB9001A96"
]
}
},
"Parameters": {
"AssetParametersa65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8S3Bucket5066F17E": {
"Type": "String",
"Description": "S3 bucket for asset \"a65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8\""
},
"AssetParametersa65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8S3VersionKeyA17B1FBA": {
"Type": "String",
"Description": "S3 key for asset version \"a65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8\""
},
"AssetParametersa65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8ArtifactHash33C59817": {
"Type": "String",
"Description": "Artifact hash for asset \"a65237a89419688b887741b81425350f4378738096922b393b8e7becb0b61db8\""
}
}
}
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/integ.parcel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { App, Construct, Stack, StackProps } from '@aws-cdk/core';
import * as path from 'path';
import * as lambda from '../lib';

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

new lambda.Function(this, 'Fn', {
code: lambda.Code.fromParcel(path.join(__dirname, 'parcel-handler/index.ts'), {
minify: true,
}),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_12_X,
});
}
}

const app = new App();
new TestStack(app, 'cdk-integ-lambda-parcel');
app.synth();
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/parcel-handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import got from 'got';

export async function handler(): Promise<void> {
const response = await got('https://aws.amazon.com');
console.log(response.body); // tslint:disable-line no-console
}
14 changes: 11 additions & 3 deletions packages/@aws-cdk/aws-s3-assets/lib/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export interface AssetOptions extends assets.CopyOptions {
* @experimental
*/
readonly sourceHash?: string;

/**
* The bundler to use for the asset
*
* @default - do not use a bundler
*/
readonly bundler?: cxapi.IBundler;
}

export interface AssetProps extends AssetOptions {
Expand Down Expand Up @@ -101,10 +108,10 @@ export class Asset extends cdk.Construct implements assets.IAsset {

this.assetPath = staging.stagedPath;

const packaging = determinePackaging(staging.sourcePath);
const packaging = props.bundler ? cdk.FileAssetPackaging.BUNDLE : determinePackaging(staging.sourcePath);

// sets isZipArchive based on the type of packaging and file extension
this.isZipArchive = packaging === cdk.FileAssetPackaging.ZIP_DIRECTORY
this.isZipArchive = packaging === cdk.FileAssetPackaging.ZIP_DIRECTORY || packaging === cdk.FileAssetPackaging.BUNDLE
? true
: ARCHIVE_EXTENSIONS.some(ext => staging.sourcePath.toLowerCase().endsWith(ext));

Expand All @@ -113,7 +120,8 @@ export class Asset extends cdk.Construct implements assets.IAsset {
const location = stack.addFileAsset({
packaging,
sourceHash: this.sourceHash,
fileName: staging.stagedPath
fileName: staging.stagedPath,
bundler: props.bundler,
});

this.s3BucketName = location.bucketName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"Parameters": {
"AssetParameterse87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78S3BucketAED3F5B0": {
"Type": "String",
"Description": "S3 bucket for asset \"e87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78\""
},
"AssetParameterse87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78S3VersionKey872CDFBD": {
"Type": "String",
"Description": "S3 key for asset version \"e87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78\""
},
"AssetParameterse87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78ArtifactHash3A5762C7": {
"Type": "String",
"Description": "Artifact hash for asset \"e87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78\""
}
},
"Resources": {
"MyUserDC45028B": {
"Type": "AWS::IAM::User"
},
"MyUserDefaultPolicy7B897426": {
"Type": "AWS::IAM::Policy",
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:GetObject*",
"s3:GetBucket*",
"s3:List*"
],
"Effect": "Allow",
"Resource": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":s3:::",
{
"Ref": "AssetParameterse87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78S3BucketAED3F5B0"
}
]
]
},
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":s3:::",
{
"Ref": "AssetParameterse87657c067ca1dbeee2ad956d58e67ab7433f66638f4448f92293b0ec3bc2c78S3BucketAED3F5B0"
},
"/*"
]
]
}
]
}
],
"Version": "2012-10-17"
},
"PolicyName": "MyUserDefaultPolicy7B897426",
"Users": [
{
"Ref": "MyUserDC45028B"
}
]
}
}
}
}
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-s3-assets/test/integ.assets.parcel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import * as path from 'path';
import * as assets from '../lib';

class TestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const asset = new assets.Asset(this, 'SampleAsset', {
path: path.join(__dirname, 'parcel-main/index.ts'),
bundler: new cxapi.ParcelBundler({
global: 'addMult',
})
});

const user = new iam.User(this, 'MyUser');
asset.grantRead(user);
}
}

const app = new cdk.App();
new TestStack(app, 'aws-cdk-asset-parcel-test');
app.synth();
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-s3-assets/test/parcel-main/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { add, mult } from '../parcel-utils';

export function addMult(a: number, b: number, c: number): number {
return mult(add(a, b), c);
}
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-s3-assets/test/parcel-utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function add(a: number, b: number): number {
return a + b;
}

export function mult(a: number, b: number): number {
return a * b;
}
16 changes: 15 additions & 1 deletion packages/@aws-cdk/core/lib/assets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IBundler } from '@aws-cdk/cx-api';

/**
* Represents the source for a file asset.
*/
Expand All @@ -20,6 +22,13 @@ export interface FileAssetSource {
* Which type of packaging to perform.
*/
readonly packaging: FileAssetPackaging;

/**
* The bundler to use for the asset.
*
* @default - do not use a bundler
*/
readonly bundler?: IBundler;
}

export interface DockerImageAssetSource {
Expand Down Expand Up @@ -83,7 +92,12 @@ export enum FileAssetPackaging {
* The asset source path points to a single file, which should be uploaded
* to Amazon S3.
*/
FILE = 'file'
FILE = 'file',

/**
* The asset uses a bundler
*/
BUNDLE = 'bundle',
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ export class Stack extends Construct implements ITaggable {
id: asset.sourceHash,
packaging: asset.packaging,
sourceHash: asset.sourceHash,

bundlerClassName: asset.bundler && asset.bundler.constructor.name,
bundlerOptions: asset.bundler && asset.bundler.options,
s3BucketParameter: params.bucketNameParameter.logicalId,
s3KeyParameter: params.objectKeyParameter.logicalId,
artifactHashParameter: params.artifactHashParameter.logicalId,
Expand Down
Loading