Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 12ab531

Browse files
flochazChazalAutomation
authored
feat: flyway runner construct (#157)
* add flyway construct * fix file name * add unit test for log retention option * put back integ test file * fix modified projen * force node12 * chore: self mutation * remove classes * chore: self mutation * remove classes * chore: self mutation * Move to pre bundle lambda and custom resource provider * remove tests that don't work with pre bundle * move to projen 0.33.1 * revert projen version related changes * Add Java 11 to github build action * fix jar copy * chore: self mutation * Add special LICENSE entry for FlywayLambda code Respect MIT license requirement. * chore: self mutation Co-authored-by: Chazal <[email protected]> Co-authored-by: Automation <[email protected]>
1 parent c202c52 commit 12ab531

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4056
-344
lines changed

.github/workflows/build.yml

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ jobs:
1717
uses: actions/setup-node@v2
1818
with:
1919
node-version: '12'
20+
- name: Setup Java 11
21+
uses: actions/setup-java@v2
22+
with:
23+
distribution: 'zulu' # OpenJDK
24+
java-version: '11'
2025
- name: Install dependencies
2126
run: cd core && yarn install --check-files --frozen-lockfile
2227
- name: Set git identity

CONTRIB_FAQ.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,15 @@ To prevent this issue, we need to use `path.join(__dirname, 'resources/first.sql
204204

205205
Place your Lambda function code under `<construct-folder>/resources/lambdas/<lambdan-function-name>` folder. Projen will detect the new folder and bundle that during `npx projen build`.
206206

207+
#### For python
208+
207209
It will copy all files to the same path in `lib` folder and install Python dependencies on any folder with `requirements.txt`. Our package will have all dependencies bundle and consumers do not have to install dependencies by themselves.
208210

209211
In the construct, create a Lambda function with `PreBundledFunction` construct to use the prebundled version. All of the parameter is the same as `lambda.Function`. The only difference is passing `codePath` prop instead of `code` with a relative path from `core/src`. The construct will use the right path with all dependencies when consumer is building.
210212

211213
Here's an example:
212214

213-
```
215+
```typescript
214216
new PreBundledFunction(this, 'helloWordNumpy', {
215217
runtime: Runtime.PYTHON_3_8,
216218
codePath: '<construct-folder>/resources/lambdas/<lambdan-function-name>',
@@ -219,13 +221,28 @@ new PreBundledFunction(this, 'helloWordNumpy', {
219221
timeout: Duration.seconds(30),
220222
});
221223
```
224+
225+
### For Java (Gradle)
226+
227+
It will copy all files to the same path in `lib` folder and call `./gradlew build shadowJar` on any folder with `build.gradle`. Our package will have all dependencies bundle and consumers do not have to install java or dependencies by themselves.
228+
229+
Here's an example:
230+
231+
```typescript
232+
new PreBundledFunction(this, 'runner', {
233+
codePath: path.join(__dirname.split('/').slice(-1)[0], './resources/flyway-lambda/build/libs/flyway-all.jar'),
234+
handler: 'com.geekoosh.flyway.FlywayCustomResourceHandler::handleRequest',
235+
runtime: lambda.Runtime.JAVA_11,
236+
});
237+
```
238+
222239
### Why do we prebundle Python code for Lambda function?
223240

224241
Most of the data engineering code are written in Python. Even our CDK constructs are in TypeScript, we still use Python a lot in our Lambda functions.
225242

226243
Normally, construct provider will **defer bundling step to construct consumers**. The provider only packages Python files and `requirements.txt` and distribute them. Consumers have to install and compile dependencies by themselves. The code looks like this:
227244

228-
```
245+
```typescript
229246
new PythonFunction(this, 'functionName', {
230247
runtime: Runtime.PYTHON_3_8,
231248
entry: '<construct-folder>/resources/lambdas/<function-name>',

LICENSE

+5
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,8 @@ Section 8 – Interpretation.
150150
c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor.
151151

152152
d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority.
153+
154+
155+
** FlywayLambdaService (core/src/db-schema-manager/resources/flyway-lambda) - https://github.com/Geekoosh/flyway-lambda
156+
157+
Copyright (c) 2021 Assaf Kamil

core/.gitattributes

+15-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/.gitignore

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/.projen/deps.json

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/.projen/tasks.json

+44-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/.projenrc.js

+39
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ const project = new AwsCdkConstructLibrary({
5555
'@aws-cdk/aws-stepfunctions-tasks',
5656
'@aws-cdk/aws-events',
5757
'@aws-cdk/aws-events-targets',
58+
'@aws-cdk/aws-s3-deployment',
59+
'@aws-cdk/aws-ec2',
60+
'@aws-cdk/aws-redshift',
61+
'@aws-cdk/aws-secretsmanager',
62+
'@aws-cdk/aws-s3-assets',
63+
'@aws-cdk/assertions',
5864
],
5965
bundledDeps: [
6066
'xmldom@github:xmldom/xmldom#0.7.0',
@@ -119,11 +125,29 @@ for (const dirPath of findAllPythonLambdaDir('src')) {
119125
pipInstallTask.exec(pipCmd);
120126
}
121127

128+
/**
129+
* Task to build java lambda jar with gradle
130+
*/
131+
const gradleBuildTask = project.addTask('gradle-build', {
132+
description: './gradlew shadowJar all folders in lib that has requirements.txt',
133+
});
134+
135+
for (const dirPath of findAllGradleLambdaDir('src')) {
136+
console.log('loop over gradle dir');
137+
// Assume that all folders with 'requirements.txt' have been copied to lib
138+
// by the task 'copy-resources'
139+
const dirPathInLib = dirname(dirPath.replace('src', 'lib'));
140+
const gradleCmd = `cd ${dirPathInLib} && ./gradlew shadowJar && cp build/libs/*.jar ./ 2> /dev/null`;
141+
142+
gradleBuildTask.exec(gradleCmd);
143+
}
144+
122145
/**
123146
* Run `copy-resources` and `pip-install` as part of compile
124147
*/
125148
project.compileTask.exec('npx projen copy-resources');
126149
project.compileTask.exec('npx projen pip-install');
150+
project.compileTask.exec('npx projen gradle-build');
127151

128152
/**
129153
* Find all directory that has a Python package.
@@ -139,4 +163,19 @@ function findAllPythonLambdaDir(rootDir) {
139163
});
140164
}
141165

166+
/**
167+
* Find all directory that has a gradle package.
168+
* Assume that they have build.gradle
169+
*
170+
* @param rootDir Root directory to begin finding
171+
* @returns Array of directory paths
172+
*/
173+
function findAllGradleLambdaDir(rootDir) {
174+
console.log('findAllGradleLambdaDir');
175+
176+
return glob.sync(`${rootDir}/**/build.gradle`).map((pathWithGradle) => {
177+
return pathWithGradle;
178+
});
179+
}
180+
142181
project.synth();

0 commit comments

Comments
 (0)