-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapi-lambda-save-dynamodb-stack.ts
90 lines (83 loc) · 2.85 KB
/
api-lambda-save-dynamodb-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
import cdk = require('@aws-cdk/cdk');
import { CfnFunction, CfnApi } from '@aws-cdk/aws-sam';
import { AssetCode } from '@aws-cdk/aws-lambda';
import { Table, AttributeType, BillingMode } from '@aws-cdk/aws-dynamodb';
//import { RestApi, LambdaIntegration, IResource, MockIntegration, PassthroughBehavior } from '@aws-cdk/aws-apigateway';
export class ApiLambdaSaveDynamoDBStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const tableName = 'twitchViewers';
const primaryKeyName = 'viewerId';
const table = new Table(this, 'viewers-table', {
tableName: tableName,
partitionKey: {
name: primaryKeyName,
type: AttributeType.String
},
billingMode: BillingMode.PayPerRequest
});
const api = new CfnApi(this, 'viewersApi', {
stageName: 'prod',
cors: '"*"'
});
new CfnFunction(this, 'SaveToDynamoDB', {
codeUri: new AssetCode('src').path,
handler: 'index.handler',
runtime: 'nodejs8.10',
environment: {
variables: {
TABLE_NAME: table.tableName,
PRIMARY_KEY: primaryKeyName
}
},
policies: `
- DynamoDBCrudPolicy:
TableName: ${tableName}`,
events: {
save: {
type: 'Api',
properties: {
path: '/viewers',
method: 'POST',
restApiId: api.logicalId
}
},
saveOptions: {
type: 'Api',
properties: {
path: '/viewers',
method: 'OPTIONS',
restApiId: api.logicalId
}
}
}
});
}
}
/*function addCorsOptions(apiResource: IResource) {
apiResource.addMethod('OPTIONS', new MockIntegration({
integrationResponses: [{
statusCode: '200',
responseParameters: {
'method.response.header.Access-Control-Allow-Headers': "'ContentType,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,POST,PUT'",
}
}],
passthroughBehavior: 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-Origin': true,
'method.response.header.Access-Control-Allow-Credentials': true,
'method.response.header.Access-Control-Allow-Methods': true,
}
}]
});
}*/