Sending notifications from CodeCatalyst Workflows in March 2023
Tags: codecatalyst, devops, aws
Published: 2023-03-31 12:19:29

---
title: Sending notifications from CodeCatalyst Workflows in March 2023
published: true
date: 2023-03-31 12:18:51 UTC
tags: CodeCatalyst,DevOps,aws,codecatalyst
canonical_url: https://lockhead.info/index.php/2023/03/31/sending-notifications-from-codecatalyst-workflows-in-march-2023/
---

As Amazon CodeCatalyst is still in Preview, it has only limited integration possibilities with other AWS services or external tools.  
Sending notifications from a Workflow execution is something that I believe is critical for a CI/CD system – and as I focus on CI/CD at the moment I’ll focus on the notifications on Workflows in this article.

## What kind of notifications do I need or expect?

As a user of a CI/CD and Workflow tool there are different levels of notifications that I would like to receive:

1. Start / End and Status of Workflow execution
2. State / Stage transitions (for longer running workflows)
3. Approvals (if required)

In addition to that, based on the context of the notification I would like to get context-specific information:

a) For the “Start” event I would like to know who or which trigger started the workflow, which branch and version it is running on, which project and workflow has been triggered. If possible getting the expected execution time / finish time would be good  
b) For the “End” event I would like to know how long the execution took and if it was successful or not. I would also like to know if artifacts have been created or if deployments have been done. If the “End” is because of a failure, I would love to know the failure reason (e.g. tests failed, deployment failed, …)  
c) For the state transitions I’d love to know the “time since started” and “expected completion time”. I would also like to, obviously, know the state that has been completed and the one that will now be started.  
d) For approvals I’d love to be able to get the information about the approval ask and all required information (commit Id, branch) to do the approval

## What does CodeCatalyst Support today?

Right now CodeCatalyst allows to set up notifications to Slack.   
Please see details on how to set this up [here](https://docs.aws.amazon.com/codecatalyst/latest/userguide/notifications.html).  
This notifications are also minimal right now:

![](https://lockhead.info/wp-content/uploads/2023/03/image.png)

In Slack this looks like this:

![](https://lockhead.info/wp-content/uploads/2023/03/image-1.png)

## How can I enhance the notification possibilities?

Luckily one of the “core actions” is the possibility to trigger a Lambda function and this is what we are going to use here to be able to trigger advanced notifications using [Amazon SNS](https://aws.amazon.com/sns/).   
In our example we are going to use this to send an eMail to a specific address, but you can also use any other [destinations](https://docs.aws.amazon.com/sns/latest/dg/sns-event-destinations.html) supported by SNS like SMS or [AWS ChatBot](https://docs.aws.amazon.com/chatbot/latest/adminguide/related-services.html).

### Setting up pre-requisites

Unfortunately we will need to set up an SNS topic and a Lambda function in a dedicated AWS account in order to use these advanced notifications.   
This means that we are “breaking” the concept of CodeCatalyst not requiring access to the AWS Console, but this is the only way that I found so far to be able to send additional notifications.

Ideally we would be setting up the SNS topic and the lambda function using CDK, but that increases the complexity of the workflow and of the setup and because of that I’m not including that in this blog post.

#### Setting up the SNS topic

Please create a SNS topic following the [AWS documentation](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html) through the console.  
We assume the topic to be in “_eu-central-1_” and the name to be “_codecatalyst-workflow-topic_“.

After the topic has been set up, you will need to [subscribe your eMail address](https://docs.aws.amazon.com/sns/latest/dg/sns-email-notifications.html) to it.

#### Setting up the lambda function

You can follow [this blog post](https://dev.to/aws-builders/cook-a-recipe-with-aws-simple-and-easy-lambda-functions-3e98) to manually set up the lambda function through the AWS console, please ensure to give the Lambda functions permissions to use the SNS topic.  
The required code using Python will look like this:

```
import boto3

sns = boto3.client('sns')

def lambda_handler(event, context):
    try:
        message = event['message']
        topic_arn = 'arn:aws:sns:eu-central-1:<accountID>:codecatalyst-workflow-topic'

        response = sns.publish(
            TopicArn=topic_arn,
            Message=message
        )
        print('Message sent to SNS topic:', response['MessageId'])
    except Exception as e:
        print('Error sending message: ', e)

```

Obviously the same can be achieved using Typscript, Go or any other supported function.  
Please adjust the _topic\_arn_ to match the topic that you just created.  
After creation this Lambda function will now have an ARN which should look similar to this:  
_arn:aws:lambda:eu-central-1:<accountId>:function:send-sns-notification-python_

We will need this ARN when setting up the notification in our Workflow.

### Integration into the workflow

Integrating this Lambda function into a workflow is easy:

```
  NotifyMe:
    Identifier: aws/lambda-invoke@v1
    Environment:
      Connections:
        - Role: CodeCatalystPreviewDevelopmentAdministrator-wzkn0l
          Name: "<connection>"
      Name: development
    Inputs:
      Sources:
        - WorkflowSource
    Compute:
      Type: Lambda
    Configuration:
      RequestPayload: '{"message":"branchName: ${WorkflowSource.BranchName}\nCommitID: ${WorkflowSource.CommitId}\nWorkflow-Name: NOT-AVAILABLE\nSTATUS: EXECUTED"}'
      ContinueOnError: false
      AWSRegion: eu-central-1
      LogType: Tail
      Function: arn:aws:lambda:eu-central-1:<accoutId>:function:send-sns-notification-python

```

As you can see, we are integrating an “aws/lambda-invoke@v1” action which then points to the lambda function that we just created.

In the “RequestPayload” we are passing a few information to the Lambda function which will then be passed to the SNS topic as part of the message.  
This is how the message will look when received as eMail:

![](https://lockhead.info/wp-content/uploads/2023/03/image-2.png)

## Missing information and next steps for enhanced notifications

As you can see we are able to send notifications from CodeCatalyst to multiple targets, including eMail with this option.

What we are missing is – and I am not sure if thats possible or not – is all of the “metadata” of the workflow execution like:

- Workflow-Name
- State-Name
- Project Name and additional information

In the documentation I was not able to find out the available environment variables about these information…. If you do have any ideas on how to access this metadata, please let me know!

Hits: 0