Amazon SNS (Simple Notification Service) Perl libraries
-
22-02-2021 - |
Question
I wanted to start using the Amazon Simple Notification Service, but I have not found any Perl libraries that I can use to access the service. I would rather not create my own library, I wanted to see if anybody has used any Perl libraries for the SNS service, and if they would recommend any.
Solution 2
I used Net::Amazon::AWSSign in conjunction with a tiny script:
#!/usr/bin/perl
use Net::Amazon::AWSSign;
$ACCESS_KEY_ID="<my key id>";
$SECRET_KEY="<my secret key>";
$TOPIC_ARN='<my topic arn>';
$TOPIC_ARN =~ s/:/%3A/g;
$MESSAGE="This is a test";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon+=1;
$timestamp = sprintf("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d.000Z",
$year,$mon,$mday,$hour,$min,$sec);
$timestamp =~ s/:/%3A/g;
$REQUEST="http://sns.us-east-1.amazonaws.com/".
"?TopicArn=$TOPIC_ARN".
"&Message=$MESSAGE".
"&Action=Publish".
"&SignatureVersion=2".
"&SignatureMethod=HmacSHA256".
"&Timestamp=$timestamp".
"&AWSAccessKeyId=$ACCESS_KEY_ID";
my $awsSign=new Net::Amazon::AWSSign("$ACCESS_KEY_ID", "$SECRET_KEY");
$signed = $awsSign->addRESTSecret($REQUEST);
$res = `curl -s -o- '$signed'`;
if ($res =~ /<error>/) {
print "ERROR!\n";
return 1;
}
0;
I actually used XML::Simple in the end, and passed the result from Curl to XMLIn, to parse the XML that Amazon returns. Do what you will...
OTHER TIPS
Amazon::SNS exists. The docs are pretty sparse but it looks like it does the basics, and the code quality looks fine to me.
I used Brad's as the starting point, thanks Brad! I had the change the localtime to gmtime. I was also not using topics but Target ARNs and using role based authentication. I had to pass the SecurityToken to get it to work and the Message only worked for android push when I put it in a GCM json wrapper. In the code I use my Application name in the TargetARN to detect the platform and adjust the payload accordingly. Note: Windows code it untested.
One last item of note is the crazy nested json encoding that seems to be required of SNS.
sub send_sns
{
# required arguments: endpoint (AWS SNS endpoint), message
my $args = shift;
my $TargetArn=encode_url($args->{endpoint});
my $message=$args->{message};
my $data = {};
my $json = JSON->new->utf8->allow_nonref;
if ($args->{endpoint} =~ /GCM\/[a-z]+_android\//) {
# Android
$data->{data}{message}=$args->{message};
my $dataString = $json->encode($data);
$message = '{"GCM": '.$json->encode( $dataString ).'}';
} elsif ($args->{endpoint} =~ /APNS\/[a-z]+_apple_ios\//) {
# iOS
$data->{aps}{alert}=$args->{message};
my $dataString = $json->encode($data);
$message = '{"APNS": '.$json->encode( $dataString ).'}';
} elsif ($args->{endpoint} =~ /ADM\/[a-z]+_windows\//) {
# windows (incomplete)
$data->{data}{message}=$args->{message};
my $dataString = $json->encode($data);
$message = '{"ADM": '.$json->encode( $dataString ).'}';
}
use Net::Amazon::AWSSign;
my $credentials = qx[ curl -s --fail http://169.254.169.254/latest/meta-data/iam/security-credentials/myrole ];
my $credObj = decode_json($credentials);
my $ACCESS_KEY_ID=$credObj->{AccessKeyId};
my $SECRET_KEY=$credObj->{SecretAccessKey};
my $token=$credObj->{Token};
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$year += 1900;
$mon+=1;
my $timestamp = sprintf("%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2d.000Z",
$year,$mon,$mday,$hour,$min,$sec);
$timestamp =~ s/:/%3A/g;
my $REQUEST="http://sns.us-east-1.amazonaws.com/".
"?TargetArn=$TargetArn".
"&Message=$message".
"&Action=Publish".
"&SignatureVersion=2".
"&SignatureMethod=HmacSHA256".
"&Timestamp=$timestamp".
"&SecurityToken=$token".
"&MessageStructure=json".
"&AWSAccessKeyId=$ACCESS_KEY_ID";
my $awsSign=new Net::Amazon::AWSSign("$ACCESS_KEY_ID", "$SECRET_KEY");
my $signed = $awsSign->addRESTSecret($REQUEST);
$res = `curl -s -o- '$signed'`;
print "returns: $res\n" if -t;
if ($res =~ /<error>/) {
print "ERROR!\n";
return 1;
} else {
return 0;
}
}