This file contains 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
var AWS = require("aws-sdk"); | |
const docClient = new AWS.DynamoDB.DocumentClient(); | |
const tableName = 'ws-connections'; | |
interface IDynamoConnection { | |
Items: IConnection[]; | |
} | |
interface IConnection { | |
userId: string; |
This file contains 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
var AWS = require("aws-sdk"); | |
const docClient = new AWS.DynamoDB.DocumentClient(); | |
const tableName = 'ws-connections'; | |
exports.main = async (event: any) => { | |
const token = getContextData(event); | |
switch (event.requestContext.routeKey) { | |
case "$connect": | |
const putParams = { |
This file contains 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
// CUSTOM API DOMAIN ------------------------------------------------------------------ | |
// custom domain | |
const apigatewaydomainsocket = new apigateway.CfnDomainName(this, "apigatewaydomainsocket", { | |
domainName, | |
domainNameConfigurations:[{ | |
certificateArn: wsCertificate, | |
endpointType: 'REGIONAL' | |
}] |
This file contains 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
// DEPLOY ------------------------------------------------------------------ | |
// deployment | |
const apigatewaydeploymentsocket = new apigateway.CfnDeployment(this, "apigatewaydeploymentsocket", { | |
apiId: apigatewaysocket.ref | |
}); | |
// stage | |
const apigatewaystagesocket = new apigateway.CfnStage(this, "apigatewaystagesocket", { | |
apiId: apigatewaysocket.ref, |
This file contains 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
// connect route | |
const apigatewayroutesocketconnect = new apigateway.CfnRoute(this, "apigatewayroutesocketconnect", { | |
apiId:apigatewaysocket.ref, | |
routeKey: "$connect", | |
authorizationType: "AWS_IAM", | |
apiKeyRequired: false, | |
operationName: "ConnectRoute", | |
target: "integrations/"+new apigateway.CfnIntegration(this, "apigatewayintegrationsocketconnect", { | |
apiId: apigatewaysocket.ref, | |
integrationType: "AWS_PROXY", |
This file contains 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
const roleapigatewaysocketapi = new iam.Role(this, "roleapigatewaysocketapi", { | |
assumedBy: new iam.ServicePrincipal("apigateway.amazonaws.com") | |
}); | |
// access role for the socket api to access the socket lambda | |
const policy = new iam.PolicyStatement({ | |
effect: iam.Effect.ALLOW, | |
resources: [lambdaAPIsocket.functionArn], | |
actions: ["lambda:InvokeFunction"] | |
}); |
This file contains 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
const wsConnections = new dynamodb.Table(this, 'WSconnections', { | |
tableName: 'ws-connections', | |
partitionKey: { name: 'userId', type: dynamodb.AttributeType.STRING }, | |
sortKey: { name: 'connectionId', type: dynamodb.AttributeType.STRING }, | |
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, | |
stream: dynamodb.StreamViewType.NEW_IMAGE | |
}); | |
wsConnections.addGlobalSecondaryIndex({ | |
indexName: 'connectionId', |
This file contains 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
const authRole = iam.Role.fromRoleArn(this, 'AuthRole', `arn:aws:iam::${Stack.of(this).region}:${Stack.of(this).account}:role/CognitoDefaultAuthenticatedRole`); | |
// Policy for Cognito invoking API Gateway | |
new iam.Policy(this, `${repositoryName}-Policy`, { | |
policyName: `${repositoryName}-Policy`, | |
roles: [authRole], | |
statements: [ | |
new iam.PolicyStatement({ | |
effect: iam.Effect.ALLOW, | |
resources: [ |
This file contains 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
const domainName = ssm.StringParameter.valueForStringParameter(this, '/ws-domain-name'); | |
const wsCertificate = ssm.StringParameter.valueForStringParameter(this, '/ws-certificate'); | |
// RECEIVER | |
const lambdaAPIsocket = new lambda.Function(this, 'wsReceiver', { | |
functionName: 'wsReceiver', | |
runtime: lambda.Runtime.NODEJS_12_X, | |
tracing: Tracing.ACTIVE, | |
handler: 'src/index.main', | |
timeout: Duration.seconds(30), |