从阿里云函数迁移到 aws lambda
阿里云函数计算最近开始取消每个月的免费额度,吃相难看。虽然我平时跑跑个人的定时任务,用到的资源很少,还是决定迁移到别的平台。让它日活-1也算我做的一个贡献吧。
1/ 安装 aws CLI
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-version.html
2/ aws configure
https://docs.aws.amazon.com/cli/latest/userguide/welcome-examples.html
里面需要的秘钥创建参考:https://us-east-1.console.aws.amazon.com/iam/home?region=us-east-1#/security_credentials
3/ Create lambda by CLI
# 创建role、为role增加policy
aws iam create-role --role-name lambda-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaDynamoDBExecutionRole
# 创建
zip function.zip index.js
aws lambda create-function --function-name my-function --zip-file fileb://function.zip --handler index.handler --runtime nodejs16.x --role arn:aws:iam::044694559979:role/lambda-ex
# 查看和更新
aws lambda invoke --function-name my-function out --log-type Tail --query 'LogResult' --output text | base64 -d
aws lambda list-functions
aws lambda update-function-configuration --function-name my-function --memory-size 256
# 其他命令
aws lambda help
4/ CLI debug
https://docs.aws.amazon.com/lambda/latest/dg/python-exceptions.html#python-exceptions-cli
aws lambda invoke \
--function-name my-function \
--cli-binary-format raw-in-base64-out \
--payload '{"key1": "value1", "key2": "value2", "key3": "value3"}' output.txt
5/ 用 lambda 应用程序持续集成
参考 https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/applications-tutorial.html
根据上面指引创建应用程序,并绑定github的私有库,代码改动推送到仓库之后自动触发持续集成。build完之后自动发布。感觉良好,而且这里面的build deploy 都是在免费配额内的。build的时间是每个月100分钟,我实测每次花费1分钟左右,不要在上面跑耗时间的任务。