I recently setup AWS CodePipeline and CodeBuild to perform continuous integration and testing. The piece that was missing out of the box was build notifications. I want to know if my build passes or fails, and if it fails what the errors were. I was able to throw together a quick solution using AWS CloudWatch Events, Lambda, and Slack. Here’s how it works… CloudWatch Events trigger a Lambda for all CodeBuild phases. The Lambda POST’s a message to a Slack web hook. I get the Slack message… Live is good. I used CloudFormation to define and deploy the stack. Lambda Permissions & Function _## Role that our Lambda will assume to provide access to other AWS resources _ AWS::IAM::Role '2012-10-17' - Allow - lambda.amazonaws.com - sts:AssumeRole '/' IamRoleLambdaExecution:Type: Properties:AssumeRolePolicyDocument:Version: Statement: Effect: Principal:Service: Action: Path: _## Create a Policy and attach it to our Lambda Role. _ AWS::IAM::Policy IamRoleLambdaExecution IamPolicyLambdaExecution '2012-10-17' - Allow - logs:* '*' - IamRoleLambdaExecution IamPolicyLambdaExecution:Type: DependsOn: Properties:PolicyName: PolicyDocument:Version: Statement: Effect: Action: Resource: Roles: Ref: AWS::Lambda::Function slack.handler 5 - IamRoleLambdaExecution- Arn <your s3 bucket> 'slack.js.zip' nodejs6.10 <your slack url> ## Lambda Function# SlackFunction:Type: Properties:Handler: Timeout: Role:Fn::GetAtt: Code:S3Bucket: S3Key: Runtime: Environment:Variables:SLACK_HOOK_URL: Upload the lambda function below to an S3 bucket as a zip file. Replace the bucket path and slack hook url in the CloudFormation snippet above. CloudWatch Events _## CloudWatch Event to trigger lambda for build slack notifications. _ 'AWS::Events::Rule' 'BuildEventRule' - 'aws.codebuild' - 'CodeBuild Build State Change' - 'IN_PROGRESS'- 'SUCCEEDED'- 'FAILED'- 'STOPPED' 'ENABLED' - !GetAtt SlackFunction.Arn 'BuildRuleLambdaTarget' BuildEventRule:Type: Properties:Description: EventPattern:source: detail-type: detail:build-status: State: Targets: Arn: Id: 'AWS::Lambda::Permission' !Ref SlackFunction 'lambda:InvokeFunction' 'events.amazonaws.com' !GetAtt BuildEventRule.Arn ## Permission for CloudWatch to invoke our Lambda# PermissionForBuildEventsToInvokeLambda:Type: Properties:FunctionName: Action: Principal: SourceArn: Now our Lambda will be invoked when CodeBuild changes state. Lambda Code That’s it! If you want to limit your notifications to a specific CodeBuild instance you can add that to the EventPattern using the . For example: project-name - 'aws.codebuild' - 'CodeBuild Build State Change' - '<your CodeBuild name>' - 'IN_PROGRESS'- 'SUCCEEDED'- 'FAILED'- 'STOPPED' EventPattern:source: detail-type: detail:project-name: build-status: