-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
132 lines (114 loc) · 4.41 KB
/
index.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
import apigateway from '@aws-cdk/aws-apigateway';
import dynamodb = require('@aws-cdk/aws-dynamodb');
import lambda from '@aws-cdk/aws-lambda';
import cdk from '@aws-cdk/core';
export class ApiLambdaCrudDynamoDBStack extends cdk.Stack {
constructor(app: cdk.App, id: string) {
super(app, id);
const dynamoTable = new dynamodb.Table(this, 'tasks', {
partitionKey: {
name: 'taskId',
type: dynamodb.AttributeType.STRING
},
tableName: 'tasks',
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const getOneLambda = new lambda.Function(this, 'getOneTaskFunction', {
code: new lambda.AssetCode('src'),
handler: 'get-one.handler',
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
TABLE_NAME: dynamoTable.tableName,
PRIMARY_KEY: 'taskId'
}
});
const getAllLambda = new lambda.Function(this, 'getAllTasksFunction', {
code: new lambda.AssetCode('src'),
handler: 'get-all.handler',
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
TABLE_NAME: dynamoTable.tableName,
PRIMARY_KEY: 'taskId'
}
});
const createOne = new lambda.Function(this, 'createTaskFunction', {
code: new lambda.AssetCode('src'),
handler: 'create.handler',
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
TABLE_NAME: dynamoTable.tableName,
PRIMARY_KEY: 'taskId'
}
});
const updateOne = new lambda.Function(this, 'updateTaskFunction', {
code: new lambda.AssetCode('src'),
handler: 'update-one.handler',
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
TABLE_NAME: dynamoTable.tableName,
PRIMARY_KEY: 'taskId'
}
});
const deleteOne = new lambda.Function(this, 'deleteTaskFunction', {
code: new lambda.AssetCode('src'),
handler: 'delete-one.handler',
runtime: lambda.Runtime.NODEJS_12_X,
environment: {
TABLE_NAME: dynamoTable.tableName,
PRIMARY_KEY: 'taskId'
}
});
dynamoTable.grantReadWriteData(getAllLambda);
dynamoTable.grantReadWriteData(getOneLambda);
dynamoTable.grantReadWriteData(createOne);
dynamoTable.grantReadWriteData(updateOne);
dynamoTable.grantReadWriteData(deleteOne);
const api = new apigateway.RestApi(this, 'todoApi', {
restApiName: 'User TODO Service'
});
const tasks = api.root.addResource('tasks');
const getAllIntegration = new apigateway.LambdaIntegration(getAllLambda);
tasks.addMethod('GET', getAllIntegration);
const createOneIntegration = new apigateway.LambdaIntegration(createOne);
tasks.addMethod('POST', createOneIntegration);
addCorsOptions(tasks);
const singleTask = tasks.addResource('{id}');
const getOneIntegration = new apigateway.LambdaIntegration(getOneLambda);
singleTask.addMethod('GET', getOneIntegration);
const updateOneIntegration = new apigateway.LambdaIntegration(updateOne);
singleTask.addMethod('PATCH', updateOneIntegration);
const deleteOneIntegration = new apigateway.LambdaIntegration(deleteOne);
singleTask.addMethod('DELETE', deleteOneIntegration);
addCorsOptions(singleTask);
}
}
export function addCorsOptions(apiResource: apigateway.IResource) {
apiResource.addMethod('OPTIONS', new apigateway.MockIntegration({
integrationResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'",
'method.response.header.Access-Control-Allow-Origin': "'*'",
'method.response.header.Access-Control-Allow-Credentials': "'false'",
'method.response.header.Access-Control-Allow-Methods': "'OPTIONS,GET,PUT,POST,DELETE'",
},
}],
passthroughBehavior: apigateway.PassthroughBehavior.NEVER,
requestTemplates: {
"application/json": "{\"statusCode\": 200}"
},
}), {
methodResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': true,
'method.response.header.Access-Control-Allow-Methods': true,
'method.response.header.Access-Control-Allow-Credentials': true,
'method.response.header.Access-Control-Allow-Origin': true,
},
}]
})
}
const app = new cdk.App();
new ApiLambdaCrudDynamoDBStack(app, 'ApiLambdaCrudDynamoDBExample');
app.synth();