Skip to content

Instantly share code, notes, and snippets.

@tacck
Created June 8, 2023 03:08
Show Gist options
  • Save tacck/655149f5ed861644ae67fae38c93ea1e to your computer and use it in GitHub Desktop.
Save tacck/655149f5ed861644ae67fae38c93ea1e to your computer and use it in GitHub Desktop.
AWS CDK sample for Post to Slack in Step Functions using API Gateway
import { PostSlackApiGwStack } from "./PostSlackApiGwStack";
...
const postSlackGwStack = new PostSlackGwStack(scope, id, props);
const postSlackGateway = postSlackGwStack.getApiGateway();
const SLACK_APP_TOKEN = "xoxb-XXXXXXXXXX";
const collPostSlackApiGatewayInputDefinition =
new tasks.CallApiGatewayRestApiEndpoint(
scope,
"collPostSlackApiGatewayInputDefinition",
{
api: postSlackGateway,
stageName: "prod",
method: tasks.HttpMethod.POST,
authType: tasks.AuthType.IAM_ROLE,
headers: stepfunctions.TaskInput.fromObject({
"x-Authorization": [`Bearer ${SLACK_APP_TOKEN}`],
}),
requestBody: stepfunctions.TaskInput.fromObject({
channel: "#YOUR_CHANNEL",
"text.$": "$.message",
}),
}
);
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as apigateway from "aws-cdk-lib/aws-apigateway";
export class PostSlackApiGwStack {
private postSlackApiGateway;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
////////// API Gateway //////////
this.postSlackApiGateway = new apigateway.RestApi(
scope,
"postSlackApiGateway",
{}
);
this.postSlackApiGateway.root.addMethod(
"POST",
new apigateway.HttpIntegration("https://slack.com/api/chat.postMessage", {
proxy: false,
httpMethod: "POST",
options: {
requestParameters: {
"integration.request.header.Authorization":
"method.request.header.x-Authorization",
},
integrationResponses: [
{
statusCode: "200",
},
],
},
}),
{
authorizationType: apigateway.AuthorizationType.IAM,
requestParameters: {
"method.request.header.x-Authorization": true,
},
methodResponses: [
{
statusCode: "200",
},
],
}
);
}
getApiGateway() {
return this.postSlackApiGateway;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment