Slack Commands & AWS Serverless

The idea of this simple utility is ties a Slack commnad to AWS Serverless. This narrow scoped example integrates Slack and AWS Serverless. This touches on key areas and hopefully provides insights on how these technologies could be combined to add meaningful value to the team (and business)

High Level Architecture

Note this is Java centric

Setting up a Spring Boot based AWS Serverless

With Maven is available execute the following command

mvn archetype:generate -DgroupId=com.awsserverless -DartifactId=slack -Dversion=1.0-SNAPSHOT \
-DarchetypeGroupId=com.amazonaws.serverless.archetypes \
-DarchetypeArtifactId=aws-serverless-springboot2-archetype \
-DarchetypeVersion=1.3.2

The above archetype sets up a new aws serverless maven project. Apom.xml is generated that includes the dependencies needed to build a basic Spring Boot API. The application is based on the spring-boot-starter-web. The generated code includes a StreamLambdaHandler class, the main entry point for AWS Lambda; an Application class that defines a basic Spring Boot application; Most importantly, the project also includes sam.yam. This is a SAM template that you can be used to test application in local or deploy it to AWS.

Writing Endpoint to manage request from Slack

Create an endpoint. Few things of interest. X-Slack-Request-Timestamp, X-Slack-Signature and requestBody. These are sent from Slack; otherwise everything is typical

@RestController
public class SlackController {
private static final Logger l =
LoggerFactory.getLogger(SlackController.class);

Validate the Slack Request

The following piece of code verifies that the Slack request is infact legitimate; note in case of AWS Gateway (serverless), the request body ordering change is changed; as a result the verification will fail; so this needs to be reassembled into right order to get it properly verified

import io.vavr.collection.List;
import io.vavr.control.Try;

Create SAM

With code written up, next step is to create SAM (Serverless Application Model); leaving out param and output

Resources:
CpDeployFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.hyla.deploy.StreamLambdaHandler::handleRequest
Runtime: java8
CodeUri: target/util-1.0-SNAPSHOT-lambda-package.zip
MemorySize: 512
Policies:
- AWSLambdaBasicExecutionRole
Timeout: 30
Events:
GetResource:
Type: Api
Properties:
Path: /{proxy+}
Method: post

Outputs:
CpDeployApi:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/ping'
Export:
Name: CpDeployApi

Deploy

mvn clean package
aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket [[bucket]]
aws cloudformation deploy --template-file output-sam.yaml --stack-name [[Stack Name]]

Setting up Slack

From https://api.slack.com/ log into your apps and set up slash command. The request URL is the API Gateway output

set up a slash command

Testing it from Slack

From a slack channel, execute the command to get the output

output from serverless lambda

Closing

Hope this high level flyby of setting up slack and aws serverless was useful. If there are any specific questions on this topic feel free send me an email

Thank you reading.

kviswanath@hylamobile.com