[Music]
hi my name is trevor sullivan i'm an AWS
solutions architect and welcome to this
video in the dotnet learning series did
you know that you can create your own
custom events in the AWS cloud there are
a few different ways to accomplish this
such as sending a message to a simple
notification service topic or SNS topic
another option is to send your event
data to the Amazon Cloud watch events
service by default many other AWS
services send key notifications to cloud
watch events but what if you wanted to
create your own custom event
notification I have good news you can as
an example scenario let's say that you
were taking a local backup of some
important files on an ec2 instance once
the backup is completed your local
script uploads the backup archive to an
Amazon s3 bucket next you want to trim
your oldest backups to save on storage
costs by using a short running and
cost-effective lambda function but how
does that lambda function know when your
backup and upload process has completed
on your ec2 instance that's where cloud
watch events comes into play cloud watch
events enables you to trigger one or
more targets across a wide variety of
other AWS cloud services these targets
can include services like AWS step
functions an SNS topic Systems Manager
run command and yes even an AWS lambda
function in the rest of this video we'll
explore how you can send your own custom
application events to the Amazon Cloud
watch service using the AWS tools for
PowerShell once you've installed the AWS
tools for PowerShell also simply known
as the AWS PowerShell module you'll set
up your credentials file and you'll be
ready to go we've covered the setup
process in a separate video so if you're
brand new to AWS or new to PowerShell be
sure to check it out before moving on
before we go any further let's start out
by exploring the AWS PowerShell commands
that allow you to interact with cloud
watch events api's to do this we use the
get AWS command land to ask for a list
of commands by AWS service name so we'll
switch
over to our shell here and call get AWS
command let name - service events you
don't need to know the exact service
name or type it out in full instead just
type part of the unique service name
such as events to perform a pattern
based match in response you'll see a
list of roughly a dozen PowerShell
commands that perform various actions
against the cloud watch events service
take special note of the right cwe event
command this is the command that we'll
be using to write our custom events to
cloud watch now that we know the
PowerShell commands that we want to call
how do we know which parameters to pass
to this command that's where the power
power shells built-in help system comes
to the rescue
we'll call the get help command to learn
the commands signature so that we know
how to call it correctly so switching
back over to our shell will call the get
help - name write cwe event command as
you'll notice from the built in
documentation this command accepts a
single parameter called - entry however
if you look at the data type that the -
entry parameter expects you'll notice
that it's a dotnet object type that
resides in the Amazon CloudWatch model
dotnet namespace in order to call this
command successfully we first need to
construct an instance of the put events
request entry net class set a few
properties on it and then pass it into
the command invocation there are a
couple of ways to instantiate a dotnet
object in PowerShell first you can use
the traditional new object command with
the tightening parameter
alternatively powershell 5.0 introduced
to the static new method to call a
classes constructor so let's go ahead
and take a look at that now so we'll
switch over to our shell and look at the
Amazon Cloud watch events dot model dot
put events request entry class and then
we'll look at the new method on it if
you reference a constructor or method
without the parentheses PowerShell will
return a list of the overloads for that
method in this case there's only a
single parameter list default
constructor so let's go ahead and use
that now we'll actually call the
constructor and assign the new object
instance to a variable named my event so
switching back to the shell here we'll
create a variable called my event and
set it equal to a new instance of the
put events request entry class great so
now we've got a new object so what do we
do now well we need to set some
properties on this objects before we can
pass it into the right cwe event command
how do you know which properties to set
a great starting place is to pipe the
object into the get member powershell
command the get member command performs
type reflection over the objects passed
into it and it will show you the
properties and methods on that object
you should also examine the Amazon Cloud
watch documentation for the put events
API as the authoritative source to
determine what input the service expects
so let's go ahead and take a look at the
get member command first you'll need to
set the time property for the event this
represents the point in time that the
event took place to do that we reference
the my event variable containing the
event object then we specify the time
property and set that equal to the
output of get date which simply returns
the current date and time
next you can optionally add one or more
affected AWS cloud resources to a string
collection setting the resources
property is not mandatory but it helps
correlate application events to specific
resources inside of your AWS account if
you do add any strings to this property
they must be valid at Amazon resource
names or AR ends you can also set this
detail property to a valid JSON string
this property provides context for the
event so that the event receiver knows
what's happening so let's go ahead and
switch to the shell here and we'll grab
the detail property and set that to a
powershell hash table
and then pipe that into the built-in
convert to JSON command so if we do my
event detail you can see that we have a
valid JSON string you also need to set
the source property indicating where the
event was triggered from you can simply
set the source property to a string like
PowerShell or perhaps more usefully the
powershell run space instance ID which
uniquely represents a powershell
instance this is more useful if you're
trying to debug a program using log
files containing that powershell run
space ID so let's go ahead and set that
source property now so well do my event
source equals host run space dot
instance ID gooood and if we take a look
at the source property you'll see that
we have to run space ID the last
property you need to set is the detail
type property which is a custom field
that specifies the type of event that
occurred so we'll go ahead and set my
event dot detail type equal to something
happened with the my event object fully
configured you're finally ready to call
the right cwe event command simply call
the right cwe event command specifying
the dash entry parameter and then pass
in your my event variable in order to
consume events sent by your custom
powershell application you'll need to
configure a cloud watch event rule that
is a topic for a different video however
so let's go ahead and run right cwe
event specify - entry and then pass in
my event as you can see we get back a
populated entry which indicates that the
command ran successfully I hope you
enjoyed learning about sending custom
events to Amazon Cloud watch from
PowerShell keep an eye out for future
videos related to PowerShell development
on AWS thank you
[Music]