Package: aws-mail
AWS mail clientThe tool is written to send everything piped into it fromstdinas an email, considering the settings found inconfig.yml.As this client uses theboto3library to send emails through AWS SES, it can be used as replacement forsendmailor any other mailer client on AWS instances - I could not make thepostfixconfiguration work at all for that scenario.Please also see the below:Configure tools relying on /usr/sbin/sendmail.The AWS EC2 instance should have an instance role attached, which allows sending emails through AWS SES.By default it just sends everything fromstdin.
Also by default,The tool recognizes certain tools' log structure and will leave everything not part of it. This works for:logwatchunattended-upgradecronNote:aws_mailignores unknown cli arguments, e.g. the ones usually sent tosendmail.Quick start# Install.
python3 -mvenv venv
venv/bin/pip install aws_mail

# Configuration.
mkdir /etc/aws_mail/
touch /etc/aws_mail/config.yml
# Copy fromthe example 'config.yml' into '/etc/aws_mail/config.yml'.
# Adapt the configuration options.

# Use.
echo -e "subject: update\nto: email@address.com\n\nGood day today." | venv/bin/aws_mail --region us-east-1InstallationThis tool is available on pypi.
It is best to install it into a virtual environment:python3 -m venv venv
venv/bin/pip install aws_mailInstaalled this way you will find an executable calledaws_mail.
Its location depends on the way you installed the tool - I recommend to work with absolute paths when referring to theaws_maillocation.Note:The default location for the configuration file is/etc/aws_mail/config.yml.The default log path is/var/log/aws_mail.Make sure those directories/files exist.UsageIn generalaws_mailworks like this:echo "Good day today." | venv/bin/aws_mail --region us-east-1The above reads the configuration from/etc/aws_mail/config.ymland logs into/var/log/aws_mail/aws_mail.log.Configurationoptiondescriptiondefaultconfiguration options--log-pathPath to the log file directory./var/log/aws_mailAbsolute, relative path and/or symlink.--configPath to the configuration file./etc/aws_mail/config.ymlAbsolute, relative path and/or symlink.--regionAWS region to use.us-east-1Can also be set in the configuration file.--default-subjectForces subject to be loaded from the configuration file <\br> Otherwise considers a line starting withSubject:.False(not set)Default value is the name of the event in the configuration file.--default-recipientsForces recipients to be loaded from the configuration file <\br> Otherwise considers a line starting withTo:.False(not set)Default value is set in configuration file.--log-levelSets log level.ERRORlog_levelasstr.Examples:Set a different log level:echo"Some message."|venv/bin/aws_mail--log-levelINFOConfiguration fileEmail settings need to be configured inconfig.yml.
This configures anAwsSesEmailHookevent from theeventhooksmodule.Since the default path to the configuration file is/etc/aws_mail/config.yml, you need to make sure both, path and file, exist.
However, you can also give another configuration file location by passing:--config /path/to/config.ymlExample configuration file:log_level: "ERROR"
events:
  example_mail_event_name:  # <-- Name of the event, used as default subject.
    enabled: true  # <-- Everything but 'true' disables this event.
    type: AwsSesEmailHook  # <-- Type of hook to use.
    sender: "{{ sender_address }}"  # <-- Change that to your sender address.
    sender_name: "{{ sender_name }}"  # <-- Change that to your sender name.
    region: "{{ aws_region }}"  # <-- AWS region the AWS SES endpoint is listening on.
    recipients:  # <-- Change that if you like or configure the tool properly, see below.
      - "{{ recipient_address}}"If set, the cli option--log-leveloverwrites the configurationlog_levelread from the configuration file.Email subjectIf the tool should find a line starting withsubject:(piped into it), this will be used as email subject.# The subject will be 'Daily logs from server1'.
    echo -e "subject: Daily logs from server1\nGood day today." | venv/bin/aws_mailOtherwise or when forced with--default-subject, the event name (key inconfig.yml) is used as email subject.# If event in 'config.yml' is called 'example_mail_event_name'
    events:
      example_mail_event_name:
      ...
    # the actual subject will be 'example_mail_event_name':
    echo -e "subject: Daily logs from server1\nGood day today." | venv/bin/aws_mail --default-subjectEmail recipientsIf the tool should find a line starting withto:(piped into it), this will be used as email recipients.# The recipients will be 'email@address.com,another@address.com'.
    echo -e "to: email@address.com,another@address.com\nGood day today." | venv/bin/aws_mailOtherwise or when forced with--default-recipients, the recipients defined inconfig.ymlare used as email recipients.# If the recipients in 'config.yml' are defined as:
    events:
      example_mail_event_name:
        recipients:
          - "email@address.com"
          - "another@address.com"
        ...
    # the actual recipients will be 'email@address.com,another@address.com':
    echo -e "to: some@address.com\nGood day today." | aws_client. --default-recipientsAWS regionTo make sure theboto3client is initialized with the correct AWS SES region, add it to yourconfig.yml.Another option is to directly pass the parameter to the tool:--region us-east-1Configure logwatchWhenlogwatchis installed, there will also be a daily cronjob by default, created in/etc/cron.daily/00logwatchon debian or/etc/cron.daily/0logwatchon RHEL/CentOS. Make sure the cronjob is configured with--output mail.Ondebian/Ubuntu:There are 2 locations:/usr/share/logwatch/default.conf/logwatch.confand/usr/share/logwatch/dist.conf/logwatch.conf./usr/share/logwatch/dist.conf/logwatch.confis read after/usr/share/logwatch/default.conf/logwatch.conf.OnAmazonLinux/RHEL/CentOS:There is only/usr/share/logwatch/default.conf/logwatch.conf.Configureaws_mail.pyas email client application in the appropriate file (depending on yourconfig.yml):/usr/share/logwatch/dist.conf/logwatch.confondebian/Ubuntu./usr/share/logwatch/default.conf/logwatch.confonAmazonLinux/RHEL/CentOS.MailTo = <email@address.com>
mailer = "/complete/path/to/venv/bin/aws_mail --region us-east-1"Note:It is also possible toleavesendmailasmailerclient and just create a symlink toaws_mail.pyas described below.aws_mail.pyignores unknown cli arguments, the ones usually sent tosendmail.If you like to just use the recipients defined withinconfig.yml, add the following option to themailer:mailer = "/complete/path/to/venv/bin/aws_mail --region us-east-1 --default-recipients"Configure tools relying on /usr/sbin/sendmailTools like:unattended-upgrade(debian)croncronwill send out emails usingsendmailin case of error logs in/var/log/syslog(debian/Ubuntu)//var/log/messages(AmazonLinux/RHEL/CentOS).aws_mailis configured to logERORRs with imported modules only.I could not find a place to actually configure themailerclient apart from changing the actual code.
So the only option left is to symlink/usr/bin/sendmailtovenv/bin/aws_mail:# Create symlink, remove existing file if necessary.
sudo ln -s /complete/path/to/venv/bin/aws_mail /usr/sbin/sendmailNote:aws_mailignores unknown cli arguments, the ones usually sent tosendmail.Note:Recipients for both,unattended-upgradeandcron, can be configured simliar tologwatch:unattended-upgrade# '/etc/apt/apt.conf.d/50unattended-upgrades'
    Unattended-Upgrade::Mail "email@address.com"cron# Set in according crontab or cron file in '/etc/cron*'.
    MAILTO=email@address.comDevelopment and dependenciesA local developemnt environment can be created the following way:# Clone the repo.
python3 -m venv venv

# Install build dependencies.
venv/bin/pip install -r build_requirements.txt

# Check below for updating python dependencies.

# Install dependencies.
venv/bin/pip-sync --dry-run requirements.txt
venv/bin/pip-sync requirements.txt

# Run.
venv/bin/python -m aws_mail.aws_mailPython dependencies can be added inrequirements.in.Please just run./update_requirements.shto compilerequirements.txt(usingpip-tools) containing only pinned dependency versions eventually.DeploymentCode styleThe necessary configuration files for tools like:flake8blackpylintpre-commitare kept in the common reporitoryhttps://github.com/normoes/python_style_generalt.
The toolcopiercan be used to get the latest version of those files.
By default the latest tag is retrieved, the option--vcs-ref=HEADretrieves from the most recent commit.# Initial command, sets some values for the project.
copier --vcs-ref=HEAD copy  'git@github.com:normoes/python_style_general.git'  ./

# Update the files
copier --vcs-ref=HEAD updateNote:Local changes need to be committed to makecopierwork.