/
test-stack.js
134 lines (126 loc) · 5.05 KB
/
test-stack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var path = require('path');
var { NestedStack1 } = require('./nested-stack');
if (process.env.PACKAGE_LAYOUT_VERSION === '1') {
var { Stack } = require('@aws-cdk/core');
var { Runtime, LayerVersion, Code, Function, DockerImageFunction, DockerImageCode } = require('@aws-cdk/aws-lambda');
var { SpecRestApi, ApiDefinition } = require('@aws-cdk/aws-apigateway');
var { NodejsFunction } = require('@aws-cdk/aws-lambda-nodejs');
var { GoFunction } = require('@aws-cdk/aws-lambda-go');
var { PythonFunction, PythonLayerVersion } = require('@aws-cdk/aws-lambda-python');
var { Role, ServicePrincipal, PolicyStatement } = require('@aws-cdk/aws-iam');
var { RetentionDays } = require('@aws-cdk/aws-logs');
} else {
var { Stack } = require('aws-cdk-lib');
var { Runtime, LayerVersion, Code, Function, DockerImageFunction, DockerImageCode } = require('aws-cdk-lib/aws-lambda');
var { SpecRestApi, ApiDefinition } = require('aws-cdk-lib/aws-apigateway');
var { NodejsFunction } = require('aws-cdk-lib/aws-lambda-nodejs');
var { GoFunction } = require('@aws-cdk/aws-lambda-go-alpha');
var { PythonFunction, PythonLayerVersion } = require('@aws-cdk/aws-lambda-python-alpha');
var { Role, ServicePrincipal, PolicyStatement } = require('aws-cdk-lib/aws-iam');
var { RetentionDays } = require('aws-cdk-lib/aws-logs');
}
const isRunningOnCodeBuild = !!process.env.CODEBUILD_BUILD_ID;
class CDKSupportDemoRootStack extends Stack{
constructor(scope, id, props) {
super(scope, id, props);
// Python Runtime
// Layers
new PythonLayerVersion(this, 'PythonLayerVersion', {
compatibleRuntimes: [
Runtime.PYTHON_3_12,
],
entry: './src/python/Layer',
});
new LayerVersion(this, 'LayerVersion', {
compatibleRuntimes: [
Runtime.PYTHON_3_12,
],
code: Code.fromAsset('./src/python/Layer'),
});
new LayerVersion(this, 'BundledLayerVersionPythonRuntime', {
compatibleRuntimes: [
Runtime.PYTHON_3_12,
],
code: Code.fromAsset('./src/python/Layer', {
bundling: {
command: [
'/bin/sh',
'-c',
'rm -rf /tmp/asset-input && mkdir /tmp/asset-input && cp * /tmp/asset-input && cd /tmp/asset-input && pip install -r requirements.txt -t . && mkdir /asset-output/python && cp -R /tmp/asset-input/* /asset-output/python',
],
image: Runtime.PYTHON_3_12.bundlingImage,
user: 'root',
}
}),
});
// ZIP package type Functions
new PythonFunction(this, 'PythonFunction', {
entry: './src/python/Function',
index: 'app.py',
handler: 'lambda_handler',
runtime: Runtime.PYTHON_3_12,
functionName: 'pythonFunc',
logRetention: RetentionDays.THREE_MONTHS,
});
new Function(this, 'FunctionPythonRuntime', {
runtime: Runtime.PYTHON_3_12,
code: Code.fromAsset('./src/python/Function'),
handler: 'app.lambda_handler',
});
new Function(this, 'BundledFunctionPythonRuntime', {
runtime: Runtime.PYTHON_3_12,
code: Code.fromAsset('./src/python/Function/', {
bundling: {
command: [
'/bin/sh',
'-c',
'rm -rf /tmp/asset-input && mkdir /tmp/asset-input && cp * /tmp/asset-input && cd /tmp/asset-input && pip install -r requirements.txt -t . && cp -R /tmp/asset-input/* /asset-output',
],
image: Runtime.PYTHON_3_12.bundlingImage,
user: 'root',
}
}),
handler: 'app.lambda_handler',
});
// NodeJs Runtime
new NodejsFunction(this, 'NodejsFunction', {
entry: path.join(__dirname, '../src/nodejs/NodeJsFunctionConstruct/app.ts'),
depsLockFilePath: path.join(__dirname, '../src/nodejs/NodeJsFunctionConstruct/package-lock.json'),
bundling: {
forceDockerBundling: true,
},
handler: 'lambdaHandler',
});
// Go Runtime
new GoFunction(this, 'GoFunction', {
entry: './src/go/GoFunctionConstruct',
bundling: {
forcedDockerBundling: true,
// Only use Google proxy in the CI tests, as it is blocked on workstations
goProxies: isRunningOnCodeBuild ? [GoFunction.GOOGLE_GOPROXY, 'direct'] : undefined,
},
});
// Image Package Type Functions
new DockerImageFunction(this, 'DockerImageFunction', {
code: DockerImageCode.fromImageAsset('./src/docker/DockerImageFunctionConstruct', {
file: 'Dockerfile',
}),
});
// Spec Rest Api
new SpecRestApi(this, 'SpecRestAPI', {
apiDefinition: ApiDefinition.fromAsset('./src/rest-api-definition.yaml'),
});
// Role to be used as credentials for the Spec rest APi
// it is used inside the spec rest api definition file
new Role(this, 'SpecRestApiRole', {
assumedBy: new ServicePrincipal('apigateway.amazonaws.com'),
roleName: 'SpecRestApiRole',
}).addToPolicy(new PolicyStatement({
actions: ['lambda:InvokeFunction'],
resources: ['*'],
}));
// Nested Stack
new NestedStack1(this, 'NestedStack', {});
}
}
exports.CDKSupportDemoRootStack = CDKSupportDemoRootStack;