AWS Elastic Compute Cloud (EC2)
Since Camel 3.1
Only producer is supported
The AWS2 EC2 component supports the ability to create, run, start, stop and terminate AWS EC2 instances.
Prerequisites
You must have a valid Amazon Web Services developer account, and be signed up to use Amazon EC2. More information is available at Amazon EC2.
URI Format
aws2-ec2://label[?options]
You can append query options to the URI in the following format:
?options=value&option2=value&…
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The AWS Elastic Compute Cloud (EC2) component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Autowired To use a custom software.amazon.awssdk.services.ec2.Ec2Client. When this option is set or autowired, the region, credentials, proxy, endpoint override and trustAllCertificates options are not used. The endpoint does not close a client that is set or autowired. | Ec2Client | ||
The configuration that the endpoints of this component start from. Each endpoint uses a copy of it. | AWS2EC2Configuration | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Required The operation that the producer performs. The CamelAwsEC2Operation header replaces this option. Enum values:
| AWS2EC2Operations | ||
Whether the EC2 client sends the requests to the URI of the uriEndpointOverride option instead of the EC2 endpoint of the region. It has no effect when uriEndpointOverride is not set. | false | boolean | |
Whether the body is the request of the AWS SDK for the operation, such as a DescribeInstancesRequest for describeInstances. If true, the producer sends that request as it is, and a body of another type fails. If false, the producer builds the request from the headers. | false | boolean | |
The ID of the AWS region of the EC2 service, in lower case, for example eu-west-1 (the value of Region.EU_WEST_1.id()). If not set, the AWS SDK resolves the region itself, for example from the AWS_REGION environment variable. Enum values:
| String | ||
The URI that the EC2 client sends the requests to, for example http://localhost:4566 for a local emulator. It is used only when overrideEndpoint is true. | String | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean | |
The host name of the proxy that the EC2 client uses. The client uses a proxy only when the proxyPort option is set too. | String | ||
The port of the proxy that the EC2 client uses. The client uses a proxy only when the proxyHost option is set too. | Integer | ||
The protocol of the proxy that the EC2 client uses. It is used only when the proxyHost and proxyPort options are both set. Enum values:
| HTTPS | Protocol | |
The access key ID of the AWS credentials, used together with the secretKey option. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true. | String | ||
The name of the profile to load when useProfileCredentialsProvider is true. If not set, the AWS SDK uses its default profile, which the AWS_PROFILE environment variable can set. | String | ||
The secret access key of the AWS credentials, used together with the accessKey option. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true. | String | ||
The session token of temporary AWS credentials, for example of an assumed IAM role. It is used only when useSessionCredentials is true, together with the accessKey and secretKey options. | String | ||
Whether the EC2 client accepts any TLS certificate of the server without checking it. It applies whether or not the endpoint is overridden. Use it only for tests, for example with a local emulator. | false | boolean | |
Whether the EC2 client loads the credentials with the default credentials provider chain of the AWS SDK: system properties, environment variables, web identity token, the shared credentials and config files, and container or instance profile credentials. It takes precedence over the other credentials options. | false | boolean | |
Whether the EC2 client loads the credentials of a profile of the shared AWS credentials and config files. The profileCredentialsName option names the profile. It takes precedence over useSessionCredentials and the accessKey and secretKey options. | false | boolean | |
Whether the EC2 client uses temporary credentials made of the accessKey, secretKey and sessionToken options, for example to assume an IAM role. If one of the three is not set, the session credentials are not used. | false | boolean |
Endpoint Options
The AWS Elastic Compute Cloud (EC2) endpoint is configured using URI syntax:
aws2-ec2:label
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Autowired To use a custom software.amazon.awssdk.services.ec2.Ec2Client. When this option is set or autowired, the region, credentials, proxy, endpoint override and trustAllCertificates options are not used. The endpoint does not close a client that is set or autowired. | Ec2Client | ||
Required The operation that the producer performs. The CamelAwsEC2Operation header replaces this option. Enum values:
| AWS2EC2Operations | ||
Whether the EC2 client sends the requests to the URI of the uriEndpointOverride option instead of the EC2 endpoint of the region. It has no effect when uriEndpointOverride is not set. | false | boolean | |
Whether the body is the request of the AWS SDK for the operation, such as a DescribeInstancesRequest for describeInstances. If true, the producer sends that request as it is, and a body of another type fails. If false, the producer builds the request from the headers. | false | boolean | |
The ID of the AWS region of the EC2 service, in lower case, for example eu-west-1 (the value of Region.EU_WEST_1.id()). If not set, the AWS SDK resolves the region itself, for example from the AWS_REGION environment variable. Enum values:
| String | ||
The URI that the EC2 client sends the requests to, for example http://localhost:4566 for a local emulator. It is used only when overrideEndpoint is true. | String | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The host name of the proxy that the EC2 client uses. The client uses a proxy only when the proxyPort option is set too. | String | ||
The port of the proxy that the EC2 client uses. The client uses a proxy only when the proxyHost option is set too. | Integer | ||
The protocol of the proxy that the EC2 client uses. It is used only when the proxyHost and proxyPort options are both set. Enum values:
| HTTPS | Protocol | |
The access key ID of the AWS credentials, used together with the secretKey option. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true. | String | ||
The name of the profile to load when useProfileCredentialsProvider is true. If not set, the AWS SDK uses its default profile, which the AWS_PROFILE environment variable can set. | String | ||
The secret access key of the AWS credentials, used together with the accessKey option. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true. | String | ||
The session token of temporary AWS credentials, for example of an assumed IAM role. It is used only when useSessionCredentials is true, together with the accessKey and secretKey options. | String | ||
Whether the EC2 client accepts any TLS certificate of the server without checking it. It applies whether or not the endpoint is overridden. Use it only for tests, for example with a local emulator. | false | boolean | |
Whether the EC2 client loads the credentials with the default credentials provider chain of the AWS SDK: system properties, environment variables, web identity token, the shared credentials and config files, and container or instance profile credentials. It takes precedence over the other credentials options. | false | boolean | |
Whether the EC2 client loads the credentials of a profile of the shared AWS credentials and config files. The profileCredentialsName option names the profile. It takes precedence over useSessionCredentials and the accessKey and secretKey options. | false | boolean | |
Whether the EC2 client uses temporary credentials made of the accessKey, secretKey and sessionToken options, for example to assume an IAM role. If one of the three is not set, the session credentials are not used. | false | boolean |
Message Headers
The AWS Elastic Compute Cloud (EC2) component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The ID of the Amazon Machine Image (AMI) to launch, for createAndRunInstances, which requires it. | String | |
CamelAwsEC2InstanceType (producer) Constant: | The instance type of the instances to launch, for createAndRunInstances, which requires it. It is an InstanceType, its AWS value (such as t2.micro), or the name of the constant (such as T2_MICRO). Enum values:
| InstanceType | |
CamelAwsEC2Operation (producer) Constant: | The operation that the producer performs. This header replaces the operation option. | String | |
CamelAwsEC2InstanceMinCount (producer) Constant: | The minimum number of instances to launch, for createAndRunInstances, which requires it. If EC2 cannot launch this many instances, it launches none. | Integer | |
CamelAwsEC2InstanceMaxCount (producer) Constant: | The maximum number of instances to launch, for createAndRunInstances, which requires it. If EC2 cannot launch this many instances, it launches as many as it can above the minimum. | Integer | |
CamelAwsEC2InstanceMonitoring (producer) Constant: | Whether detailed monitoring is enabled for the instances that createAndRunInstances launches. If not set, EC2 enables basic monitoring only. | Boolean | |
CamelAwsEC2InstanceKernelId (producer) Constant: | The ID of the kernel of the instances that createAndRunInstances launches. | String | |
CamelAwsEC2InstanceEbsOptimized (producer) Constant: | Whether the instances that createAndRunInstances launches are optimized for Amazon EBS I/O. Not every instance type supports it, and it can cost extra. | Boolean | |
CamelAwsEC2InstanceSecurityGroups (producer) Constant: | The names of the security groups of the instances that createAndRunInstances launches, as a Collection of String. EC2 accepts group names only in the default VPC; for another VPC, use pojoRequest with the security group IDs in the RunInstancesRequest. If not set, EC2 uses the default security group. | Collection | |
CamelAwsEC2InstancesIds (producer) Constant: | The IDs of the instances, as a Collection of String or as a String of IDs separated by commas. startInstances, stopInstances, terminateInstances, rebootInstances, monitorInstances and unmonitorInstances require it, and describeInstances and describeInstancesStatus filter by it when it is set. createTags and deleteTags require it too, as the IDs of the resources to tag, which can be any EC2 resources. | Collection | |
CamelAwsEC2InstancesKeyPair (producer) Constant: | The name of the key pair of the instances that createAndRunInstances launches. | String | |
CamelAwsEC2InstancesClientToken (producer) Constant: | A unique, case-sensitive token that makes createAndRunInstances idempotent: EC2 does not launch the instances again for a retry with the same token. | String | |
CamelAwsEC2InstancesPlacement (producer) Constant: | The placement of the instances that createAndRunInstances launches, such as the Availability Zone. | Placement | |
CamelAwsEC2InstancesTags (producer) Constant: | The tags, as a Collection of Tag, that createTags adds to the resources in CamelAwsEC2InstancesIds, or that deleteTags removes from them. Both operations require it. | Collection | |
CamelAwsEC2SubnetId (producer) Constant: | The ID of the subnet to launch the instances into, for createAndRunInstances. | String | |
CamelAwsEC2NextToken (describeInstances describeInstancesStatus) Constant: | The pagination token of describeInstances and describeInstancesStatus. The producer reads it as the token of the page to return, and sets it on the result to the token of the next page, which has no value on the last page. | String | |
CamelAwsEC2MaxResults (describeInstances describeInstancesStatus) Constant: | The maximum number of results in one page of describeInstances and describeInstancesStatus, from 5 to 1000. EC2 does not accept it together with the IDs of CamelAwsEC2InstancesIds. | Integer | |
CamelAwsEC2IsTruncated (describeInstances describeInstancesStatus) Constant: | Whether describeInstances or describeInstancesStatus has more results, which is when the result has a token of the next page in CamelAwsEC2NextToken. The producer sets it. | Boolean |
Message Body
The message body at each side of the AWS Elastic Compute Cloud (EC2) component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| Not used: the operations take their input from headers, such as the ids of the instances in CamelAwsEC2InstancesIds as a Collection. With pojoRequest=true the request of the AWS SDK for the operation, such as a DescribeInstancesRequest: another body fails.
|
Producer output |
| The response of the AWS SDK for the operation, such as a RunInstancesResponse for createAndRunInstances; rebootInstances keeps the body.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Required EC2 component options
You have to provide the amazonEc2Client in the Registry or your accessKey and secretKey to access the Amazon EC2 service.
Usage
Static credentials, Default Credential Provider and Profile Credentials Provider
You have the possibility of avoiding the usage of explicit static credentials by specifying the useDefaultCredentialsProvider option and set it to true.
The order of evaluation for Default Credentials Provider is the following:
-
Java system properties -
aws.accessKeyIdandaws.secretKey. -
Environment variables -
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY. -
Web Identity Token from AWS STS.
-
The shared credentials and config files.
-
Amazon ECS container credentials - loaded from the Amazon ECS if the environment variable
AWS_CONTAINER_CREDENTIALS_RELATIVE_URIis set. -
Amazon EC2 Instance profile credentials.
You have also the possibility of using Profile Credentials Provider, by specifying the useProfileCredentialsProvider option to true and profileCredentialsName to the profile name.
Only one of static, default and profile credentials could be used at the same time.
For more information about this you can look at AWS credentials documentation
Examples
Producer Examples
-
createAndRunInstances: this operation will create an EC2 instance and run it
-
Java
-
XML
-
YAML
from("direct:createAndRun")
.setHeader("CamelAwsEC2ImageId", constant("ami-fd65ba94"))
.setHeader("CamelAwsEC2InstanceType", constant(InstanceType.T2_MICRO))
.setHeader("CamelAwsEC2InstanceMinCount", constant("1"))
.setHeader("CamelAwsEC2InstanceMaxCount", constant("1"))
.to("aws2-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=createAndRunInstances"); <route>
<from uri="direct:createAndRun"/>
<setHeader name="CamelAwsEC2ImageId">
<constant>ami-fd65ba94</constant>
</setHeader>
<setHeader name="CamelAwsEC2InstanceType">
<constant>t2.micro</constant>
</setHeader>
<setHeader name="CamelAwsEC2InstanceMinCount">
<constant>1</constant>
</setHeader>
<setHeader name="CamelAwsEC2InstanceMaxCount">
<constant>1</constant>
</setHeader>
<to uri="aws2-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=createAndRunInstances"/>
</route> - route:
from:
uri: direct:createAndRun
steps:
- setHeader:
name: CamelAwsEC2ImageId
constant: "ami-fd65ba94"
- setHeader:
name: CamelAwsEC2InstanceType
constant: "t2.micro"
- setHeader:
name: CamelAwsEC2InstanceMinCount
constant: "1"
- setHeader:
name: CamelAwsEC2InstanceMaxCount
constant: "1"
- to:
uri: aws2-ec2://TestDomain
parameters:
accessKey: xxxx
secretKey: xxxx
operation: createAndRunInstances -
startInstances: this operation will start a list of EC2 instances
The CamelAwsEC2InstancesIds header is a collection of instance IDs, or a String of IDs separated by commas (such as i-1,i-2), which can be set from XML and YAML routes too.
from("direct:start")
.process(exchange -> {
exchange.getIn().setHeader("CamelAwsEC2InstancesIds", List.of("myinstance"));
})
.to("aws2-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=startInstances"); -
stopInstances: this operation will stop a list of EC2 instances
from("direct:stop")
.process(exchange -> {
exchange.getIn().setHeader("CamelAwsEC2InstancesIds", List.of("myinstance"));
})
.to("aws2-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=stopInstances"); -
terminateInstances: this operation will terminate a list of EC2 instances
from("direct:terminate")
.process(exchange -> {
exchange.getIn().setHeader("CamelAwsEC2InstancesIds", List.of("myinstance"));
})
.to("aws2-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=terminateInstances"); Using a POJO as body
Sometimes building an AWS Request can be complex because of multiple options. We introduce the possibility to use a POJO as a body. In AWS EC2 there are multiple operations you can submit, as an example for Create and run an instance, you can do something like:
from("direct:start")
.setBody(RunInstancesRequest.builder().imageId("test-1").instanceType(InstanceType.T2_MICRO).build())
.to("aws2-ec2://TestDomain?accessKey=xxxx&secretKey=xxxx&operation=createAndRunInstances&pojoRequest=true"); In this way, you’ll pass the request directly without the need of passing headers and options specifically related to this operation.
Dependencies
Maven users will need to add the following dependency to their pom.xml.
pom.xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-aws2-ec2</artifactId>
<version>${camel-version}</version>
</dependency> where ${camel-version} must be replaced by the actual version of Camel.