最近开始使用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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations @aafeng! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
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!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit