Skip to content

Commit

Permalink
feat(lambda): configure workdir for docker image based functions (#16111
Browse files Browse the repository at this point in the history
)

CloudFormation allows setting the [`WorkingDirectory`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-imageconfig.html#cfn-lambda-function-imageconfig-workingdirectory) property in `AWS::Lambda::Function.ImageConfig` to override the docker container's working directory, but this isn't currently exposed through CDK. Not sure if that was intentional.

This PR wires that up.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
blaenk authored Sep 14, 2021
1 parent a9d5118 commit b3eafc2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
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
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

0 comments on commit b3eafc2

Please sign in to comment.