每天进步一点点:使用Python开发AWS LambdasteemCreated with Sketch.

in cn •  3 years ago 

最近开始使用AWS系列产品,感觉非常有趣。把学习过程记录一下。今天说的是如何使用Python开发一个Serverless的Lambda。以下的步骤在Ubuntu 18中测试通过。

安装serverless框架

npm install -g serverless

创建项目

mkdir -p hello-world
cd hello-world
serverless create --template aws-python3 --name hello-world

创建AWS用户

创建一个用户,例如:serverless-user,并将得到的access key及secret access key保存到 .aws/credentials

[serverless]
aws_access_key_id = XXXXXXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXX

部署到AWS

运行如下命令进行部署:

export AWS_PROFILE="serverless"
serverless deploy  --region eu-west-1

到目前为止,你还无法看到APIGateway对应的URL,因为它尚未被定义。

编辑 serverless.yml:

events:
    - http:
        path: hello
        method: get

再运行:

serverless deploy -v

你就会看到类似于:https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev ,通过这个URL就可以调用Lambda了。

测试

你可以通过上面的URL进行测试,也可以运行如下命令进行测试:

sls invoke -f hello

清理

在完成了这个程序之后,不要忘记清理资源,毕竟部署到AWS中的资源都是要花钱的。。。

serverless remove
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  • 스팀 코인판 커뮤니티를 이용해주셔서 감사합니다.
  • 2019년 10월 15일부터는 스팀코인판에서 작성한 글만 SCT 토큰을 보상받을 수 있습니다
  • 스팀 코인판 이외의 곳에서 작성된 글은 SCT 보상에서 제외되니 주의 바랍니다.
  • Thanks to everyone who continues to participate in SteemCoinPan community.
  • From Oct 15, 2019, we will provide SCT rewards for postings published on SteemCoinPan.
  • You won't get SCT rewards at all if you create a posting on other Steem Dapps after Oct 15, 2019.

Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!

Congratulations @aafeng! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published more than 300 posts. Your next target is to reach 350 posts.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Hi, @aafeng!

You just got a 0.89% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.