Skip to content

Commit

Permalink
docs: update to explain usage of Code Artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
setu4993 committed Jan 26, 2022
1 parent f1f1a5c commit 220d089
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion packages/@aws-cdk/aws-lambda-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ const codeArtifactAuthToken = execSync(`aws codeartifact get-authorization-token

const indexUrl = `https://aws:${codeArtifactAuthToken}@${domain}-${domainOwner}.d.codeartifact.${region}.amazonaws.com/pypi/${repoName}/simple/`;

new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: {
environment: { PIP_INDEX_URL: indexUrl },
},
});
```

If you also want to use the Code Artifact repo for initializing the Docker image for bundling, use `buildArgs`:

```ts
import { execSync } from 'child_process';

const entry = '/path/to/function';
const image = DockerImage.fromBuild(entry);

const domain = 'my-domain';
const domainOwner = '111122223333';
const repoName = 'my_repo';
const region = 'us-east-1';
const codeArtifactAuthToken = execSync(`aws codeartifact get-authorization-token --domain ${domain} --domain-owner ${domainOwner} --query authorizationToken --output text`).toString().trim();

const indexUrl = `https://aws:${codeArtifactAuthToken}@${domain}-${domainOwner}.d.codeartifact.${region}.amazonaws.com/pypi/${repoName}/simple/`;

new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
Expand All @@ -172,4 +197,33 @@ new lambda.PythonFunction(this, 'function', {
});
```

This type of an example should work for `pip` and `poetry` based dependencies, but will not work for `pipenv`.
**Note:** Setting custom build args for bundling will force the base bundling image to be rebuilt every time (i.e. skip the Docker cache).

Setting only environment variable should work for `pip` and `poetry` based dependencies, whereas `pipenv` based dependencies will require **both** build args and environment variables to be set.


Example for using Code Artifact with `pipenv`-based dependencies:

```ts
import { execSync } from 'child_process';

const entry = '/path/to/function';
const image = DockerImage.fromBuild(entry);

const domain = 'my-domain';
const domainOwner = '111122223333';
const repoName = 'my_repo';
const region = 'us-east-1';
const codeArtifactAuthToken = execSync(`aws codeartifact get-authorization-token --domain ${domain} --domain-owner ${domainOwner} --query authorizationToken --output text`).toString().trim();

const indexUrl = `https://aws:${codeArtifactAuthToken}@${domain}-${domainOwner}.d.codeartifact.${region}.amazonaws.com/pypi/${repoName}/simple/`;

new lambda.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: {
buildArgs: { PIP_INDEX_URL: indexUrl },
environment: { PIP_INDEX_URL: indexUrl },
},
});
```

0 comments on commit 220d089

Please sign in to comment.