2018年1月31日 星期三

serverless (AWS lambda) + AWS mongodb cluster

照這個教學
https://s3.amazonaws.com/quickstart-reference/mongodb/latest/doc/MongoDB_on_the_AWS_Cloud.pdf


  1. 選 new VPC 
  2. 會到 cloudformation
  3. 下一步
    1. Availability Zones <=要全選
    2. Number of Availability Zones <=全部
    3. Allowed Bastion External Access CIDR Allowed CIDR <= 0.0.0.0/0
    4. Key Pair Name <= key選本機原有的就好了<=之後還要從本機copy到,他開的機器,做ssh 的測試。
    5. MongoDB Admin Username  ...
    6. MongoDB Admin Password ...
  4. 下一步
  5. 約20分鐘
  6. 會開出兩台 ec2
    1. LinuxBastion <= 測試你的 mongo cluster 用的
    2. PrimaryReplicaNode0 <= mongo cluster 只有 vpc 能連
  7. pdf 的測試方式為
    1. scp -i key .... 到 LinuxBastion
      scp –i mykey.pem mykey.pem ec2-user@Bastion-public-ip:/home/ec2-user/mykey.pem
    2. ssh  - i key PrimaryReplicaNode0的ip
    3. mongo 
      mongo
      use admin
      db.auth("admin", "YourAdminPassword") rs.printReplicationInfo()
      rs.status()
  8. 應該都正常了
===lambda
    1. 我直接改這個範例做測試
      https://github.com/serverless/examples/tree/master/aws-node-rest-api-mongodb

    2. service
      : aws-node-rest-api-mongodb

      provider:
      name: aws
      region: ap-northeast-1 //記得region 要設對,不然找不到 sg
      runtime: nodejs6.10
      vpc:
      securityGroupIds:
      - "sg-db38a1xx" //加這個
      subnetIds:
      - "subnet-39dd6bxx" //加這個
      - "subnet-44e59exx" //加這個

      functions:
      createUser:
      handler: handler.createUser
      events:
      - http:
      path: user
      method: post
      cors: true
      vpc:
      securityGroupIds:
      - "sg-db38a1xx" //加這個
      subnetIds:
      - "subnet-39dd6bxx" //加這個
      - "subnet-44e59exx" //加這個


    3. securityGroupIds  <= 來自ec2 => PrimaryReplicaNode0 => Security groups
       => 點進去 => 看 Source
    4. Subnet ID <= 來自ec2 => PrimaryReplicaNode0 => Subnet ID 
    5. sls deploy
endpoints:
  POST - https:// x.execute-api.ap-northeast-1.amazonaws.com/dev/user
  PUT - https://x.execute-api.ap-northeast-1.amazonaws.com/dev/user/{id}
  DELETE - https://x.execute-api.ap-northeast-1.amazonaws.com/dev/user/{id}
  GET - https://x.execute-api.ap-northeast-1.amazonaws.com/dev/user/{id}

照這個試試吧!
結束!


2018年1月27日 星期六