-
Notifications
You must be signed in to change notification settings - Fork 4k
/
integ.restapi.multistack.ts
49 lines (41 loc) · 1.45 KB
/
integ.restapi.multistack.ts
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
/// !cdk-integ *
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import * as constructs from 'constructs';
import * as apig from '../lib';
class FirstStack extends cdk.Stack {
public readonly firstLambda: lambda.Function;
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.firstLambda = new lambda.Function(this, 'firstLambda', {
functionName: 'FirstLambda',
code: lambda.Code.fromInline(`exports.handler = async function(event) {
return {
'headers': { 'Content-Type': 'text/plain' },
'statusCode': 200
}
}`),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
});
}
}
interface SecondStackProps extends cdk.StackProps {
readonly lambda: lambda.Function;
}
class SecondStack extends cdk.Stack {
constructor(scope: constructs.Construct, id: string, props: SecondStackProps) {
super(scope, id, props);
const api = new apig.RestApi(this, 'BooksApi', {
restApiName: 'SecondRestAPI',
});
api.root.addMethod('ANY');
const booksApi = api.root.addResource('books');
const lambdaIntegration = new apig.LambdaIntegration(props.lambda);
booksApi.addMethod('GET', lambdaIntegration);
}
}
const app = new cdk.App();
const first = new FirstStack(app, 'FirstStack');
new SecondStack(app, 'SecondStack', { lambda: first.firstLambda });
app.synth();