1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lambdaでメッセージを編集してSNS Publishする一例

Posted at

Lambdaのコード

 ヒアドキュメントと変数、json.dumpsを利用した一例です。
 SecurityHubの通知を例にして作成しています。

lambda_function
from __future__ import print_function
import json
import boto3
import os

sns = boto3.client("sns")
def lambda_handler(event, context):
    message = event["detail"]["findings"][0]
    msg = """
    Security Hubにより検知した違反内容
    セキュリティ基準コントロール:{standards_control_arn}
    タイトル:{title}
    説明:{description}
    セキュリティステータス:{status}
    重要度:{severity}
    修復手順:{remediation_text}({remedeation_url})
    
    対象リソース
    {resources}
    
    詳細
    --
    {detail}
    """.format(standards_control_arn=message["ProductFields"]["StandardsControlArn"], \
    title=message["Title"],\
    description=message["Description"], \
    status=message["Compliance"]["Status"], \
    severity=message["Severity"]["Label"],\
    remediation_text=message["Remediation"]["Recommendation"]["Text"], \
    remedeation_url=message["Remediation"]["Recommendation"]["Url"],\
    resources=json.dumps(message["Resources"], indent=4), \
    detail=json.dumps(event, indent=4))
    
    response = sns.publish(
        TopicArn=os.environ.get("sns_arn"),
        Subject="テスト",
        Message=msg
    )
    return response
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?