aws lambda使用_使用AWS Lambda 1设置

aws lambda使用

Before getting started with lambda setup lets take a look at our use case and how we landed on serverless as a solution to our problem .

在开始使用lambda设置之前,让我们看一下用例以及如何使用无服务器作为解决问题的方法。

Problem statement :- Every product has its website and so did ours but it wasn’t accessed very frequently as majority of our traffic was on the mobile application , but sometimes we witnessed traffic surge .

问题陈述:-每个产品都有其网站,我们的网站也有它的网站,但是由于我们的大部分流量是通过移动应用程序进行的,因此它的访问频率并不高,但是有时我们目睹了流量的激增。

Solution :- To serve the Rest APIs required by the website , provisioning a server was not the best solution as it would require more maintenance and we had to pay even if there was no traffic

解决方案:-为了满足网站所需的Rest API,配置服务器不是最佳解决方案,因为它将需要更多维护,即使没有流量,我们也必须付费

[ENTER SERVERLESS]

[无服务器]

Using serverless functions we could easily serve the needs of the website without worrying much about the provisioning , maintenance of the server and also paying only for what we use

使用无服务器功能,我们可以轻松满足网站的需求,而不必担心服务器的配置,维护以及仅为使用的服务器付费

Summary :- This is why we chose serveless as a solution to our problem but this is not the case every time as lambda itself has some drawbacks and in a few cases provisioning a server is better , so choose wisely

简介:-这就是为什么我们选择“无用”作为解决问题的原因,但并非每次Lambda本身都有一些缺点,并且在某些情况下配置服务器更好的情况下并非如此,因此请明智地选择

配料 : (Ingredients :)

To build a serverless backend you’ll need ,

要构建无服务器后端,您需要,

  • A lambda function

    Lambda函数
  • An API gateway (to trigger lambda function)

    API网关(触发lambda函数)
  • Some IAM Stuff (https://docs.aws.amazon.com/lambda/latest/dg/lambda-permissions.html)

    一些IAM资料( https://docs.aws.amazon.com/lambda/latest/dg/lambda-permissions.html )

aws lambda使用_使用AWS Lambda 1设置_第1张图片
Made by me on onenote 我在onenote上制作的

食谱 : (Recipe :)

Setting up aws lambda is very easy , all you need is good code . You can find awesome guides on getting started with serverless and that should help you set up a basic hello world “ function . I’ve listed down a few steps incase you need them

设置aws lambda非常容易,您只需要好的代码。 您可以找到有关无服务器入门的出色指南,这些指南应有助于您建立基本的 hello world”功能。 我列出了一些步骤,以防您需要

aws lambda使用_使用AWS Lambda 1设置_第2张图片
these steps are too simple 这些步骤太简单了

And here is a sample serverless.yml file to define your function

这是一个示例serverless.yml文件,用于定义您的功能

service: demo-service
provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: ap-south-1
functions:
  webApp:
    handler: handler.index
    events:
     - http: ANY /
     - http: 'ANY {proxy +}'

But wait , you don’t run hello world “ in production ? The basic example has some drawbacks if you're running it in production

但是,等等,您不在生产中运行 hello world”吗? 如果您在生产环境中运行基本示例,则存在一些缺陷

  • You need to zip the code every time and update it through the lambda console , which is not considered a very good practice

    您每次都需要压缩代码并通过lambda控制台对其进行更新,这不是很好的做法
  • If you’ve multiple event triggers , you’ll have to set them up and update them manually

    如果您有多个事件触发器,则必须对其进行设置并手动更新

[输入无服务器应用程序模型(SAM)] ([ENTER SERVERLESS APPLICATION MODEL (SAM)])

Sam is a service provided by AWS to deploy serverless applications with ease , Sam takes care of provisioning and updating every service related to the service definition . SAM is basically a wrapper around Cloudformation , if you’ve used that , Also it makes local development a lot easy . All you need is a template.yml file.

Sam是AWS提供的一项服务,用于轻松部署无服务器应用程序,Sam负责配置和更新与服务定义相关的每项服务。 SAM基本上是Cloudformation的包装,如果您使用过SAM,那么它也使本地开发变得非常容易。 您只需要一个template.yml文件。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31 #this defines that is a SAM template
Description: > # Description
  Sample lambda server
  SAM Template for lambda-api-gateway
Resources: # Resources that this SAM wil configure it can contain (API gateway definition,Lambda and many more)
  WebsiteLambda: 
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./package #this is where my code is
      Handler: index.handler # {EntryFile}.{HandlerFunction}
      Runtime: nodejs12.x  
      Timeout : 5 #Optional
      Role : lambda-arn #Role required by your function,if not provided SAM will create one for you
      Environment: #ENV variables
         Variables:
             NODE_ENV: staging
             LAMBDA : false
      Events: # trigger events
        WebsiteApi: 
          Type: Api # API-> API gateway 
          Properties:
            Path: /{proxy+} # wildcard for accept anything 
            Method: any # ANY method


Outputs: #Outputs of the sam template
  WebsiteLambdaAPI:
    Description: "API Gateway endpoint URL for  website function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
  WebsiteLambda:
    Description: "website lambda function"
    Value: !GetAtt WebsiteLambda.Arn

And now you can use simple commands to deploy , test your function . Before starting make sure you’ve properly configure AWS-cli with proper roles

现在,您可以使用简单的命令来部署,测试功能。 在开始之前,请确保已正确配置具有适当角色的AWS-cli

  • Build

    建立
sam build
  • Deploy

    部署
sam deploy --guided
  • Test

    测试
sam local invoke

to test using the api gateway

使用api网关进行测试

sam local start-api

Getting started with sam might take a while if you’re not familiar with cloudformation , but it’ll make developing and deploying lambda easier

如果您不熟悉cloudformation,可能需要一段时间才能开始使用sam,但这会使lambda的开发和部署更加容易

使用Lambda函数时要记住的注意事项 (Considerations to keep in mind while using Lambda Functions)

  • Lambda functions do not have a static ip

    Lambda函数没有静态IP
  • You need to configure lambda to be accessible in VPC

    您需要配置Lambda以在VPC中访问
  • You cannot access the Internet from Lambda , for that you’ll need to add a NAT gateway to your VPC

    您无法从Lambda访问Internet,因为您需要向VPC添加NAT网关

在Lambda中使用RDS: (Using RDS In Lambda :)

If you want to access RDS from lambda , it is highly recommended that you use RDS Proxy (https://aws.amazon.com/rds/proxy/) to connect to database . RDS Proxy will manage a pool of warm connections so that when functions scale , they do not overload your db by eating up all the connections also make sure your lambda is in the same VPC .

如果要从lambda访问RDS,强烈建议您使用RDS代理( https://aws.amazon.com/rds/proxy/ )连接到数据库。 RDS代理将管理一个热连接池,以便在函数扩展时,它们不会通过耗尽所有连接来使您的数据库过载,并且还要确保您的lambda位于同一VPC中。

从Lambda访问S3: (Accessing S3 from Lambda :)

to access s3 from lambda , you need to add a VPC Endpoint (https://docs.aws.amazon.com/vpc/latest/userguide/vpce-gateway.html) to s3 in your VPC , you cannot directly access s3 from lambda

要从lambda访问s3,您需要将VPC端点( https://docs.aws.amazon.com/vpc/latest/userguide/vpce-gateway.html )添加到VPC中的s3,您不能直接从拉姆达

Now since we’re ready with our setup , its time to write some code . See you in the follow up article . Feel free to drop a question or a feedback below :)

现在,既然我们已经准备好设置,就该写一些代码了。 在后续文章中见。 请在下面随意提出问题或反馈:)

Part 0 :

第0部分:

翻译自: https://medium.com/@rahultripathidev/using-aws-lambda-1-setup-820f794d4d1c

aws lambda使用

你可能感兴趣的:(python)