|
| 1 | +import apigateway = require('@aws-cdk/aws-apigateway'); |
| 2 | +import dynamodb = require('@aws-cdk/aws-dynamodb'); |
| 3 | +import lambda = require('@aws-cdk/aws-lambda'); |
| 4 | +import cdk = require('@aws-cdk/cdk'); |
| 5 | + |
| 6 | +export class ApiLambdaCrudDynamoDBStack extends cdk.Stack { |
| 7 | + constructor(app: cdk.App, id: string) { |
| 8 | + super(app, id); |
| 9 | + |
| 10 | + const dynamoTable = new dynamodb.Table(this, 'items', { |
| 11 | + partitionKey: { |
| 12 | + name: 'itemId', |
| 13 | + type: dynamodb.AttributeType.String |
| 14 | + }, |
| 15 | + tableName: 'items' |
| 16 | + }); |
| 17 | + |
| 18 | + const getOneLambda = new lambda.Function(this, 'getOneItemFunction', { |
| 19 | + code: new lambda.AssetCode('src'), |
| 20 | + handler: 'get-one.handler', |
| 21 | + runtime: lambda.Runtime.NodeJS810, |
| 22 | + environment: { |
| 23 | + TABLE_NAME: dynamoTable.tableName, |
| 24 | + PRIMARY_KEY: 'itemId' |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + const getAllLambda = new lambda.Function(this, 'getAllItemsFunction', { |
| 29 | + code: new lambda.AssetCode('src'), |
| 30 | + handler: 'get-all.handler', |
| 31 | + runtime: lambda.Runtime.NodeJS810, |
| 32 | + environment: { |
| 33 | + TABLE_NAME: dynamoTable.tableName, |
| 34 | + PRIMARY_KEY: 'itemId' |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + const createOne = new lambda.Function(this, 'createItemFunction', { |
| 39 | + code: new lambda.AssetCode('src'), |
| 40 | + handler: 'create.handler', |
| 41 | + runtime: lambda.Runtime.NodeJS810, |
| 42 | + environment: { |
| 43 | + TABLE_NAME: dynamoTable.tableName, |
| 44 | + PRIMARY_KEY: 'itemId' |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + const updateOne = new lambda.Function(this, 'updateItemFunction', { |
| 49 | + code: new lambda.AssetCode('src'), |
| 50 | + handler: 'update-one.handler', |
| 51 | + runtime: lambda.Runtime.NodeJS810, |
| 52 | + environment: { |
| 53 | + TABLE_NAME: dynamoTable.tableName, |
| 54 | + PRIMARY_KEY: 'itemId' |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + const deleteOne = new lambda.Function(this, 'deleteItemFunction', { |
| 59 | + code: new lambda.AssetCode('src'), |
| 60 | + handler: 'delete-one.handler', |
| 61 | + runtime: lambda.Runtime.NodeJS810, |
| 62 | + environment: { |
| 63 | + TABLE_NAME: dynamoTable.tableName, |
| 64 | + PRIMARY_KEY: 'itemId' |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + dynamoTable.grantReadWriteData(getAllLambda); |
| 69 | + dynamoTable.grantReadWriteData(getOneLambda); |
| 70 | + dynamoTable.grantReadWriteData(createOne); |
| 71 | + dynamoTable.grantReadWriteData(updateOne); |
| 72 | + dynamoTable.grantReadWriteData(createOne); |
| 73 | + dynamoTable.grantReadWriteData(deleteOne); |
| 74 | + |
| 75 | + const api = new apigateway.RestApi(this, 'itemsApi', { |
| 76 | + restApiName: 'Items Service' |
| 77 | + }); |
| 78 | + |
| 79 | + const items = api.root.addResource('items'); |
| 80 | + const getAllIntegration = new apigateway.LambdaIntegration(getAllLambda); |
| 81 | + items.addMethod('GET', getAllIntegration); |
| 82 | + |
| 83 | + const createOneIntegration = new apigateway.LambdaIntegration(createOne); |
| 84 | + items.addMethod('POST', createOneIntegration); |
| 85 | + addCorsOptions(items); |
| 86 | + |
| 87 | + const singleItem = items.addResource('{id}'); |
| 88 | + const getOneIntegration = new apigateway.LambdaIntegration(getOneLambda); |
| 89 | + singleItem.addMethod('GET', getOneIntegration); |
| 90 | + |
| 91 | + const updateOneIntegration = new apigateway.LambdaIntegration(updateOne); |
| 92 | + singleItem.addMethod('PATCH', updateOneIntegration); |
| 93 | + |
| 94 | + const deleteOneIntegration = new apigateway.LambdaIntegration(deleteOne); |
| 95 | + singleItem.addMethod('DELETE', deleteOneIntegration); |
| 96 | + addCorsOptions(singleItem); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export function addCorsOptions(apiResource: apigateway.IResource) { |
| 101 | + apiResource.addMethod('OPTIONS', new apigateway.MockIntegration({ |
| 102 | + integrationResponses: [{ |
| 103 | + statusCode: '200', |
| 104 | + responseParameters: { |
| 105 | + 'method.response.header.Access-Control-Allow-Headers': "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'", |
| 106 | + 'method.response.header.Access-Control-Allow-Origin': "'*'", |
| 107 | + 'method.response.header.Access-Control-Allow-Credentials': "'false'", |
| 108 | + 'method.response.header.Access-Control-Allow-Methods': "'OPTIONS,GET,PUT,POST,DELETE'", |
| 109 | + }, |
| 110 | + }], |
| 111 | + passthroughBehavior: apigateway.PassthroughBehavior.Never, |
| 112 | + requestTemplates: { |
| 113 | + "application/json": "{\"statusCode\": 200}" |
| 114 | + }, |
| 115 | + }), { |
| 116 | + methodResponses: [{ |
| 117 | + statusCode: '200', |
| 118 | + responseParameters: { |
| 119 | + 'method.response.header.Access-Control-Allow-Headers': true, |
| 120 | + 'method.response.header.Access-Control-Allow-Methods': true, |
| 121 | + 'method.response.header.Access-Control-Allow-Credentials': true, |
| 122 | + 'method.response.header.Access-Control-Allow-Origin': true, |
| 123 | + }, |
| 124 | + }] |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +const app = new cdk.App(); |
| 129 | +new ApiLambdaCrudDynamoDBStack(app, 'ApiLambdaCrudDynamoDBExample'); |
| 130 | +app.run(); |
0 commit comments