Skip to content

Commit

Permalink
Merge branch 'master' into sfn-input
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Sep 15, 2021
2 parents c081318 + 2737119 commit a8cacb3
Show file tree
Hide file tree
Showing 17 changed files with 1,463 additions and 89 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-config/lib/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ export class ResourceType {
/** Amazon EC2 customer gateway */
public static readonly EC2_CUSTOMER_GATEWAY = new ResourceType('AWS::EC2::CustomerGateway');
/** Amazon EC2 internet gateway */
public static readonly EC2_INTERNET_GATEWAY = new ResourceType('AWS::EC2::CustomerGateway');
public static readonly EC2_INTERNET_GATEWAY = new ResourceType('AWS::EC2::InternetGateway');
/** Amazon EC2 network ACL */
public static readonly EC2_NETWORK_ACL = new ResourceType('AWS::EC2::NetworkAcl');
/** Amazon EC2 route table */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@
"Properties": {
"Code": {
"S3Bucket": {
"Ref": "AssetParameters1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24eaS3BucketD8D20B9A"
"Ref": "AssetParametersa3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8S3BucketD1AD544E"
},
"S3Key": {
"Fn::Join": [
Expand All @@ -1232,7 +1232,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24eaS3VersionKeyCD2774D3"
"Ref": "AssetParametersa3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8S3VersionKey93A19D70"
}
]
}
Expand All @@ -1245,7 +1245,7 @@
"Fn::Split": [
"||",
{
"Ref": "AssetParameters1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24eaS3VersionKeyCD2774D3"
"Ref": "AssetParametersa3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8S3VersionKey93A19D70"
}
]
}
Expand Down Expand Up @@ -1348,17 +1348,17 @@
"Type": "String",
"Description": "Artifact hash for asset \"e9882ab123687399f934da0d45effe675ecc8ce13b40cb946f3e1d6141fe8d68\""
},
"AssetParameters1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24eaS3BucketD8D20B9A": {
"AssetParametersa3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8S3BucketD1AD544E": {
"Type": "String",
"Description": "S3 bucket for asset \"1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24ea\""
"Description": "S3 bucket for asset \"a3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8\""
},
"AssetParameters1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24eaS3VersionKeyCD2774D3": {
"AssetParametersa3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8S3VersionKey93A19D70": {
"Type": "String",
"Description": "S3 key for asset version \"1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24ea\""
"Description": "S3 key for asset version \"a3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8\""
},
"AssetParameters1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24eaArtifactHash3943F7F3": {
"AssetParametersa3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8ArtifactHash238275D6": {
"Type": "String",
"Description": "Artifact hash for asset \"1f6de40da10b415b255c07df709f791e772ffb9f7bdd14ad81fb75643aad24ea\""
"Description": "Artifact hash for asset \"a3058ccb468d757ebb89df5363a1c20f5307c6911136f29d00e1a68c9b2aa7e8\""
},
"AssetParameters972240f9dd6e036a93d5f081af9a24315b2053828ac049b3b19b2fa12d7ae64aS3Bucket1F1A8472": {
"Type": "String",
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ new DockerImageFunction(this, 'ECRFunction', {
});
```

The props for these docker image resources allow overriding the image's `CMD`, `ENTRYPOINT`, and `WORKDIR`
configurations. See their docs for more information.

## Execution Role

Lambda functions assume an IAM role during execution. In CDK by default, Lambda
Expand Down
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ export interface CodeImageConfig {
* @default - use the ENTRYPOINT in the docker image or Dockerfile.
*/
readonly entrypoint?: string[];

/**
* Specify or override the WORKDIR on the specified Docker image or Dockerfile.
* A WORKDIR allows you to configure the working directory the container will use.
* @see https://docs.docker.com/engine/reference/builder/#workdir
* @default - use the WORKDIR in the docker image or Dockerfile.
*/
readonly workingDirectory?: string;
}

/**
Expand Down Expand Up @@ -435,6 +443,14 @@ export interface EcrImageCodeProps {
*/
readonly entrypoint?: string[];

/**
* Specify or override the WORKDIR on the specified Docker image or Dockerfile.
* A WORKDIR allows you to configure the working directory the container will use.
* @see https://docs.docker.com/engine/reference/builder/#workdir
* @default - use the WORKDIR in the docker image or Dockerfile.
*/
readonly workingDirectory?: string;

/**
* The image tag to use when pulling the image from ECR.
* @default 'latest'
Expand All @@ -460,6 +476,7 @@ export class EcrImageCode extends Code {
imageUri: this.repository.repositoryUriForTag(this.props?.tag ?? 'latest'),
cmd: this.props.cmd,
entrypoint: this.props.entrypoint,
workingDirectory: this.props.workingDirectory,
},
};
}
Expand All @@ -485,6 +502,14 @@ export interface AssetImageCodeProps extends ecr_assets.DockerImageAssetOptions
* @default - use the ENTRYPOINT in the docker image or Dockerfile.
*/
readonly entrypoint?: string[];

/**
* Specify or override the WORKDIR on the specified Docker image or Dockerfile.
* A WORKDIR allows you to configure the working directory the container will use.
* @see https://docs.docker.com/engine/reference/builder/#workdir
* @default - use the WORKDIR in the docker image or Dockerfile.
*/
readonly workingDirectory?: string;
}

/**
Expand All @@ -510,6 +535,7 @@ export class AssetImageCode extends Code {
imageUri: asset.imageUri,
entrypoint: this.props.entrypoint,
cmd: this.props.cmd,
workingDirectory: this.props.workingDirectory,
},
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ export class Function extends FunctionBase {
imageConfig: undefinedIfNoKeys({
command: code.image?.cmd,
entryPoint: code.image?.entrypoint,
workingDirectory: code.image?.workingDirectory,
}),
kmsKeyArn: props.environmentEncryption?.keyArn,
fileSystemConfigs,
Expand Down
12 changes: 3 additions & 9 deletions packages/@aws-cdk/aws-lambda/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,17 @@ export class Runtime {
/**
* The .NET Core 2.1 runtime (dotnetcore2.1)
*/
public static readonly DOTNET_CORE_2_1 = new Runtime('dotnetcore2.1', RuntimeFamily.DOTNET_CORE, {
bundlingDockerImage: 'lambci/lambda:build-dotnetcore2.1',
});
public static readonly DOTNET_CORE_2_1 = new Runtime('dotnetcore2.1', RuntimeFamily.DOTNET_CORE);

/**
* The .NET Core 3.1 runtime (dotnetcore3.1)
*/
public static readonly DOTNET_CORE_3_1 = new Runtime('dotnetcore3.1', RuntimeFamily.DOTNET_CORE, {
bundlingDockerImage: 'lambci/lambda:build-dotnetcore3.1',
});
public static readonly DOTNET_CORE_3_1 = new Runtime('dotnetcore3.1', RuntimeFamily.DOTNET_CORE);

/**
* The Go 1.x runtime (go1.x)
*/
public static readonly GO_1_X = new Runtime('go1.x', RuntimeFamily.GO, {
bundlingDockerImage: 'lambci/lambda:build-go1.x',
});
public static readonly GO_1_X = new Runtime('go1.x', RuntimeFamily.GO);

/**
* The Ruby 2.5 runtime (ruby2.5)
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('code', () => {
cmd: ['cmd', 'param1'],
entrypoint: ['entrypoint', 'param2'],
tag: 'mytag',
workingDirectory: '/some/path',
}),
handler: lambda.Handler.FROM_IMAGE,
runtime: lambda.Runtime.FROM_IMAGE,
Expand All @@ -238,6 +239,7 @@ describe('code', () => {
ImageConfig: {
Command: ['cmd', 'param1'],
EntryPoint: ['entrypoint', 'param2'],
WorkingDirectory: '/some/path',
},
});
});
Expand Down Expand Up @@ -315,6 +317,7 @@ describe('code', () => {
code: lambda.Code.fromAssetImage(path.join(__dirname, 'docker-lambda-handler'), {
cmd: ['cmd', 'param1'],
entrypoint: ['entrypoint', 'param2'],
workingDirectory: '/some/path',
}),
handler: lambda.Handler.FROM_IMAGE,
runtime: lambda.Runtime.FROM_IMAGE,
Expand All @@ -325,6 +328,7 @@ describe('code', () => {
ImageConfig: {
Command: ['cmd', 'param1'],
EntryPoint: ['entrypoint', 'param2'],
WorkingDirectory: '/some/path',
},
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,7 @@ describe('function', () => {
imageUri: 'ecr image uri',
cmd: ['cmd', 'param1'],
entrypoint: ['entrypoint', 'param2'],
workingDirectory: '/some/path',
},
}),
handler: lambda.Handler.FROM_IMAGE,
Expand All @@ -2122,6 +2123,7 @@ describe('function', () => {
ImageConfig: {
Command: ['cmd', 'param1'],
EntryPoint: ['entrypoint', 'param2'],
WorkingDirectory: '/some/path',
},
});
});
Expand Down
6 changes: 0 additions & 6 deletions packages/@aws-cdk/aws-lambda/test/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,4 @@ describe('runtime', () => {
// THEN
expect(runtime.bundlingDockerImage.image).toEqual('my-docker-image');
});

test('dotnetcore and go have overridden images', () => {
expect(lambda.Runtime.DOTNET_CORE_3_1.bundlingDockerImage.image).toEqual('lambci/lambda:build-dotnetcore3.1');
expect(lambda.Runtime.DOTNET_CORE_2_1.bundlingDockerImage.image).toEqual('lambci/lambda:build-dotnetcore2.1');
expect(lambda.Runtime.GO_1_X.bundlingDockerImage.image).toEqual('lambci/lambda:build-go1.x');
});
});
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-s3-deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,26 @@ size of the AWS Lambda resource handler.
> NOTE: a new AWS Lambda handler will be created in your stack for each memory
> limit configuration.
## EFS Support

If your workflow needs more disk space than default (512 MB) disk space, you may attach an EFS storage to underlying
lambda function. To Enable EFS support set `efs` and `vpc` props for BucketDeployment.

Check sample usage below.
Please note that creating VPC inline may cause stack deletion failures. It is shown as below for simplicity.
To avoid such condition, keep your network infra (VPC) in a separate stack and pass as props.

```ts
new s3deploy.BucketDeployment(this, 'DeployMeWithEfsStorage', {
sources: [s3deploy.Source.asset(path.join(__dirname, 'my-website'))],
destinationBucket,
destinationKeyPrefix: 'efs/',
useEfs: true,
vpc: new ec2.Vpc(this, 'Vpc'),
retainOnDelete: false,
});
```

## Notes

- This library uses an AWS CloudFormation custom resource which about 10MiB in
Expand Down
75 changes: 73 additions & 2 deletions packages/@aws-cdk/aws-s3-deployment/lib/bucket-deployment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import * as cloudfront from '@aws-cdk/aws-cloudfront';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as efs from '@aws-cdk/aws-efs';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from '@aws-cdk/aws-s3';
Expand Down Expand Up @@ -107,6 +108,14 @@ export interface BucketDeploymentProps {
*/
readonly memoryLimit?: number;

/**
* Mount an EFS file system. Enable this if your assets are large and you encounter disk space errors.
* Enabling this option will require a VPC to be specified.
*
* @default - No EFS. Lambda has access only to 512MB of disk space.
*/
readonly useEfs?: boolean

/**
* Execution role associated with this function
*
Expand Down Expand Up @@ -197,6 +206,7 @@ export interface BucketDeploymentProps {

/**
* The VPC network to place the deployment lambda handler in.
* This is required if `useEfs` is set.
*
* @default None
*/
Expand Down Expand Up @@ -230,18 +240,59 @@ export class BucketDeployment extends CoreConstruct {
}
}

if (props.useEfs && !props.vpc) {
throw new Error('Vpc must be specified if useEfs is set');
}

const accessPointPath = '/lambda';
let accessPoint;
if (props.useEfs && props.vpc) {
const accessMode = '0777';
const fileSystem = this.getOrCreateEfsFileSystem(scope, {
vpc: props.vpc,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
accessPoint = fileSystem.addAccessPoint('AccessPoint', {
path: accessPointPath,
createAcl: {
ownerUid: '1001',
ownerGid: '1001',
permissions: accessMode,
},
posixUser: {
uid: '1001',
gid: '1001',
},
});
accessPoint.node.addDependency(fileSystem.mountTargetsAvailable);
}

// Making VPC dependent on BucketDeployment so that CFN stack deletion is smooth.
// Refer comments on https://github.com/aws/aws-cdk/pull/15220 for more details.
if (props.vpc) {
this.node.addDependency(props.vpc);
}

const mountPath = `/mnt${accessPointPath}`;
const handler = new lambda.SingletonFunction(this, 'CustomResourceHandler', {
uuid: this.renderSingletonUuid(props.memoryLimit),
uuid: this.renderSingletonUuid(props.memoryLimit, props.vpc),
code: lambda.Code.fromAsset(path.join(__dirname, 'lambda')),
layers: [new AwsCliLayer(this, 'AwsCliLayer')],
runtime: lambda.Runtime.PYTHON_3_6,
environment: props.useEfs ? {
MOUNT_PATH: mountPath,
} : undefined,
handler: 'index.handler',
lambdaPurpose: 'Custom::CDKBucketDeployment',
timeout: cdk.Duration.minutes(15),
role: props.role,
memorySize: props.memoryLimit,
vpc: props.vpc,
vpcSubnets: props.vpcSubnets,
filesystem: accessPoint ? lambda.FileSystem.fromEfsAccessPoint(
accessPoint,
mountPath,
): undefined,
});

const handlerRole = handler.role;
Expand Down Expand Up @@ -279,7 +330,7 @@ export class BucketDeployment extends CoreConstruct {

}

private renderSingletonUuid(memoryLimit?: number) {
private renderSingletonUuid(memoryLimit?: number, vpc?: ec2.IVpc) {
let uuid = '8693BB64-9689-44B6-9AAF-B0CC9EB8756C';

// if user specify a custom memory limit, define another singleton handler
Expand All @@ -293,8 +344,28 @@ export class BucketDeployment extends CoreConstruct {
uuid += `-${memoryLimit.toString()}MiB`;
}

// if user specify to use VPC, define another singleton handler
// with this configuration. otherwise, it won't be possible to use multiple
// configurations since we have a singleton.
// A VPC is a must if EFS storage is used and that's why we are only using VPC in uuid.
if (vpc) {
uuid += `-${vpc.node.addr}`;
}

return uuid;
}

/**
* Function to get/create a stack singleton instance of EFS FileSystem per vpc.
*
* @param scope Construct
* @param fileSystemProps EFS FileSystemProps
*/
private getOrCreateEfsFileSystem(scope: Construct, fileSystemProps: efs.FileSystemProps): efs.FileSystem {
const stack = cdk.Stack.of(scope);
const uuid = `BucketDeploymentEFS-VPC-${fileSystemProps.vpc.node.addr}`;
return stack.node.tryFindChild(uuid) as efs.FileSystem ?? new efs.FileSystem(scope, uuid, fileSystemProps);
}
}

/**
Expand Down
Loading

0 comments on commit a8cacb3

Please sign in to comment.