无服务器框架和AWS Lambda函数代码

时间:2020-06-28 22:49:59

标签: aws-lambda serverless-framework

我是Serverless框架的新手。有没有一种方法可以使用无服务器框架从NodeJS部署功能,以使本地src文件中的代码与Lambda函数中的代码相同?我想看一下原始代码,也许手动在其中放一些console.log,但我看到的只是缩小的代码:

enter image description here

serverless.ymp代码

service: XXX
provider:
name: aws
runtime: nodejs12.x
region: ${opt:region, 'us-east-2'}
stage: ${opt:stage, 'dev'}
tags:
    datadog: ${self:provider.stage}
environment:
    DYNAMODB_XXXXXS: ${self:provider.stage}-banks
    DYNAMODB_XXXXX_ACCOUNTS: ${self:provider.stage}-bank-accounts
    NODE_STAGE: ${self:provider.stage}
iamRoleStatements:
    - Effect: Allow
    Action:
        - logs:CreateLogGroup
        - logs:CreateLogStream
        - logs:PutLogEvents
    Resource: "arn:aws:logs:*:*:*"
    - Effect: Allow
    Action:
        - dynamodb:Query
    Resource:
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.DYNAMODB_XXXXXS}"
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.DYNAMODB_XXXXXS}/*"
    - Effect: Allow
    Action:
        - dynamodb:PutItem
    Resource:
        - "arn:aws:dynamodb:${self:provider.region}:*:table/${self:provider.environment.DYNAMODB_XXXXX_ACCOUNTS}"
functions:
banks:
    handler: src/index.handler
    events:
    - http:
        path: /banks
        method: post
        cors: true
        authorizer:
            name: authorizer
            arn: arn:aws:cognito-idp:${self:provider.region}:2XXXX4:userpool/${self:custom.XXX.cognito.userpool}
    - http:
        path: /banks
        method: get
        cors: true
        authorizer:
            name: authorizer
            arn: arn:aws:cognito-idp:${self:provider.region}:XXXX04:userpool/${self:custom.XXx.cognito.userpool}
plugins:
- serverless-dynamodb-local
- serverless-webpack
- serverless-offline
package:
individually: true
exclude:
    - node_modules/**
resources:
Resources:
    DynamoDBTableBanks:
    Type: AWS::DynamoDB::Table
    Properties:
        TableName: ${self:provider.environment.DYNAMODB_XXXXXS}
        KeySchema:
        - AttributeName: code
            KeyType: HASH
        - AttributeName: agent
            KeyType: RANGE
        AttributeDefinitions:
        - AttributeName: code
            AttributeType: S
        - AttributeName: agent
            AttributeType: S
        - AttributeName: country
            AttributeType: S
        GlobalSecondaryIndexes:
        - IndexName: banksByAgentCountry
            KeySchema:
            - AttributeName: agent
                KeyType: HASH
            - AttributeName: country
                KeyType: RANGE
            Projection:
            ProjectionType: "ALL"
            ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
        ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
    DynamoDBTableBankAccount:
    Type: AWS::DynamoDB::Table
    Properties:
        TableName: ${self:provider.environment.DYNAMODB_XXXXX_ACCOUNTS}
        KeySchema:
        - AttributeName: id
            KeyType: HASH
        - AttributeName: receiverId
            KeyType: RANGE
        AttributeDefinitions:
        - AttributeName: id
            AttributeType: S
        - AttributeName: receiverId
            AttributeType: S
        ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
custom:
XXXX:
    cognito:
    userpool: ${env:COGNITO_USER_POOL_ID, 'us-east-2_XXXX'}
dynamodb:
    stages:
    - dev
    start:
    convertEmptyValues: true
    heapInitial: 200m
    heapMax: 1g
    inMemory: true
    migrate: true
    port: 8000
webpack:
    webpackConfig: "webpack.config.js"
    packager: "npm"
    includeModules:
    forceExclude:
        - aws-sdk

1 个答案:

答案 0 :(得分:0)

这不是必需的。只需将console.log添加到代码中,当您在Lambda中对其进行测试时,就会在CloudWatch中看到它的回声。如果您想改善反馈回路,请考虑使用由Serverless团队开发的Studio之类的工具,旨在为您提供开发模式体验。您只需要连接到Serverless Framework仪表板(https://www.serverless.com/framework/docs/dashboard#enabling-the-dashboard-on-existing-serverless-framework-services),然后在CLI上运行serverless dev,现在当您保存Lambda处理程序代码时,它将自动检测到该代码,并在几秒钟内将其上传到AWS中并提供您需要邮递员风格的界面进行测试。只需点击信息中心顶部菜单上的“工作室”即可访问它。