Slack & Rundeck Integration Using AWS Services
Overview
High-Level Design
- Slash commands created via a custom Slack application point to a URL hosted on the AWS API Gateway
- AWS API Gateway resource calling an AWS Lambda function.
- Lambda function invoking a Rundeck job via Rundeck API.
- KMS & VPC NAT Gateway are required for security purposes.
Steps
Rundeck API Setup
- Login to Rundeck and generate an API token (permissions permitting) from the Profile option.
- Test the API token using something like Chrome Postman Rest Client.
- Here is an example to test the job execution API.
- Retrieve a job UUID for the Rundeck job that you want to execute. You can obtain this from the Job Definition.
- Construct an HTTPS POST command as in the example below.
- Rundeck is not accessible via the URL (firewall, private URL)
- The token is not valid
- Token permissions are not sufficient to perform the API request.
Creating AWS Lambda Function
- Login to AWS Console and go to Lambda service
var https = require('https');
/*
* We will secure this later.
* It is not good practice to have api token in the clear
*/
let rundeck_auth_token = 'your-token';
function exec_rundeck_job(jobid) {
return new Promise(function(resolve, reject) {
if (jobid === "")
{
console.log("error : no jobid");
reject("No rundeck job defined for request. Contact support!");
return;
}
console.log("executing jobid " + jobid);
var post_options = {
host: 'your-rundeck-url',
port: 'your-rundeck-port',
path: '/api/1/job/' + jobid + '/executions',
method: 'POST',
headers: {
'X-Rundeck-Auth-Token': rundeck_auth_token
}
};
// Set up the request
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf8');
var body = [];
res.on('data', function(chunk) {
console.log('Response data: ' + chunk);
body.push(chunk);
});
res.on('end', function() {
console.log('Response end : ' + body);
resolve("Response received");
});
});
post_req.on('error', error => {
reject(error);
});
// post the data
post_req.write(JSON.stringify({}));
post_req.end();
});
}
exports.handler = async(event) => {
var user_message = "";
var response = "";
var error = false;
var msg = "";
await exec_rundeck_job("your-job-id").then(function() {
user_message = "Tada!";
error = false;
})
.catch(function(body) {
error = true;
user_message = body;
});
msg = {
text: user_message
};
response = {
statusCode: (error ? 500 : 200),
body: JSON.stringify(msg)
};
return response;
};
Use Test to make sure that the lambda function is working as expected. We are going to be building on this code, so you need to make sure that it is correct.
Securing API Token via AWS KMS
Now test the function after all of these steps are completed.
Securing Lambda Function via AWS NAT Gateway
- Create a new VPC for Lambda functions
- Create a NAT within this VPC
- Create public/private subnets
- Assign an Elastic IP to the public subnet
- Deploy Lambda function in the VPC
- Whitelist the Elastic IP to access Rundeck.
Creating API Gateway Resource
Slack Slash commands essential invoke an HTTP POST on a server. AWS allows us to use the API Gateway to call the Lambda function that we created. This is the simplest method without setting up a server to receive the message and then invoke the lambda function.
- Log in to AWS Account
- Create API
- Create a Resource
- Create Method (POST)
- Configure the Integration Request to LAMBDA_PROXY and provide details as requested. (Lambda function region/name)
- Use the Test option to validate the method.
- If you have created the Lambda function as indicated in the earlier section of this tutorial then you should see that your Lambda function is called, and the rundeck job executed.
- Deploy API
- Use the Deploy API option under the Actions banner.
- You will need to specify a Deployment Stage.
- API Gateway will publish a URL for the API deployed. Make a note of this - it will be needed later when configuring the Slack Slash command.
- Log in to AWS Account
- Create API
- Create a Resource
- Create Method (POST)
- Configure the Integration Request to LAMBDA_PROXY and provide details as requested. (Lambda function region/name)
- Use the Test option to validate the method.
- If you have created the Lambda function as indicated in the earlier section of this tutorial then you should see that your Lambda function is called, and the rundeck job executed.
- Deploy API
- Use the Deploy API option under the Actions banner.
- You will need to specify a Deployment Stage.
- API Gateway will publish a URL for the API deployed. Make a note of this - it will be needed later when configuring the Slack Slash command.
Creating Slash Commands
Debugging
Improvements
- Check token sent from Slack to validate that it is your Slack command that is calling the Lambda function
- Extract commands and parameters to execute the correct rundeck job.
Comments
Post a Comment