Mule App to Create Signed AWS API Request (Signature Version 4)
This document covers a Mule Application code that publishes a message to AWS SNS Topic without using AWS Connectors. Instead, we will create a Signed AWS API Request (Signature Version 4) - Link in Dataweave and send it using a HTTP Request component. We will have an AWS SQS Queue subscribed to that SNS Topic to check the message delivery. This demo will be done in a local Windows environment.
Let’s begin...
Pre-requisites
- AWS Account. Region used in this demo is US East (Ohio) i.e., us-east-2.
- IAM User part of a User Group having the following 2 permission policies attached - AmazonSQSFullAccess & AmazonSNSFullAccess. Also, assuming that you have the accessKey and secretKey of that user.
- SNS Topic (Type - FIFO) with Content-based message deduplication set to Enabled.
- SQS Queue (Type - FIFO) with Content-based message deduplication set to Enabled and Subscribed to the above said SNS Topic.
- Postman
- Anypoint Studio and Java (Be sure to verify that AnypointStudio.ini file’s -vm path is pointing to the JDK you are using).
For this demo, I am using JDK 11.0.17 - Link. But MuleSoft recommends JDK 8 for best compatibility in Anypoint Studio and Cloudhub deployments. I have also correctly configured %JAVA_HOME% and Path system environment variables in Windows.
Anypoint Studio being used is version 7.14 and its AnypointStudio.ini file has following entry -
Anypoint Studio Code
Create a new Mule Application project in Anypoint Studio. Create a file named app-dev.yaml (You can name it as you wish) in the following project path -
The final file structure will look like this -
Enter the following code snippet in this file and make changes wherever required. Don't forget to encrypt the AWS IAM User's accessKey and secretKey first using the Secure Properties Tool - Link
Now, include the Secure Properties module in your Workspace's Mule Palette and create a Secure Properties Config in this project. Point it to the app-dev.yaml file you just created. OR You can just use the following code -
Next, download the following two *.dwl files - GitHub Gist Link
Place them in this project path -
The final file structure will look like this -
src/main/resources/dwlfiles/awsSignature.dwl
Now, open the project’s main *.xml file (You can later segregate the entire code in interface.xml and implementation.xml files if you want) and use the following XML code to setup a listener flow -
name="send-data-to-SNS-interface-flow"
doc:id="7d8d07d1-feaa-4011-b1e4-bc426f26887d"
>
<http:listener
doc:name="Listener"
doc:id="b99bede3-2d0e-4c24-9e44-c4f80ba47f97"
config-ref="HTTP_Listener_config"
path="/send"
allowedMethods="POST"
/>
<flow-ref
doc:name="send-data-to-SNS-implementation-flow"
doc:id="3ec7f7bd-33b6-4652-8f67-4b30c9878a79"
name="send-data-to-SNS-implementation-flow"
/>
</flow>
This code will look like this -
You will have to create a Listener configuration. For that, you can use the following code -
Then, in the same *.xml file, use the following XML code to setup the subflow that creates Signed AWS API Request -
This code will look like this -
Do not try to configure the HTTP Request component. It just needs to hit the final URL, nothing else.
Don’t forget to provide your encryption key in Run configuration as environment variables – mule.key
Run the Mule Application now.
You can import the following Postman Collection (v2.1) file in Postman to test the application with an example payload - GitHub Gist Link
Alternatively, if you want, the cURL code for the same is below -
As you send this request and if all goes well, then the Console View in Anypoint Studio should first log the final Signed AWS API Request URL (You can even use this URL in a Browser, it will work) and then its response as shown below -
You can go to AWS Console and verify that the message was received or not by using the Poll for messages button in SQS Dashboard. You'll find your message there as shown -
This completes the demo.
Troubleshooting Common Issues
I don’t think anyone will encounter any issues using this guide so I will leave the common issues part blank this time :)
Comments
Post a Comment