Contact Us 1-800-596-4880

Amazon EC2 Connector 2.5 Example - Mule 4

This example creates an Amazon EBS (Elastic Block Store) volume that you can attach to an Amazon EC2 instance that is in the same availability zone as the volume.

The example includes configuring global elements for Anypoint Connector for Amazon EC2 (Amazon EC2 Connector), configuring additional properties such as volume size and type, configuring the Set Payload component, adding and configuring the Logger component, and testing your application’s use of the connector.

The following image shows the Mule flow for this example:

Flow for creating an EBS volume

This example uses variables for some field values. You can either:

  • Replace the variables with their values in the code.

  • Provide the values for each variable in a properties file and then refer to that file from the connector configuration.
    If you don’t know how to use a properties file, see Configuring Property Placeholders.

Configure Global Elements for the Connector

  1. Create a new Mule project in Anypoint Studio.

  2. Create a file named mule-artifact.properties to store your Amazon credentials.

  3. Add the following properties to the mule-artifact.properties file:

    amazon.accesskey=<Access Key>
    amazon.secretkey=<Secret Key>
    amazon.region=<Region>
    amazon.sessionToken=<Session Token>
  4. Save the mule-artifact.properties file in the project’s src/main/resources directory.

  5. Drag the HTTP Listener component to the canvas.

  6. Add a global element for HTTP Listener:

    1. Select Listener on the canvas.

    2. In the configuration screen for the operation, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields.

    3. In the Global Element Properties dialog, provide the following values:

      • Name
        Listener

      • Host
        localhost

      • Port
        8081

        The following image shows an example of the HTTP Listener global element configuration:

        Global element configuration for HTTP Listener
    4. Click OK.

  7. Drag the Amazon EC2 Connector Create volume operation next to the Listener component.

  8. Add a new Amazon EC2 global element:

    1. Select the Create volume operation on the canvas.

    2. In the configuration screen for the operation, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields.

  9. Configure the global element as follows:

    • Name
      Amazon EC2 Configuration

    • Session Token
      Session token used to validate the temporary security credentials

    • Access Key
      Alphanumeric text string that uniquely identifies the user who owns the account

    • Secret Key

      Key that acts as the password

    • Region Endpoint
      Region to select from the drop-down menu for the Amazon EC2 client

      The following image shows an example of what the configuration looks like:

      EC2 global configuration

      The corresponding XML looks like this:

      <ec2:config
      	name="Amazon_EC2_configuration"
      	doc:name="Amazon EC2 Configuration" >
      <ec2:basic-connection
      	accessKey="${amazon.accesskey}"
      	secretKey="${amazon.secretkey}"
      	region="${amazon.region}"
      	sessionToken="${amazon.sessionToken}"/>
      </ec2:config>
  10. Click Test Connection to confirm that Mule can connect with the Amazon EC2 instance.

  11. If the connection is successful, click OK to save the configurations.

    Otherwise, review or correct any incorrect parameters, then test again.

Configure Additional Properties

  1. Select the Create volume operation on the Studio canvas.

  2. In the General tab, enter the following values:

    1. Enter a value for Display Name.
      This example uses Create volume.

    2. In the Basic Settings section, select the name of the Amazon EC2 configuration you created in Configure Global Elements for the Connector.

    3. In the General section, enter values for the following fields:

      • Availability Zone

        Amazon availability zone you have access to. This example uses us-east-1a.

      • Size

        Size of the volume in gibibytes (GiBs). This example uses 5.

      • Volume Type

        The default volume type is Standard.

        Publish message connector props
  3. Verify that your XML looks like this:

    <ec2:create-volume
    	config-ref="Amazon_EC2_configuration"
    	availabilityZone="us-east-1a"
    	doc:name="Create volume"
    	size="5"/>

Configure the Set Payload Component

  1. In the Mule Palette view, select Set Payload and drag it to the right of the Amazon EC2 Create volume operation.
    The Set Payload transformer sends the response to the client in the browser.

  2. In the canvas, select Set Payload and set the Value field to #[payload.volume.volumeId].

    This prints the volume ID of the EBS volume.

    The following image shows the Set Payload configuration:

    EC2 Create Volume payload transformer

Add and Configure the Logger Component

  1. In the Mule Palette view, select Logger and drag it to the right of the Set Payload component.
    The Logger component displays the volume ID that is transformed by the Set Payload component from the Create Volume operation in the Mule console.

  2. In the canvas, select Logger and enter the following values:

    • Message
      #[payload]

    • Level
      INFO

      EC2 create volume logger
  3. Save the project.

Test the App

  1. In Package Explorer, right-click the project and select Run As > Mule Application.

  2. Browse to http://localhost:8081/createVolume.

    You see the generated volume ID in the browser and console.

Example Mule App XML Code

Copy and paste this code into your XML Editor to quickly load the flow for this example use case into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ec2="http://www.mulesoft.org/schema/mule/ec2"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
	http://www.mulesoft.org/schema/mule/core/current/mule.xsd
	http://www.mulesoft.org/schema/mule/http
	http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
	http://www.mulesoft.org/schema/mule/ec2
	http://www.mulesoft.org/schema/mule/ec2/current/mule-ec2.xsd">
	<http:listener-config
		name="HTTP_Listener_config"
		doc:name="HTTP Listener config" >
		<http:listener-connection host="localhost" port="8081" />
	</http:listener-config>
	<ec2:config
		name="Amazon_EC2_configuration"
		doc:name="Amazon EC2 Configuration" >
		<ec2:basic-connection
			accessKey="${amazon.accesskey}"
			secretKey="${amazon.secretkey}"
			region="USEAST1"
			sessionToken="${amazon.sessionToken}"/>
	</ec2:config>
	<flow name="create-ebs-volume" >
		<http:listener
			config-ref="HTTP_Listener_config"
			path="/createVolume"
			doc:name="Listener" />
		<ec2:create-volume
			config-ref="Amazon_EC2_configuration"
			availabilityZone="us-east-1a"
			doc:name="Create volume"
			size="5"/>
		<set-payload
			value="#[payload.volume.volumeId]"
			doc:name="Set Payload"  />
		<logger
			level="INFO"
			doc:name="Logger"
			message="#[payload]"/>
	</flow>
</mule>
View on GitHub