Last active
August 15, 2020 22:20
-
-
Save richardsonlima/e257d46ecdac6abaf0d8f8b16f1d6d4f to your computer and use it in GitHub Desktop.
node version v12.18.1 npm version 6.14.5 cdk version 1.59.0 (build 1d082f4)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create an API Gateway endpoint that invokes a Lambda function. This Lambda functions chooses a random number | |
// between 0 and 10000, and posts this number to an SQS queue. Another Lambda function picks up this message, | |
// and puts the value in DynamoDB | |
import * as cdk from '@aws-cdk/core'; | |
import { Queue } from '@aws-cdk/aws-sqs'; | |
import { Table, AttributeType } from '@aws-cdk/aws-dynamodb'; | |
import { Function, Runtime, Code } from '@aws-cdk/aws-lambda'; | |
import { RestApi, LambdaIntegration } from '@aws-cdk/aws-apigateway'; | |
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources'; | |
export class ServerlessStackStack extends cdk.Stack { | |
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
// The code that defines your stack goes here | |
const queue = new Queue(this, 'queue', { | |
queueName: 'queue' | |
}); | |
const table = new Table(this, 'table', { | |
partitionKey: { name: 'id', type: AttributeType.NUMBER } | |
}); | |
const publishFunction = new Function(this, 'publishFunction', { | |
runtime: Runtime.NODEJS_10_X, | |
handler: 'index.handler', | |
code: Code.asset('./handlers/publish'), | |
environment: { | |
QUEUE_URL: queue.queueUrl | |
}, | |
}); | |
const api = new RestApi(this, 'api', { | |
deployOptions: { | |
stageName: 'dev' | |
} | |
}); | |
api.root.addMethod('GET', new LambdaIntegration(publishFunction)); | |
const subscribeFunction = new Function(this, 'subscribeFunction', { | |
runtime: Runtime.NODEJS_10_X, | |
handler: 'index.handler', | |
code: Code.asset('./handlers/subscribe'), | |
environment: { | |
QUEUE_URL: queue.queueUrl, | |
TABLE_NAME: table.tableName | |
}, | |
events: [ | |
new SqsEventSource(queue) | |
] | |
}); | |
queue.grantSendMessages(publishFunction); | |
table.grant(subscribeFunction, "dynamodb:PutItem"); | |
// | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rename it to index.js and put on ./handlers/publish | |
const aws = require('aws-sdk'); | |
const sqs = new aws.SQS(); | |
exports.handler = async (event) => { | |
const randomInt = Math.floor(Math.random() * Math.floor(10000)).toString(); | |
const params = { | |
QueueUrl: process.env.QUEUE_URL, | |
MessageBody: randomInt | |
}; | |
await sqs.sendMessage(params).promise(); | |
return { | |
statusCode: 200, | |
body: `Successfully pushed message ${randomInt}!!` | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rename it to index.js and put on ./handlers/subscribe | |
const aws = require('aws-sdk'); | |
const dynamodb = new aws.DynamoDB(); | |
exports.handler = async (event) => { | |
for (const record of event.Records) { | |
const id = record.body; | |
console.log(id); | |
const params = { | |
TableName: process.env.TABLE_NAME, | |
Item: { | |
"id": { | |
N: id | |
} | |
} | |
} | |
await dynamodb.putItem(params).promise(); | |
} | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "serverless-stack", | |
"version": "0.1.0", | |
"bin": { | |
"serverless-stack": "bin/serverless-stack.js" | |
}, | |
"scripts": { | |
"build": "tsc", | |
"watch": "tsc -w", | |
"test": "jest", | |
"cdk": "cdk" | |
}, | |
"devDependencies": { | |
"@aws-cdk/assert": "1.59.0", | |
"@types/jest": "^26.0.4", | |
"@types/node": "10.17.27", | |
"jest": "^26.0.4", | |
"ts-jest": "^26.1.3", | |
"aws-cdk": "1.59.0", | |
"ts-node": "^8.1.0", | |
"typescript": "~3.9.6" | |
}, | |
"dependencies": { | |
"@aws-cdk/core": "1.59.0", | |
"source-map-support": "^0.5.16", | |
"@aws-cdk/aws-sqs": "^1.59.0", | |
"@aws-cdk/aws-dynamodb": "^1.59.0", | |
"@aws-cdk/aws-apigateway": "^1.59.0", | |
"@aws-cdk/aws-lambda-event-sources": "^1.59.0", | |
"@aws-cdk/aws-lambda": "^1.59.0", | |
"@aws-cdk/aws-iam": "^1.59.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import 'source-map-support/register'; | |
import * as cdk from '@aws-cdk/core'; | |
import { ServerlessStackStack } from '../lib/serverless-stack-stack'; | |
const app = new cdk.App(); | |
new ServerlessStackStack(app, 'ServerlessStackStack'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment