Reduce Anypoint Costs: Use Shared Flows Instead of System APIs
This document talks about how to use Shared Flows in MuleSoft. Usually in an API-led connectivity architecture, we have 3 layers: Experience APIs, Process APIs, and System APIs. This drives business agility for an organization, but at the same time increases costs in Anypoint Platform. Specifically, we are talking about costs associated with the Cloudhub Workers (Number and Size of vCores). A Cloudhub Worker is a dedicated Mule instance where an API runs. So, the number of workers required by an organization is at least equal, if not greater than the number of APIs in the application network. This number quickly gets multiplied by 3 or more, based on the number of Non-Prod and Prod environments.
One way of saving these costs is replacing System APIs (at least the simpler ones) with Shared Flows. We create a Mule project with generalized flows and publish that project to Anypoint Exchange as a Mule-Plugin. Then, we include that Mule-Plugin as a dependency in other Mulesoft APIs and re-use those flows using Flow Reference component.
To showcase this, I have created one such Mule Plugin project named S3-Sys-Flows which contains sub-flows capable of performing basic operations in AWS S3. I recommend that you clone this repository in your local environment and then import the code in Anypoint Studio to review it. We'll publish this code to Anypoint Exchange and re-use its sub-flows in a Test Mule project.
Repository URL
Don't forget to checkout the README.md file to learn more.
Pre-requisites
- Anypoint Platform Account
- Connected App in Anypoint Platform with Correct Scope (Exchange Administrator)
Publish to Exchange
After importing the above code in Anypoint Studio, first you have to replace value of <groupId> tag with your Anypoint Platform's Org ID (or Business Group ID) in pom.xml.
Then open Preferences from Window menu. Under Anypoint Studio heading, click on Maven.
Here you'll replace the Base command line for builds to the following command:
Replace the values of -Dconnectedapp_client_id and -Dconnectedapp_client_secret with the Client ID and Client Secret of your Connected App.
Note: Remember to use the Restore Defaults button here after you are done publishing the Mule Plugin to Anypoint Exchange. Otherwise, you will not be able to run/deploy any Mule App in your local environment.
Click Apply and Close button to save the new command.
Before we Run this Mule App, I recommend that you set Clear Application Data option to Prompt in the Run Configuration. This will help you check the Console logs without any problems.
You can Run the Mule App now, which will use the new Maven command that we saved in the previous step. That command will Publish this Mule-Plugin project to Anypoint Exchange.
Keep an eye on the Console logs and after a few seconds you will see a Clear Application Data prompt. Don't click on any of the options just yet. Drag the prompt box out of your way and take a look at the Console logs. You'll see that this Mule App (or Mule Plugin) has been uploaded to the Anypoint Exchange.
Now, as you go ahead with either Yes or No on that prompt, you will see that the application will fail to deploy. This is fine, because required properties are missing and this plugin app is supposed to be utilized by other Mule Projects.
Let's check Anypoint Exchange, you'll find your Mule Plugin available as a Connector here.
Add as Dependency & Use
In the Mule Project where you wish to use this Mule-Plugin, go to the Mule Palette and click on the Search in Exchange... option.
Here, ensure that you have logged into your Anypoint Platform account. Then using the Search Bar, find the Mule Plugin/Connector that you published in the previous section and Add it to your project.
Once the Mule Plugin is added as a dependency to your Mule Project, you need to add a Global Configuration Element of Import type. On the next window, type in the Name of the XML file (which contains the flows/sub-flows) from Mule Plugin project as shown.
Afterwards, just add a Flow Reference component in your Mule Project and under the Flow name drop down, you'll see all the sub-flows from the Mule-Plugin project available for use.
Troubleshooting Common Issues
Known Issue #1
If you are unable to find your Mule-Plugin in Anypoint Exchange after the Publish to Exchange step, then check the mule-maven-plugin in pom.xml of the Mule Plugin project. The value of classifier tag there must be mule-plugin.
Known Issue #2
Specific to the sample Mule-Plugin project shown here (S3-Sys-Flows), if you are not able to deploy the other Mule Project where this plugin is added then you might have missed to add following properties to it:
s3:
region: "dummy"
role:
arn: "dummy"
duration: "3600" # in seconds
accessKey: "dummy"
secretKey: "dummy"
connectionTimeout: "30" # in seconds
responseTimeout: "30" # in seconds
maxConnections: "-1"
reconnection:
frequency: "2000" # in milliseconds
attempts: "2"
Comments
Post a Comment