Files
spring-cloud-static/spring-cloud-aws/2.0.0.M2/multi/multi__cloud_environment.html
2017-11-17 04:34:49 +00:00

138 lines
26 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>3.&nbsp;Cloud environment</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="multi_spring-cloud-aws.html" title="Spring Cloud AWS"><link rel="up" href="multi_spring-cloud-aws.html" title="Spring Cloud AWS"><link rel="prev" href="multi__basic_setup.html" title="2.&nbsp;Basic setup"><link rel="next" href="multi__managing_cloud_environments.html" title="4.&nbsp;Managing cloud environments"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.&nbsp;Cloud environment</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__basic_setup.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="multi__managing_cloud_environments.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_cloud_environment" href="#_cloud_environment"></a>3.&nbsp;Cloud environment</h1></div></div></div><p>Applications often need environment specific configuration information, especially in changing environments like in the
Amazon cloud environment. Spring Cloud AWS provides a support to retrieve and use environment specific data inside the
application context using common Spring mechanisms like property placeholder or the Spring expression language.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_retrieving_instance_metadata" href="#_retrieving_instance_metadata"></a>3.1&nbsp;Retrieving instance metadata</h2></div></div></div><p><a class="link" href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html" target="_top">Instance metadata</a> are available inside an
EC2 environment. The metadata can be queried using a special HTTP address that provides the instance metadata. Spring Cloud
AWS enables application to access this metadata directly in expression or property placeholder without the need to call
an external HTTP service.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_enabling_instance_metadata_support_with_xml" href="#_enabling_instance_metadata_support_with_xml"></a>3.1.1&nbsp;Enabling instance metadata support with XML</h3></div></div></div><p>The instance metadata retrieval support is enabled through an XML element like the standard property placeholder in Spring.
The following code sample demonstrates the activation of the instance metadata support inside an application context.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;aws-context:context-instance-data</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</span></pre><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p>Instance metadata can be retrieved without an authorized service call, therefore the configuration above does not require
any region or security specific configuration.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_enabling_instance_metadata_support_with_java" href="#_enabling_instance_metadata_support_with_java"></a>3.1.2&nbsp;Enabling instance metadata support with Java</h3></div></div></div><p>The instance metadata can also be configured within a Java configuration class without the need for an XML configuration.
The next example shows a typical Spring <code class="literal">@Configuration</code> class that enables the instance metadata with the
<code class="literal">org.springframework.cloud.aws.context.config.annotation.EnableInstanceData</code></p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
<em><span class="hl-annotation" style="color: gray">@EnableContextInstanceData</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationConfiguration {
}</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_enabling_instance_metadata_support_in_spring_boot" href="#_enabling_instance_metadata_support_in_spring_boot"></a>3.1.3&nbsp;Enabling instance metadata support in Spring Boot</h3></div></div></div><p>The instance metadata is automatically available in a Spring Boot application as a property source if the application
is running on an EC2 instance.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_using_instance_metadata" href="#_using_instance_metadata"></a>3.1.4&nbsp;Using instance metadata</h3></div></div></div><p>Instance metadata can be used in XML, Java placeholders and expressions. The example below demonstrates the usage of
instance metadata inside an XML file using placeholders and also the expression referring to the special variable <code class="literal">environment</code></p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;bean</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">class</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"org.springframework.cloud.aws....SimpleConfigurationBean"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;property</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"value1"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">value</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"#{environment.ami-id}"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;property</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"value2"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">value</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"#{environment.hostname}"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;property</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"value3"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">value</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"${instance-type}"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;property</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"value4"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">value</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"${instance-id}"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/bean&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</span></pre><p>Instance metadata can also be injected with the Spring <code class="literal">org.springframework.beans.factory.annotation.Value</code> annotation
directly into Java fields. The next example demonstrates the use of instance metadata inside a Spring bean.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Component</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationInfoBean {
<em><span class="hl-annotation" style="color: gray">@Value("${ami-id:N/A}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String amiId;
<em><span class="hl-annotation" style="color: gray">@Value("${hostname:N/A}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String hostname;
<em><span class="hl-annotation" style="color: gray">@Value("${instance-type:N/A}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String instanceType;
<em><span class="hl-annotation" style="color: gray">@Value("${services/domain:N/A}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String serviceDomain;
}</pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>Every instance metadata can be accessed by the key available in the <a class="link" href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html" target="_top">instance metadata service</a>
Nested properties can be accessed by separating the properties with a slash ('/').</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_using_instance_user_data" href="#_using_instance_user_data"></a>3.1.5&nbsp;Using instance user data</h3></div></div></div><p>Besides the default instance metadata it is also possible to configure user data on each instance. This user data is retrieved and
parsed by Spring Cloud AWS. The user data can be defined while starting an EC2 instance with the application. Spring Cloud AWS
expects the format <code class="literal">&lt;key&gt;:&lt;value&gt;;&lt;key&gt;:&lt;value&gt;</code> inside the user data so that it can parse the string and extract the key value pairs.</p><p>The user data can be configured using either the management console shown below or a <a class="link" href="http://aws.amazon.com/cloudformation/aws-cloudformation-templates/" target="_top">CloudFormation template</a>.</p><div class="informalfigure"><div class="mediaobject"><img src="images/cloud-environment-user-data.png" alt="User data in the management console"></div></div><p>A CloudFormation template snippet for the configuration of the user data is outlined below:</p><pre class="programlisting">...
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Resources"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"ApplicationServerInstance"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Type"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"AWS::EC2::Instance"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Properties"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"ImageId"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"ami-6a56b81d"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"UserData"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Fn::Base64"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"data1:value1;data2:value2"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"InstanceType"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"t1.micro"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
...</pre><p>The user data can be accessed directly in the application context like the instance metadata through placeholders
or expressions.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Component</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> SecondConfigurationBean {
<em><span class="hl-annotation" style="color: gray">@Value("${data1}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String firstDataOption;
<em><span class="hl-annotation" style="color: gray">@Value("${data2}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String secondDataOption;
}</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_using_instance_tags" href="#_using_instance_tags"></a>3.1.6&nbsp;Using instance tags</h3></div></div></div><p>User configured properties can also be configured with tags instead of user data. Tags are a global concept in the context
of Amazon Web services and used in different services. Spring Cloud AWS supports instance tags also across different
services. Compared to user data, user tags can be updated during runtime, there is no need to stop and restart
the instance.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Tip"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="images/tip.png"></td><th align="left">Tip</th></tr><tr><td align="left" valign="top"><p><a class="link" href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html" target="_top">User data</a> can also be used to execute scripts
on instance startup. Therefore it is useful to leverage instance tags for user configuration and user data to execute scripts
on instance startup.</p></td></tr></table></div><p>Instance specific tags can be configured on the instance level through the management console outlined below and
like user data also with a CloudFormation template shown afterwards.</p><div class="informalfigure"><div class="mediaobject"><img src="images/cloud-environment-instance-tags.png" alt="Instance data in the management console"></div></div><p>A CloudFormation template snippet for the configuration of the instance tags is outlined below:</p><pre class="programlisting">...
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Resources"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"UserTagAndUserDataInstance"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Type"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"AWS::EC2::Instance"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Properties"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"ImageId"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"ami-6a56b81d"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"InstanceType"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"t1.micro"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Tags"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">[</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Key"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tag1"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Value"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tagv1"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Key"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tag3"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Value"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tagv3"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Key"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tag2"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Value"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tagv2"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">},</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">{</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Key"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tag4"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">,</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Value"</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"tagv4"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">]</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
...</pre><p>To retrieve the instance tags, Spring Cloud AWS has to make authenticated requests and therefore it will need the region
and security configuration before actually resolving the placeholders. Also because the instance tags are not available while starting
the application context, they can only be referenced as expressions and not with placeholders. The <code class="literal">context-instance-data</code>
element defines an attribute <code class="literal">user-tags-map</code> that will create a map in the application context for the name. This map
can then be queried using expression for other bean definitions.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;aws-context:context-instance-data</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">user-tags-map</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"instanceData"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</span></pre><p>A java bean might resolve expressions with the <code class="literal">@Value</code> annotation.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> SimpleConfigurationBean {
<em><span class="hl-annotation" style="color: gray">@Value("#{instanceData.tag1}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String value1;
<em><span class="hl-annotation" style="color: gray">@Value("#{instanceData.tag2}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String value2;
<em><span class="hl-annotation" style="color: gray">@Value("#{instanceData.tag3}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String value3;
<em><span class="hl-annotation" style="color: gray">@Value("#{instanceData.tag4}")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String value4;
}</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_configuring_custom_ec2_client" href="#_configuring_custom_ec2_client"></a>3.1.7&nbsp;Configuring custom EC2 client</h3></div></div></div><p>In some circumstances it is necessary to have a custom EC2 client to retrieve the instance information. The
<code class="literal">context-instance-data</code> element supports a custom EC2 client with the <code class="literal">amazon-ec2</code> attribute. The next
example shows the use of a custom EC2 client that might have a special configuration in place.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;beans</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;aws-context:context-credentials&gt;....&lt;/aws-context:context-credentials&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;aws-context:context-region</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">...</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"> /&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;aws-context:context-instance-data</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">amazon-ec2</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"myCustomClient"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">/&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;bean</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">id</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"myCustomClient"</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">class</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"com.amazonaws.services.ec2.AmazonEC2Client"</span><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&gt;</span>
...
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/bean&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/beans&gt;</span></pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_injecting_the_default_ec2_client" href="#_injecting_the_default_ec2_client"></a>3.1.8&nbsp;Injecting the default EC2 client</h3></div></div></div><p>If there are user tags configured for the instance data (see above) Spring Cloud AWS configures an EC2 client with
the specified region and security credentials. Application developers can inject the EC2 client directly into their code using the <code class="literal">@Autowired</code> annotation.</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationService {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">final</span> AmazonEC2 amazonEc2;
<em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> ApplicationService(AmazonEC2 amazonEc2) {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.amazonEc2 = amazonEc2;
}
}</pre></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__basic_setup.html">Prev</a>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;<a accesskey="n" href="multi__managing_cloud_environments.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.&nbsp;Basic setup&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud-aws.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;4.&nbsp;Managing cloud environments</td></tr></table></div></body></html>