Created
June 8, 2023 03:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | |
}), | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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