Skip to content

Instantly share code, notes, and snippets.

View leantorres73's full-sized avatar
🎯
Focusing

Leandro Torres leantorres73

🎯
Focusing
View GitHub Profile
var AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient();
const tableName = 'ws-connections';
interface IDynamoConnection {
Items: IConnection[];
}
interface IConnection {
userId: string;
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 = {
// CUSTOM API DOMAIN ------------------------------------------------------------------
// custom domain
const apigatewaydomainsocket = new apigateway.CfnDomainName(this, "apigatewaydomainsocket", {
domainName,
domainNameConfigurations:[{
certificateArn: wsCertificate,
endpointType: 'REGIONAL'
}]
// DEPLOY ------------------------------------------------------------------
// deployment
const apigatewaydeploymentsocket = new apigateway.CfnDeployment(this, "apigatewaydeploymentsocket", {
apiId: apigatewaysocket.ref
});
// stage
const apigatewaystagesocket = new apigateway.CfnStage(this, "apigatewaystagesocket", {
apiId: apigatewaysocket.ref,
// 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",
@leantorres73
leantorres73 / gist:21b3eca501b2f993b50f8e4f8561bf45
Created January 14, 2021 15:39
api policy receiver lambda
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"]
});
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',
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: [
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),