没有合适的资源?快使用搜索试试~ 我知道了~
AWS Lambda In Action Chapter2
需积分: 10 13 下载量 61 浏览量
2017-11-13
03:52:12
上传
评论
收藏 716KB PDF 举报
温馨提示
试读
11页
amazon web services 中的lambda服务介绍,第二章内容。
资源推荐
资源详情
资源评论
Username: Columbia University Book: AWS Lambda in Action: Event-driven serverlessapplications. No part of any chapter or book may be reproduced or
transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher of the book or chapter. Redistribution or
other use that violates the fair use privilege under U.S. copyright laws (see 17 USC107) or that otherwise violates these Terms of Service is strictly prohibited.
Violators will be prosecuted to the full extent of U.S. Federal and Massachusetts laws.
Chapter 2. Your first Lambda function
This chapter covers
Creating your first AWS Lambda function
Understanding function configurations and settings
Testing functions from the web console
Using the AWS command-line interface to call functions
In the first chapter you learned how AWS Lambda functions work and how they can be used synchronously (returning a result) or asynchronously (for example,
subscribing a function to an event). In the second part of the chapter you learned how a group of functions can be used to build an event-driven application in
which the logic is bound to events coming from outside (a client application) or inside (relationships among data).
It’s now time to build your first functions and see how they can be used from a client application using the AWS Lambda interface. The AWS Lambda interface is
easy to use with the AWS command-line interface (CLI), or one of the AWS software development kits (SDKs) that can run on a server, in a browser, or in a mobile
client.
2.1. Creating a new function
Any good programming book should start with the “Hello World” example. But with AWS Lambda you don’t have standalone applications, but functions that take
an input (event) and can optionally provide a result (when called synchronously).
Let’s start with a slightly more complex task: a function that’s looking into the event for a name to “greet” and returns “Hello <name>!” If no name is provided in the
event, then the function should return a more generic “Hello World!”
Tip
You need an AWS account to follow the examples in this book. With a new AWS account, the Free Tier covers all the examples in this book with no cost to you. For
more information on the AWS Free Tier and how to create a new AWS account, please visit http://aws.amazon.com/free/.
To create your first function, open your browser and go to https://console.aws.amazon.com/. Log in with your AWS credentials and select Lambda from the Compute
section; choose your preferred AWS region from the menu at the top right (usually the one closest to you to reduce network latency) and then click “Get Started
Now” on the welcome page. If this isn’t the first function in that region, instead of the welcome page you’ll see the list of functions, and you can select “Create a
Lambda function” to proceed.
To simplify the creation of a new Lambda function, blueprints are provided to show the integration with other AWS services and other services such as Amazon
Alexa, Twilio, and Algorithmia (figure 2.1). Select the “Blank Function” to start from scratch.
Figure 2.1. With AWS Lambda you can create a new function using one of the provided blueprints, but for your first function you’ll start from scratch.
You can then choose a trigger for the new function (figure 2.2). A trigger is a source of events that will execute the function, providing the event an input. You have
multiple options for the trigger; a few of them will be used by the examples you’ll build while reading this book.
Figure 2.2. You can choose a trigger during the creation of a new function. Triggers invoke a Lambda function when certain events happen.
For example, you can choose the Amazon API Gateway to have a Web API calling a function, or AWS IoT, to build a serverless back end for an Internet of Things
(IoT) platform connecting devices with AWS. You’ll directly invoke this function and don’t need a trigger for now. Select “Next.”
You can now create a new AWS Lambda function (figure 2.3). For the name of the function, type “greetingsOnDemand”.
Figure 2.3. Configuring a new AWS Lambda function, starting with a name and a description, the runtime to use, and the code to execute
Note
AWS Lambda has no official rule on how to write function names. Sometimes function names are written in all lowercase, with dashes to separate words. In this
book, I’ll follow the lowerCamelCase convention, joining all words together, starting with a lowercase letter and using uppercase for the first letter of every word
after the first one.
The description text can be “Returns greetings when you ask for them.” Giving a meaningful description can help when building an application with multiple
functions or to facilitate reuse of a function (or only its code) for different purposes.
Tip
Descriptions can also be read through AWS APIs, and having conventions in the content of the description—for example, describing the expected input/output—
can help in implementing automated “discovery” for your functions.
Using the AWS Lambda web console, you can provide the code for your function in three different ways:
Edit code inline, straight from the web browser
Upload a zip file from your local environment
Upload a zip file from Amazon S3
You can include custom libraries or modules that your code depends on in the zip file. In part 3 you’ll see that uploading functions via Amazon S3 can be an
interesting feature for implementing automatic deployments and continuous integration with AWS Lambda.
As the runtime for your function, you can choose Node.js 4.3 or Python 2.7. Examples of both are provided in this chapter; choose the language you prefer for your
back end deployment.
Using the Java Runtime
I’m not providing Java 8 examples throughout the book for the sake of simplicity, because Java code cannot be written inline in the web console and needs to be
compiled, packaged, and uploaded. For that, my suggestion is to use the AWS Toolkit for Eclipse, found at https://aws.amazon.com/eclipse.
剩余10页未读,继续阅读
资源评论
lmhapxh
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功