多伦多到温莎

If you read About time, you’ll know that I’m a big believer in spending time now on building things that save time in the future. To this end, I built a simple Twitter bot in Go that would occasionally post links to my articles and keep my account interesting even when I’m too busy to use it. The tweets help drive traffic to my sites, and I don’t have to lift a finger.

如果您阅读《 关于时间》 ,您会知道我非常相信现在花时间来构建可以节省时间的东西。 为此,我在Go中构建了一个简单的Twitter机器人,该机器人有时会发布指向我文章的链接,即使在我忙于使用它的情况下,也会使我的帐户变得有趣。 这些推文有助于吸引流量到我的网站,而我不必费力。

I ran the bot on an Amazon EC2 instance for about a month. My AWS usage has historically been pretty inexpensive (less than the price of a coffee in most of North America), so I was surprised when the little instance I was using racked up a bill 90% bigger than the month before. I don’t think AWS is expensive, to be clear, but still… I’m cheap. I want my Twitter bot, and I want it for less.

我在Amazon EC2实例上运行该机器人大约一个月。 从历史上看,我对AWS的使用非常便宜(低于北美大部分地区的咖啡价格),所以当我使用的小实例的账单比前一个月大90%时,我感到惊讶。 显然,我认为AWS并不昂贵,但仍然……我很便宜。 我想要我的Twitter机器人,而且想要更少的钱。

I’d been meaning to explore AWS Lamda, and figured this was a good opportunity. Unlike an EC2 instance that is constantly running (and charging you for it), Lambda charges you per request and according to the duration of the time your function takes to run. There’s a free tier, too, and the first 1 million requests, plus a certain amount of compute time, are free.

我一直想探索AWS Lamda,并认为这是一个很好的机会。 与不断运行(并向您收费)的EC2实例不同,Lambda会根据您的请求并根据功能运行的持续时间向您收费。 也有一个免费层,前一百万个请求以及一定数量的计算时间是免费的。

Roughly translated to running a Twitter bot that posts for you, say, twice a day, your monthly cost for using Lambda would total… carry the one… nothing. I’ve been running my Lambda function for a couple weeks now, completely free.

粗略地解释为运行一个Twitter机器人,该机器人每天为您发布两次,您每月使用Lambda的费用总计……携带一个……什么也没有。 我已经完全免费地运行Lambda函数两周了。

When it recently came time for me to take the reigns of the @freeCodeCampTO Twitter, I decided to employ a similar strategy. I also used this opportunity to document the process for you, dear reader.

当我最近要接受@freeCodeCampTO Twitter的统治时,我决定采用类似的策略。 亲爱的读者,我也借此机会为您记录了过程。

So if you’re currently using a full-time running instance for a task that could be served by a cron job, this is the article for you. I’ll cover how to write your function for Lambda, and how to get it set up to run automatically. And, as a sweet little bonus, I’ll include a handy bash script that updates your function from the command line whenever you need to make a change. Let’s do it!

因此,如果您当前正在使用一个全时运行的实例来执行可由cron作业执行的任务,那么本文适合您。 我将介绍如何为Lambda编写函数,以及如何将其设置为自动运行。 而且,作为一点好处,我将包含一个方便的bash脚本,该脚本可在需要更改时从命令行更新您的功能。 我们开始做吧!

Lambda适合您吗? (Is Lambda right for you?)

When I wrote the code for my Twitter bot in Go, I intended to have it run on an AWS instance and I borrowed heavily from Francesc’s awesome Just for Func episode. Some time later, I modified it to randomly choose an article from my RSS feeds and to tweet the link, twice a day. I wanted to do something similar for the @freeCodeCampTO bot, and have it tweet an inspiring quote about programming every morning.

当我在Go中为Twitter机器人编写代码时,我打算让它在AWS实例上运行,并从Francesc出色的Just for Func剧集中大量借用了代码。 一段时间后,我对其进行了修改,以从RSS提要中随机选择一篇文章,并每天两次推文链接。 我想为@freeCodeCampTO机器人做类似的事情,并每天早上在推特上发一条激励人心的报价。

This is a good use case for Lambda because:

这对于Lambda是一个很好的用例,因为:

  • The program should execute once该程序应执行一次
  • It runs on a regular schedule, using time as a trigger它以时间为触发,定期运行
  • It doesn’t need to run constantly不需要持续运行

The important thing to keep in mind is that Lambda runs a function once in response to an event that you define. The most widely applicable trigger is a simple cron expression, but there are many other trigger events you can hook up. You can get an overview here.

要记住的重要一点是,Lambda会针对您定义的事件运行一次函数。 适用范围最广的触发器是一个简单的cron表达式,但是您还可以关联许多其他触发器事件。 您可以在此处获得概述。

编写Lambda函数 (Write a Lambda function)

I found this really straightforward to do in Go. First, grab the aws-lambda-go library:

我发现在Go中确实很简单。 首先,获取aws-lambda-go库:

go get github.com/aws/aws-lambda-go/lambda

Then make this your func main():

然后将其func main()

func main() { lambda.Start(tweetFeed)
}

where tweetFeed is the name of the function that makes everything happen. While I won’t go into writing the whole Twitter bot here, you can view my code on GitHub.

其中tweetFeed是使所有事情发生的函数的名称。 虽然我不会在这里编写整个Twitter机器人,但是您可以在GitHub上查看我的代码 。

设置AWS Lambda (Setting up AWS Lambda)

I’m assuming you already have an AWS account. If not, first things first here: https://aws.amazon.com/free

我假设您已经有一个AWS账户。 如果不是这样,那么首先要做的是: https : //aws.amazon.com/free

1.创建您的功能 (1. Create your function)

Find AWS Lambda in the list of services, then look for this shiny button:

在服务列表中找到AWS Lambda,然后寻找这个闪亮的按钮:

We’re going to author a function from scratch. Name your function, then under Runtime choose “Go 1.x”.

我们将从头开始编写一个函数。 命名您的函数,然后在运行时下选择“ Go 1.x”。

Under Role name write any name you like. It’s a required field, but irrelevant for this use case.

在“ 角色名称”下输入您喜欢的任何名称。 这是必填字段,但与该用例无关。

Click Create function.

单击创建功能。

2.配置您的功能 (2. Configure your function)

You’ll see a screen for configuring your new function. Under Handler enter the name of your Go program.

您会看到一个用于配置新功能的屏幕。 在处理程序下,输入Go程序的名称。

If you scroll down, you’ll see a spot to enter environment variables. This is a great place to enter the Twitter API tokens and secrets, using the variable names that your program expects. The AWS Lambda function will create the environment for you using the variables you provide here.

如果向下滚动,则会看到一个输入环境变量的位置。 这是使用程序期望的变量名输入Twitter API令牌和机密的好地方。 AWS Lambda函数将使用您在此处提供的变量为您创建环境。

No further settings are necessary for this use case. Click Save at the top of the page.

此用例无需其他设置。 点击页面顶部的保存

3.上传您的代码 (3. Upload your code)

You can upload your function code as a zip file on the configuration screen. Since we’re using Go, you’ll want to go build first, then zip the resulting executable before uploading that to Lambda.

您可以在配置屏幕上将功能代码作为zip文件上传。 由于我们正在使用Go,因此您需要go build ,然后压缩生成的可执行文件,然后再将其上传到Lambda。

…Of course I’m not going to do that manually every time I want to tweak my function. That’s what awscli and this bash script are for!

…当然,我每次想要调整功能时都不会手动执行此操作。 这就是awscli和此bash脚本的作用所在!

update.sh

update.sh

go build && \
zip fcc-tweet.zip fcc-tweet && \
rm fcc-tweet && \
aws lambda update-function-code --function-name fcc-tweet --zip-file fileb://fcc-tweet.zip && \
rm fcc-tweet.zip

Now whenever I make a tweak, I just run bash update.sh.

现在,只要进行调整,就可以运行bash update.sh

If you’re not already using AWS Command Line Interface, do pip install awscli and thank me later. Find instructions for getting set up and configured in a few minutes here under Quick Configuration.

如果您尚未使用AWS Command Line Interface ,请执行pip install awscli并稍后感谢我。 在此处的 “ 快速配置”下找到几分钟后即可进行设置和配置的说明。

4.测试你的功能 (4. Test your function)

Wanna see it go? Of course you do! Click “Configure test events” in the dropdown at the top.

想看吗? 当然可以! 点击顶部下拉菜单中的“配置测试事件”。

Since you’ll use a time-based trigger for this function, you don’t need to enter any code to define test events in the popup window. Simply write any name under Event name and empty the JSON in the field below. Then click Create.

由于您将为此功能使用基于时间的触发器,因此无需在弹出窗口中输入任何代码来定义测试事件。 只需在“ 事件名称”下写下任何名称,然后在下面的字段中清空JSON。 然后点击创建

Click Test at the top of the page, and if everything is working correctly you should see…

点击页面顶部的测试 ,如果一切正常,您应该会看到…

5.设置CloudWatch Events (5. Set up CloudWatch Events)

To run our function as we would a cron job — as a regularly scheduled time-based event — we’ll use CloudWatch. Click CloudWatch Events in the Designer sidebar.

为了像执行cron工作一样运行我们的功能(作为定期安排的基于时间的事件),我们将使用CloudWatch。 单击Designer侧栏中的CloudWatch Events

Under Configure triggers, you’ll create a new rule. Choose a descriptive name for your rule without spaces or punctuation, and ensure Schedule expression is selected. Then input the time you want your program to run as a rate expression, or cron expression.

配置触发器下 ,您将创建一个新规则。 为您的规则选择一个不带空格或标点符号的描述性名称,并确保选择了Schedule表达式 。 然后输入您希望程序以rate表达式或cron表达式运行的时间。

A cron expression looks like this: cron(0 12 * * ? *)

cron表达式如下所示: cron(0 12 * * ? *)

The items in the brackets represent, in order: minutes, hours, day of month, month, day of week, and year. In English, it says: Run at noon (UTC) every day.

括号中的项目按顺序表示:分钟,小时,一个月中的某天,一个月,一周中的某天和年份。 用英语说:每天中午(UTC)运行。

For more on how to write your cron expressions, read this.

有关如何编写cron表达式的更多信息,请阅读此内容。

To find out what the current time in UTC is, click here.

要了解UTC当前时间,请单击此处。

If you want your program to run twice a day, say once at 10am and again at 3pm, you’ll need to set two separate CloudWatch Events triggers and cron expression rules.

如果您希望程序每天运行两次,比如说上午10点一次,下午3点一次,那么您需要设置两个单独的CloudWatch Events触发器和cron表达式规则。

Click Add.

点击添加

看着它走 (Watch it go)

That’s all you need to get your Lambda function up and running! Now you can sit back, relax, and do more important things than share your RSS links on Twitter.

这就是启动和运行Lambda功能所需的全部! 现在,您可以高枕无忧,做些比在Twitter上共享RSS链接更重要的事情。

翻译自: https://www.freecodecamp.org/news/running-a-free-twitter-bot-on-aws-lambda-66160eb4de4/

多伦多到温莎

多伦多到温莎_我想要freeCodeCamp Toronto的Twitter来发布报价,所以我做了一个免费的bot来做到这一点。...相关推荐

  1. 构建一个react项目_您想要了解更多有关React的内容吗? 让我们构建一个游戏,然后玩。...

    构建一个react项目 by Samer Buna 通过Samer Buna 您想要了解更多有关React的内容吗? 让我们构建一个游戏,然后玩. (Do you want to learn more ...

  2. python中输出n开始的5个奇数_送你99道Python经典练习题,练完直接上手做项目,免费送了来拿吧...

    学python没练习题怎么行.今天,给大家准备一个项目: 99道编程练习,这些题如果能坚持每天至少完成一道,一定可以帮大家轻松 get Python 的编程技能.目前,这个项目已经获得了 2924 S ...

  3. 女友想让我带她吃大闸蟹,我用Python做了一个最全吃蟹攻略!拿去用吧!

    ​​​ 又到了吃大闸蟹的季节了,你吃了吗!那么为什么这么多人喜欢吃大闸蟹呢?大闸蟹有哪些特点,都有哪些大闸蟹品牌值得关注呢? 今天,就跟随着本文一看究竟吧!让你买到最好,最靠谱的螃蟹!这样吃起来才最美 ...

  4. 往返多伦多与温莎的交通全攻略!

    曾就读温莎大学的小编,在读书的两年间多次往返于多伦多与温莎两地,可算是把各种交通工具都熟悉了一遍.总的来说,往返两地的交通选择多样,也十分方便.尽管大家能通过各种途径得知这些信息,但为了给更多的将要到 ...

  5. 温莎大学的计算机专业,温莎大学的计算机专业

    温莎大学的计算机专业是一门专业的课程,为您的整个职业生涯奠定基础.该课程是以工业为导向的计算机应用课程的知识和技能.该课程将通过实习项目,可以让你不断增加软件行业知识和工作经验.计算机应用硕士专业的课 ...

  6. 温莎大学计算机硕士,温莎大学应用计算机硕士申请条件

    今天向大家重点推荐下温莎大学的应用计算机专业.提到温莎大学大家可能想到的是它的王牌专业在加拿大首屈一指的汽车工程专业.除了汽车工程专业之外,应用计算机专业每年申请人数众多,该专业是就业率非常而且非常受 ...

  7. 温莎大学的计算机科学,温莎大学 University of Windsor

    温莎大学 University of Windsor 建校时间:1857年 2 0 1 5排名:综合排名*十二 地理位置:安大略省温莎市位于加拿大*南端,美加边境 学校优势:出色的汽车工程专业.,以及 ...

  8. 温莎大学的计算机专业,加拿大留学,温莎大学英语计算机专业了解一下

    原标题:加拿大留学,温莎大学英语计算机专业了解一下 对于留学申请人来说,多年来计算机专业一直是热度不减.而在近些年,也有很多学生和家长更为看好加拿大的教学质量和未来的工作环境.加拿大在计算机科学领域的 ...

  9. 温莎大学的计算机科学,温莎大学荣誉计算机科学专业本科.pdf

    温莎大学荣誉计算机科学专业本科.pdf 留学监理服务网 温莎大学 荣誉计算机科学 - Honours Computer Science 基本信息 所属学 温莎大学 - University of 所在 ...

最新文章

  1. U盘快速​安装Ubuntu系统
  2. How to resolve warning message Access restriction -The type Resource is not accessible
  3. jmeter使用_jmeter工具的使用
  4. Linux设备驱动开发详解-Note(5)---Linux 内核及内核编程(1)
  5. (译)如何使用NSCoding和NSFileManager来保存你的应用程序数据
  6. re正则表达式7_{}
  7. java dbutil_Java:IO流,增删改查,配置文件,预处理后的DBUtils,含个人详细总结【诗书画唱】...
  8. Android抓包工具Fiddler的使用教程
  9. 【长期更新】Linux学习笔记
  10. multisim 仿真
  11. php千月影视,千月影视双端源码完美运营新手搭建教程
  12. PCI/PCIe接口卡Windows驱动程序(4)- 驱动程序代码(源文件)
  13. python numpy 矩阵运算_NumPy向量和矩阵的运算
  14. Jmeter分布式部署测试-----远程连接多台电脑做压力性能测试
  15. Kaggle数据竞赛入门-Titanic生存预测
  16. Http 协议学习笔记 (燕十三老师)
  17. 正则表达式,兼容正整数或负整数的写法
  18. anaconda python降级有影响吗_anaconda更新所有降级包
  19. 在C语言中如何产生随机数
  20. 如何从App Store排行榜中脱颖而出

热门文章

  1. 用Java中的HashSet写一个学生管理系统(添加、删除、修改、查询学生信息)
  2. python自动化ppt_python自动化怎么操作ppt?
  3. 1小时学会:最简单的iOS直播推流(八)h264/aac 软编码
  4. 前端开发学习Day27
  5. 核方法---径向基函数网络
  6. python 爬虫气象气象定时报 气象预警推送
  7. 嘿,程序员,你该学点经济学了!
  8. aligned_storage简单学习
  9. backup restore On Ubuntu
  10. 匹夫细说C#:委托的简化语法,聊聊匿名方法和闭包