1

i created my app using AWS(API Gateway/Lambda/DynamoDB). but loading speed of the app is too late.

so, i want to improve the loading speed using DynamoDB DAX(cache).

serverless.yml

service: myapp
frameworkVersion: '2.31'

provider:
  name: aws
  runtime: nodejs14.x
  lambdaHashingVersion: 20201221
  stage: $opt:stage
  region: us-east-1
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dax:*
      Resource: '*'
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: '*'
  vpc:
    securityGroupIds:
      - !GetAtt daxSecurityGroup.GroupId
    subnetIds:
      - !Ref daxSubnet

functions:
  graphql:
    handler: ./build/src/app.handler
    events:
      - http:
          path: graphql
          method: ANY
    environment:
      DAX_ENDPOINT: !GetAtt daxCluster.ClusterDiscoveryEndpoint
    vpc:
      securityGroupIds:
        - !GetAtt daxSecurityGroup.GroupId
      subnetIds:
        - !Ref daxSubnet

resources:
  Resources:
    daxCluster:
      Type: AWS::DAX::Cluster
      Properties:
        ClusterName: dax-cluster
        IAMRoleARN: !GetAtt daxRole.Arn
        NodeType: dax.t2.small
        ReplicationFactor: 1
        SecurityGroupIds:
          - !GetAtt daxSecurityGroup.GroupId
        SubnetGroupName: !Ref daxSubnetGroup
    daxRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Statement:
            - Action:
              - sts:AssumeRole
              Effect: Allow
              Principal:
                Service:
                - dax.amazonaws.com
          Version: '2012-10-17'
        RoleName: dax-role
        ManagedPolicyArns:
          - arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess
    daxSecurityGroup:
      Type: AWS::EC2::SecurityGroup
      Properties:
        GroupDescription: Security Group for Dax
        GroupName: dax-security-group
        VpcId: !Ref daxVpc
    daxSecurityGroupIngress:
      Type: AWS::EC2::SecurityGroupIngress
      DependsOn: daxSecurityGroup
      Properties:
        GroupId: !GetAtt daxSecurityGroup.GroupId
        IpProtocol: tcp
        FromPort: 8111
        ToPort: 8111
        SourceSecurityGroupId: !GetAtt daxSecurityGroup.GroupId
    daxVpc:
      Type: AWS::EC2::VPC
      Properties:
        CidrBlock: 10.0.0.0/16
        EnableDnsHostnames: true
        EnableDnsSupport: true
        InstanceTenancy: default
        Tags:
          - Key: Name
            Value: dax-cluster
    daxSubnet:
      Type: AWS::EC2::Subnet
      Properties:
        AvailabilityZone:
          Fn::Select:
            - 0
            - Fn::GetAZs: ''
        CidrBlock: 10.0.0.0/20
        Tags:
          - Key: Name
            Value: dax-cluster
        VpcId: !Ref daxVpc
    daxSubnetGroup:
      Type: AWS::DAX::SubnetGroup
      Properties:
        Description: Subnet group for DAX
        SubnetGroupName: dax-subnet-group
        SubnetIds:
          - !Ref daxSubnet
    MyTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:provider.stage}_table
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        PointInTimeRecoverySpecification:
          PointInTimeRecoveryEnabled: true

app.ts

import { DynamoDB } from "aws-sdk";
import AmazonDaxClient from "amazon-dax-client";

export default async() {

...

const endpoint = process.env.DAX_ENDPOINT as string;
const config = { ... }
const dax: any = new AmazonDaxClient({endpoints: [endpoint], region: 'us-east-1'});
const dynamodb = new DynamoDB.DocumentClient({...config, service: dax});

await dynamodb.transactWrite({
 TransactItems: [
  {
   Delete: {
    TableName: 'development_table',
    Key: {
     id: args.id,
     createdAt: createdAt
    }
   }
  },
 ],
}).promise();

return ( ... )

}

My App can not connect to API Gateway.(Timeout) Please help me, That's very kind of you.

i tried...

serverless.yml

...

functions:
  graphql:
    handler: ./build/src/app.handler
    events:
      - httpApi:
          path: /{id+}
          method: GET
      - httpApi:
          path: /
          method: POST

...

But, App can not connect.

i referred to AWS blog

package

"@types/amazon-dax-client": "^1.2.3",
"amazon-dax-client": "^1.2.9",
matzok
  • 11
  • 1

0 Answers0