Setup alerts/notifications to Slack channel when a new Amazon Linux 2 AMI is available

0

How do I setup alerts/notifications when a new Amazon Linux 2 AMI is available

There is a SSM parameter to query:

aws ssm get-parameters --names /aws/service/ecs/optimized-ami/amazon-linux-2/recommended

notification for a new AMI to be sent to a slack channel

I would very much appreciate it if someone could give me the steps as I struggled online.

Many thanks in advance Del

3개 답변
1

New AMI updates are published to the SNS topic arn:aws:sns:us-east-1:137112412989:amazon-linux-2022-ami-updates

From this article you can use the sample Lambda code (in the second example for Slack). Following the steps, under the last section, "Add an SNS topic trigger to your Lambda function", for step 4 use the above SNS topic instead.

Steps cross-posted here:

Example Python code snippet for Slack

Slack Incoming Webhooks expect a JSON request with a message string corresponding to a "text" key. They also support message customization, such as adding a user name and icon, or overriding the webhook's default channel. For more information, see Sending messages using incoming webhooks on the Slack website.

Note: In this example function code for Slack Incoming Webhooks, replace https://hooks.slack.com/services/xxxxxxx with the Incoming Webhook URL. Also replace #CHANNEL_NAME with the destination channel's name.

#!/usr/bin/python3.6
import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
    url = "https://hooks.slack.com/services/xxxxxxx"
    msg = {
        "channel": "#CHANNEL_NAME",
        "username": "WEBHOOK_USERNAME",
        "text": event['Records'][0]['Sns']['Message'],
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    print({
        "message": event['Records'][0]['Sns']['Message'], 
        "status_code": resp.status, 
        "response": resp.data
    })

Test the Lambda function

  1. On the Functions page of the Lambda console, choose your function.
  2. At the top right, choose Select a test event. Then, choose Configure test events.
  3. In the Configure test event dialog box, choose Create new test event.
  4. For Event template, choose Amazon SNS Topic Notification.
  5. For Event name, enter a name for the test event.
  6. Choose Create.
  7. Choose Test.
  8. Review the Execution result.

If the test invocation succeeds with a 200 status code, then the Amazon SNS notification message is accepted by your webhook, which delivers it to the corresponding channel. If the invocation fails with a 4xx status code, then check the webhook URL and verify that the key-value pair is correct and accepted by your destination webhook.

For more information about testing functions in the Lambda console, see Invoke the Lambda function.

Add an SNS topic trigger to your Lambda function

After sending an SNS message to your webhook as a test in the Lambda console, subscribe your function to your SNS topic. To configure this from the Lambda console, add an SNS topic trigger by doing the following:

  1. On the Functions page of the Lambda console, choose your function.
  2. Under Designer, choose Add trigger. For more information, see Use the designer.
  3. Under Trigger configuration, choose Select a trigger, and then choose SNS.
  4. For SNS topic, choose the SNS topic that you created earlier arn:aws:sns:us-east-1:137112412989:amazon-linux-2022-ami-updates
  5. Select the Enable trigger check box.
  6. Choose Add.
profile pictureAWS
답변함 2년 전
0

You can set up notifications or trigger actions based on Parameter Store events using EventBridge and SNS

See https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-cwe.html

You can then integrate this with Slack using a Chatbot or Webhooks

profile pictureAWS
전문가
Matt-B
답변함 2년 전
0

Hi Aaron_D

Thanks very much for your time and for this valuable info.

Lambda function test passed successfully and could send a message to a slack channel. However, I am having issues with SNS topic.

From this link https://docs.aws.amazon.com/linux/al2022/ug/receive-update-notification.html we are here dealing with Lambda not email. I can add a subscription. However, I cannot directly add a topic by using this ARN:

Arn:aws:sns:us-east-1:137112412989:amazon-linux-2022-ami-updates

Because otherwise it will create an ARN specific to the topic.

However I can only create a subscription by adding this ARN:

Topic ARN: arn:aws:sns:us-east-1:137112412989:amazon-linux-2022-ami-updates

Protocol: AWS Lambda

Endpoint: Lambda ARN of the Lambda function I created

When adding a trigger to Lambda it does not allow me to add a topic even though I have full permissions. It does not show in the drop down list.

Many thanks

Del

답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠