-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathexample-stack.ts
136 lines (117 loc) · 4.15 KB
/
example-stack.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
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
135
136
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
import { AttributeType, BillingMode, Table } from 'aws-cdk-lib/aws-dynamodb';
import { LayerVersion, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import {
NodejsFunction,
NodejsFunctionProps,
} from 'aws-cdk-lib/aws-lambda-nodejs';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { Construct } from 'constructs';
const commonProps: Partial<NodejsFunctionProps> = {
runtime: Runtime.NODEJS_20_X,
tracing: Tracing.ACTIVE,
timeout: Duration.seconds(30),
logRetention: RetentionDays.ONE_DAY,
environment: {
NODE_OPTIONS: '--enable-source-maps', // see https://docs.aws.amazon.com/lambda/latest/dg/typescript-exceptions.html
POWERTOOLS_SERVICE_NAME: 'items-store',
POWERTOOLS_METRICS_NAMESPACE: 'PowertoolsCDKExample',
POWERTOOLS_LOG_LEVEL: 'DEBUG',
},
bundling: {
externalModules: [
'@aws-lambda-powertools/commons',
'@aws-lambda-powertools/logger',
'@aws-lambda-powertools/tracer',
'@aws-lambda-powertools/metrics',
'@aws-lambda-powertools/parameters',
],
},
layers: [],
};
export class CdkAppStack extends Stack {
public constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const uuidApi = new UuidApi(this, 'uuid-api');
const table = new Table(this, 'Table', {
billingMode: BillingMode.PAY_PER_REQUEST,
partitionKey: {
type: AttributeType.STRING,
name: 'id',
},
});
commonProps.layers?.push(
LayerVersion.fromLayerVersionArn(
this,
'powertools-layer',
`arn:aws:lambda:${
Stack.of(this).region
}:094274105915:layer:AWSLambdaPowertoolsTypeScript:18`
)
);
const putItemFn = new NodejsFunction(this, 'put-item-fn', {
...commonProps,
entry: './functions/put-item.ts',
handler: 'handler',
});
putItemFn.addEnvironment('SAMPLE_TABLE', table.tableName);
table.grantWriteData(putItemFn);
const getAllItemsFn = new NodejsFunction(this, 'get-all-items-fn', {
...commonProps,
entry: './functions/get-all-items.ts',
handler: 'handler',
});
getAllItemsFn.addEnvironment('SAMPLE_TABLE', table.tableName);
table.grantReadData(getAllItemsFn);
const getByIdFn = new NodejsFunction(this, 'get-by-id-fn', {
...commonProps,
entry: './functions/get-by-id.ts',
handler: 'handler',
});
uuidApi.apiUrlParam.grantRead(getByIdFn);
uuidApi.apiUrlParam.grantRead(putItemFn);
uuidApi.apiUrlParam.grantRead(getAllItemsFn);
getByIdFn.addEnvironment('SAMPLE_TABLE', table.tableName);
table.grantReadData(getByIdFn);
const api = new RestApi(this, 'items-api', {
restApiName: 'Items Service',
description: 'This service serves items.',
deployOptions: {
tracingEnabled: true,
},
});
const itemPutIntegration = new LambdaIntegration(putItemFn);
api.root.addMethod('POST', itemPutIntegration);
const itemsIntegration = new LambdaIntegration(getAllItemsFn);
api.root.addMethod('GET', itemsIntegration);
const item = api.root.addResource('{id}');
const itemIntegration = new LambdaIntegration(getByIdFn);
item.addMethod('GET', itemIntegration);
}
}
class UuidApi extends Construct {
public readonly apiUrlParam: StringParameter;
public constructor(scope: Construct, id: string) {
super(scope, id);
const uuidFn = new NodejsFunction(this, 'UuidFn', {
runtime: Runtime.NODEJS_20_X,
entry: './functions/uuid.ts',
});
const api = new RestApi(this, 'uuid-api', {
restApiName: 'UUID Service',
description: 'This service serves UUIDs.',
deployOptions: {
tracingEnabled: true,
},
});
const uuidIntegration = new LambdaIntegration(uuidFn);
const uuid = api.root.addResource('uuid');
uuid.addMethod('GET', uuidIntegration);
this.apiUrlParam = new StringParameter(this, 'uuid-api-url', {
parameterName: '/app/uuid-api-url',
stringValue: `${api.url}/uuid`,
});
}
}