AMAZON and Lambda(1)Lambda Introduction

AMAZON and Lambda(1)Lambda Introduction

Introduction Of Lambda
Lambda function: custom codes and dependent libraries
Event source: AWS service, for example, SNS, custom service which triggers my function and executes its logic.
Downstream resources: AWS service, DynamoDB tables or Amazon S3 bucket
Log streams:
AWS SAM: Serveless Application Model

On my Local Machine I already have AWS Cli installed
>aws --version
aws-cli/1.10.16 Python/2.7.13 Darwin/17.3.0 botocore/1.8.15

Try on lambda command to list the functions
>aws lambda list-functions
{
    "Functions": [
        {
            "TracingConfig": {
                "Mode": "PassThrough"
            },

Install SAM Local
Have Docker on Local first, I am using MAC
>docker --version
Docker version 18.02.0-ce-rc1, build 5e1d90a

https://github.com/lambci/docker-lambda

--rm, the container is removed when it exits or when the daemon exits, whichever happens first.
>docker run --rm -v "$PWD":/var/task lambci/lambda

Installing SAM Local
>npm install -g aws-sam-local

>sam --version
sam version 0.2.6

Create a Simple Lambda Function and Explore the Console
Author from Scratch or Blueprints after “Create Function”

Name: sqsEventConsumer
Runtime: Node.js 6.10
I choose create a new role and SQS Poller Permission to future usage.

After create the function, it will show up the next page.
In the Node.JS 6.10 example, it demos like
exports.handler = (event, context, callback) => {
    // TODO implement
    callback(null, 'Hello from Lambda');
};

Handler shows: index.handler, that is the file name and function name
Concurrent Execution Limit
http://docs.amazonaws.cn/en_us/lambda/latest/dg/concurrent-executions.html#per-function-concurrency

Click “Test”, then we can test the lambda.

Best practices
http://docs.amazonaws.cn/en_us/lambda/latest/dg/best-practices.html

Lambda Env
http://docs.amazonaws.cn/en_us/lambda/latest/dg/current-supported-versions.html

The demo hello world can be
exports.handler = (event, context, callback) => {
    var name = event.name;
    callback(null, name + ' Hello from Lambda');
};

The test data is as follow:
{
  "name": "test"
}

The result is
"test Hello from Lambda"


References:
https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/use-cases.html
http://docs.amazonaws.cn/lambda/latest/dg/getting-started.html
http://docs.amazonaws.cn/en_us/lambda/latest/dg/getting-started-create-function.html


猜你喜欢

转载自sillycat.iteye.com/blog/2409833