c7n Mailer type error when using variable for email address

215 views
Skip to first unread message

mwilco...@gmail.com

unread,
Mar 6, 2019, 4:55:20 PM3/6/19
to cloud-custodian
Hi,

I'm using c7n-org and in my accounts.yml file, I'm using variables per AWS account for the notification email address like this:

accounts:
- account_id: '123123123123'
  name: account-1
  role: arn:aws:iam::123123123123:role/CloudCustodian
  vars:
    admin-email: 'account...@email.com'

- account_id: '321321321321' name: account-2 role: arn:aws:iam::321321321321:role/CloudCustodian vars: admin-email: 'account...@email.com'


Then I use them in policies like this:

actions: - type: notify template: default.html priority_header: 1 subject: "My Subject - [custodian {{ account }} - {{ region }}]" violation_desc: "RDS Instance has had no connections in the last 28 days and has been deleted." action_desc: "Description" to: - { admin-email } - event-owner transport: type: sqs queue: https://sqs.us-east-1.amazonaws.com/123123123123/cloud-custodian-mailer region: us-east-1

When I use the { admin-email } variable, c7n Mailer fails with this message:

[ERROR] 2019-03-06T20:50:42.181Z 03ed9a50-6c7b-46fe-a6f4-de54fe1a190e Error starting mailer MailerSqsQueueProcessor().
Error: sequence item 0: expected string, dict found

Traceback (most recent call last):
File "/var/task/c7n_mailer/handle.py", line 46, in start_c7n_mailer
mailer_sqs_queue_processor.run(parallel)
File "/var/task/c7n_mailer/sqs_queue_processor.py", line 128, in run
self.process_sqs_message(sqs_message)
File "/var/task/c7n_mailer/sqs_queue_processor.py", line 155, in process_sqs_message
', '.join(sqs_message['action'].get('to'))))
TypeError: sequence item 0: expected string, dict found

Is it possible to use variables like this or am I doing something wrong?

Thanks,

Matt

Kapil Thangavelu

unread,
Mar 6, 2019, 5:21:11 PM3/6/19
to mwilco...@gmail.com, cloud-custodian
There appears to be some peculiarities to python's formatting (lots of examples https://pyformat.info)  it looks like the extra spaces around the { admin-email } are causing some issues

ie. removing the extra space seems to work, but with spaces it causes an error 

>>> "hello {admin-email}".format(**{'admin-email': 'xyz'})

'hello xyz'

>>> "hello { admin-email }".format(**{'admin-email': 'xyz'})

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

KeyError: ' admin-email '

"hello {admin-email}".format(**{'admin-email': 'xyz'})


--
You received this message because you are subscribed to the Google Groups "cloud-custodian" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-custodi...@googlegroups.com.
To post to this group, send email to cloud-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-custodian/1bf88cc6-8a0d-421d-b258-9786e1f79020%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mwilco...@gmail.com

unread,
Mar 7, 2019, 3:49:35 AM3/7/19
to cloud-custodian
Thanks Kapil,

I just tried removing the spaces so now I just have {admin-email}, deployed the policy, tested it and I still get the same error from Mailer:

Error: sequence item 0: expected string, dict found

Traceback (most recent call last):
File "/var/task/c7n_mailer/handle.py", line 46, in start_c7n_mailer
mailer_sqs_queue_processor.run(parallel)
File "/var/task/c7n_mailer/sqs_queue_processor.py", line 128, in run
self.process_sqs_message(sqs_message)
File "/var/task/c7n_mailer/sqs_queue_processor.py", line 155, in process_sqs_message
', '.join(sqs_message['action'].get('to'))))
TypeError: sequence item 0: expected string, dict found

Here is the action I have in my policy:

    actions:
      - type: notify
        template: default.html
        priority_header: 1
        subject: "subject-[custodian {{ account_id }} - {{ region }}]"
        violation_desc: "description:"
        action_desc: |
            "description"

        to:
            - {admin-email}
            - event-owner
        transport:
            type: sqs
            queue: https://sqs.us-east-1.amazonaws.com/123123123123/c7n-mailer
            region: us-east-1

mwilco...@gmail.com

unread,
Mar 7, 2019, 5:19:14 AM3/7/19
to cloud-custodian
I've done some testing and I don't think the variable is being passed to the policy.

I tested just adding the admin-email variable to the subject line in the same way as I do for the account and region as shown below and it literally prints out "{ admin-email }":

    subject: "My Subject - [custodian {{ account }} - {{ region }} - {{ admin-email }}"

I tried all of these and none of them printed the email address that I set in the accounts.yml file:

* admin-email
* {admin-email}
* { admin-email }
* {{admin-email}}
* {{ admin-email }}

I thought I set it right after double checking here:
https://github.com/cloud-custodian/cloud-custodian/tree/master/tools/c7n_org


I'm not sure what I've missed?

Thanks,

Matt


On Wednesday, March 6, 2019 at 9:55:20 PM UTC, mwilco...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages