This tutorial will show you how to setup SaltStack Reactor system to react to AWS EC2 events like instance start, stop and terminate. This method does not require salt-api or webhooks (Hooray)
Requires SaltStack 2016.3 or this commit https://github.com/saltstack/salt/pull/32557/commits/3031d1b69732559da93cce0fb30eade8cfab7177
aws sqs create-queue \
--queue-name "saltstack"
aws iam create-user \
--username <pick a username>
#Assign to a role#Assign permissions to a user
aws sqs add-permission \
--queue-url <from create-queue output> \
--aws-account-ids "arn:aws:iam::<AWS Account number>:role/saltmaster" \
--actions *
aws sqs add-permission \
--queue-url <from create-queue output> \
--aws-account-ids "arn:aws:iam::<AWS Account number>:user/<username picked in step 2>" \
--actions *
sqs:
region: us-west-2
message_format: json
key: OTHISHISbFaKE014b3gQky3VGET6YOUR8OWNsKEY
keyid: ALSOIFAKEUNICEITRYI0
engines:
sqs_events:
queue: saltstack
aws events put-rule \
--name saltstack \
--description "Sent instance change events to the SaltStack event bus on the Salt Master." \
--event-pattern "{ \"source\": [\"aws.ec2\"], \"detail-type\": [\"EC2 Instance State-change Notification\"]}" aws events put-targets \
--rule saltstack \
--targets Id=<some random id>,Arn=<From output of sqs create-queue>
{%- if data['message']['detail']['state'] == "terminated" %}
delete_key:
wheel.key.delete:
- match: "{{ data['message']['detail']['instance-id'] }}"
{%- endif %}
--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yes, this kind of documentation/tutorial is really cool and should really be stored somewhere.
It reminds of the (now missing) "Into the Salt Mine" blog (or I forgot what the name was, which was really useful when I started to learn Salt.
This kind of slightly more advanced topic is very cool, too.
> Notification,source:aws.ec2,account:XXXXXXXXX,time:2016-07-02T08:56:38Z,region:eu-west-1,resources:[arn:aws:ec2:eu-west-1:xxxxxx:instance/i-2ca045a7],detail:{instance-id:i-2ca045a7,state:running}}
reactor: - 'salt/engine/sqs': - /srv/reactor/aws.sls