Skip to content

Commit edc6682

Browse files
committed
Add an AWS SQS Lambda example
This adds a very simple example that posts to a Slack channel for every SQS message, using an AWS Lambda. New in @pulumi/aws 0.14.2!
1 parent d83d7c8 commit edc6682

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

aws-js-sqs-slack/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/bin/
2+
/node_modules/
3+
/.pulumi/

aws-js-sqs-slack/Pulumi.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: aws-js-sqs-slack
2+
description: Post to Slack for each SQS message!
3+
runtime: nodejs

aws-js-sqs-slack/README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Post AWS SQS Messages to Slack using Serverless Lambdas
2+
3+
This example wires up a serverless AWS Lambda to an AWS SQS queue and demonstrates posting a
4+
message to Slack. This program provisions resources using Pulumi's deployment system, but lets
5+
you write serverless code as ordinary JavaScript functions.
6+
7+
## Prerequisites
8+
9+
This program requires the Pulumi CLI. If you don't have it installed already,
10+
[get it here](https://pulumi.io/install) or simply run `curl -fsSL https://get.pulumi.com | sh`.
11+
12+
After that, you'll need to [configure your AWS credentials](https://pulumi.io/install/aws.html) so that Pulumi can
13+
deploy into your account. If your AWS CLI is already configured, everything should just work.
14+
15+
Since this example uses Slack, you'll also need
16+
[an access token](https://get.slack.help/hc/en-us/articles/215770388-Create-and-regenerate-API-tokens).
17+
18+
## Running the Program
19+
20+
After installing the CLI and cloning the repo, `cd` into the directory, and run these commands:
21+
22+
1. Install NPM modules using `npm install` (or `yarn install` if you prefer Yarn).
23+
24+
2. Create a new stack:
25+
26+
```
27+
$ pulumi stack init sqs-slack-dev
28+
```
29+
30+
3. Configure the required variables:
31+
32+
```
33+
# Set the AWS region to deploy into:
34+
$ pulumi config set aws:region us-west-2
35+
# Configure the Slack channel and access token to use:
36+
$ pulumi config set slackChannel "#general"
37+
$ pulumi config set slackToken xoxb-123456789012-Xw937qtWSXJss1lFaKeqFAKE --secret
38+
```
39+
40+
4. Deploy your program to AWS using the `pulumi update` command:
41+
42+
```
43+
$ pulumi update
44+
```
45+
46+
This command will show you the changes before it makes them. As soon as you select `yes`, it will begin
47+
provisioning resources, uploading your lambda, etc. After it completes, your program is live!
48+
49+
5. To test this out, push a message into your SQS queue using the AWS CLI:
50+
51+
```
52+
$ aws sqs send-message --queue-url $(pulumi stack output queueURL) --message-body "Pulumi+AWS rocks :boom:"
53+
```
54+
55+
If you've done everything right, you'll see a message posted to your Slack channel!
56+
57+
<img src="./sqs_slack.png" />
58+
59+
Notice we've used the `pulumi stack output` command to read the SQS queue URL that was provisioned.
60+
61+
6. Run the `pulumi logs --follow` command to follow the logs. After a short while, you should see `console.log`
62+
output that your message was posted to Slack.
63+
64+
```
65+
$ pulumi logs --follow
66+
2018-07-05T16:46:03.708-07:00[mySlackPoster-queue-subscripti] 2018-07-05T23:46:03.708Z 68b50931-a005-5e85-b5c4-5a890fee5519 Posted SQS message 3caa4069-f549-44d7-8534-6d61840d3420 to Slack channel #general
67+
```
68+
69+
7. If you'd like to make some edits, try changing the `index.js` file, and then just run `pulumi update` again.
70+
Pulumi will detect the minimal set of edits needed to deploy your code.
71+
72+
8. After you're done playing around, you can destroy your program and stack by simply running two commands:
73+
74+
```
75+
$ pulumi destroy --yes
76+
$ pulumi stack rm --yes
77+
```
78+
79+
## Learning More
80+
81+
To learn more about Pulumi, try checking out the [Tutorials](https://pulumi.io/quickstart) and
82+
[Tour](https://pulumi.io/tour) sections of [pulumi.io](https://pulumi.io).

aws-js-sqs-slack/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
let pulumi = require("@pulumi/pulumi");
2+
let aws = require("@pulumi/aws");
3+
let serverless = require("@pulumi/aws-serverless");
4+
5+
let config = new pulumi.Config(pulumi.getProject());
6+
let slackChannel = config.get("slackChannel") || "#general";
7+
let slackToken = config.require("slackToken");
8+
9+
let queue = new aws.sqs.Queue("mySlackQueue", { visibilityTimeoutSeconds: 180 });
10+
11+
serverless.queue.subscribe("mySlackPoster", queue, async (e) => {
12+
let slack = require("@slack/client");
13+
let client = new slack.WebClient(slackToken);
14+
for (let rec of e.Records) {
15+
await client.chat.postMessage({
16+
channel: slackChannel,
17+
text: `*SQS message ${rec.messageId}*:\n${rec.body}\n`+
18+
`(with :love_letter: from Pulumi)`,
19+
as_user: true,
20+
});
21+
console.log(`Posted SQS message ${rec.messageId} to Slack channel ${slackChannel}`);
22+
}
23+
}, { batchSize: 1 });
24+
25+
module.exports = {
26+
queueURL: queue.id,
27+
};

aws-js-sqs-slack/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "aws-js-sqs-slack",
3+
"dependencies": {
4+
"@pulumi/aws": "latest",
5+
"@pulumi/aws-serverless": "latest",
6+
"@pulumi/pulumi": "latest",
7+
"@slack/client": "^4.3.1"
8+
}
9+
}

aws-js-sqs-slack/sqs_slack.png

44 KB
Loading

0 commit comments

Comments
 (0)