|
| 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). |
0 commit comments