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

85 lines
17 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>4.&nbsp;Managing cloud environments</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__cloud_environment.html" title="3.&nbsp;Cloud environment"><link rel="next" href="multi__messaging.html" title="5.&nbsp;Messaging"></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">4.&nbsp;Managing cloud environments</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__cloud_environment.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="multi__messaging.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="_managing_cloud_environments" href="#_managing_cloud_environments"></a>4.&nbsp;Managing cloud environments</h1></div></div></div><p>Managing environments manually with the management console does not scale and can become error-prone with the increasing
complexity of the infrastructure. Amazon Web services offers a <a class="link" href="http://aws.amazon.com/cloudformation/" target="_top">CloudFormation</a>
service that allows to define stack configuration templates and bootstrap the whole infrastructure with the services.
In order to allow multiple stacks in parallel, each resource in the stack receives a unique physical name that contains
some arbitrary generated name. In order to interact with the stack resources in a unified way Spring Cloud AWS allows
developers to work with logical names instead of the random physical ones.</p><p>The next graphics shows a typical stack configuration.</p><div class="informalfigure"><div class="mediaobject"><img src="images/cloudformation-overview.png" alt="CloudFormation overview"></div></div><p>The <span class="strong"><strong>Template File</strong></span> describes all stack resources with their <span class="emphasis"><em>logical name</em></span>. The <span class="strong"><strong>CloudFormation</strong></span> service parses the stack
template file and creates all resources with their <span class="emphasis"><em>physical name</em></span>. The application can use all the stack configured resources
with the <span class="emphasis"><em>logical name</em></span> defined in the template. Spring Cloud AWS resolves all <span class="emphasis"><em>logical names</em></span> into the respective
<span class="emphasis"><em>physical name</em></span> for the application developer.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_automatic_cloudformation_configuration" href="#_automatic_cloudformation_configuration"></a>4.1&nbsp;Automatic CloudFormation configuration</h2></div></div></div><p>If the application runs inside a stack (because the underlying EC2 instance has been bootstrapped within the stack), then
Spring Cloud AWS will automatically detect the stack and resolve all resources from the stack. Application developers
can use all the logical names from the stack template to interact with the services. In the example below, the database
resource is configured using a CloudFormation template, defining a logical name for the database instance.</p><pre class="programlisting">"applicationDatabase": {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"AllocatedStorage": "5",
"DBInstanceClass": "db.t1.micro",
"DBName": "test"
...
]
}
}</pre><p>The datasource is then created and will receive a physical name (e.g. ir142c39k6o5irj) as the database service name. Application
developers can still use the logical name (in this case <code class="literal">applicationDatabase</code>) to interact with the database. The example
below shows the stack configuration which is defined by the element <code class="literal">aws-context:stack-configuration</code> and resolves automatically
the particular stack. The <code class="literal">data-source</code> element uses the logical name for the <code class="literal">db-instance-identifier</code> attribute to work with
the database.</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">xmlns:xsi</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xmlns:aws-context</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/cloud/aws/context"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xmlns</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/beans"</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">xsi:schemaLocation</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"http://www.springframework.org/schema/cloud/aws/context
http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd"</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-credentials&gt;</span>
...
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/aws-context:context-credentials&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&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:stack-configuration/&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;jdbc:data-source</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">db-instance-identifier</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"applicationDatabase"</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;/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>Further detailed information on the Amazon RDS configuration and setup can be found in the respective chapter in this
documentation.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_manual_cloudformation_configuration" href="#_manual_cloudformation_configuration"></a>4.2&nbsp;Manual CloudFormation configuration</h2></div></div></div><p>If the application is not running inside a stack configured EC2 instance, then the stack configuration must be configured
manually. The configuration consists of an additional element attribute <code class="literal">stack-name</code> that will be used to resolve all the
respective stack configuration information at runtime.</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">...</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;aws-context:stack-configuration</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">stack-name</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"myStackName"</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><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_cloudformation_configuration_with_java_config_classes" href="#_cloudformation_configuration_with_java_config_classes"></a>4.3&nbsp;CloudFormation configuration with Java config classes</h2></div></div></div><p>Spring Cloud AWS also supports the configuration of the CloudFormation support within Java classes avoiding the use of
XML inside the application configuration. Spring Cloud AWS provides the annotation
<code class="literal">og.springframework.cloud.aws.context.config.annotation.EnableStackConfiguration</code> that allows the automatic and manual
stack configuration. The next example shows a configuration class that configures the CloudFormation support with an
explicit stack name (here <code class="literal">manualStackName</code>).</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
<em><span class="hl-annotation" style="color: gray">@EnableStackConfiguration(stackName = "manualStackName")</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> ApplicationConfiguration {
}</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>Do not define the <code class="literal">stackName</code> attribute if an automatic stack name should be enabled.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_cloudformation_configuration_in_spring_boot" href="#_cloudformation_configuration_in_spring_boot"></a>4.4&nbsp;CloudFormation configuration in Spring Boot</h2></div></div></div><p>Spring Cloud AWS also supports the configuration of the CloudFormation support within the Spring Boot configuration. The
manual and automatic stack configuration can be defined with properties that are described in the table below.</p><div class="informaltable"><table style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; border-left: 0.5pt solid ; border-right: 0.5pt solid ; "><colgroup><col class="col_1"><col class="col_2"><col class="col_3"></colgroup><thead><tr><th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left" valign="top">property</th><th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left" valign="top">example</th><th style="border-bottom: 0.5pt solid ; " align="left" valign="top">description</th></tr></thead><tbody><tr><td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left" valign="top"><p>cloud.aws.stack.name</p></td><td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left" valign="top"><p>myStackName</p></td><td style="border-bottom: 0.5pt solid ; " align="left" valign="top"><p>The name of the manually configured stack name that will be used to retrieve the resources.</p></td></tr><tr><td style="border-right: 0.5pt solid ; " align="left" valign="top"><p>cloud.aws.stack.auto</p></td><td style="border-right: 0.5pt solid ; " align="left" valign="top"><p>true</p></td><td style="" align="left" valign="top"><p>Enables the automatic stack name detection for the application.</p></td></tr></tbody></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_manual_name_resolution" href="#_manual_name_resolution"></a>4.5&nbsp;Manual name resolution</h2></div></div></div><p>Spring Cloud AWS uses the CloudFormation stack to resolve all resources internally using the logical names. In some circumstances
it might be needed to resolve the physical name inside the application code. Spring Cloud AWS provides a pre-configured
service to resolve the physical stack name based on the logical name. The sample shows a manual stack resource resolution.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Service</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> 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> ResourceIdResolver resourceIdResolver;
<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(ResourceIdResolver resourceIdResolver) {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.resourceIdResolver = resourceIdResolver;
}
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> handleApplicationLogic() {
String physicalBucketName =
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.resourceIdResolver.resolveToPhysicalResourceId(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"someLogicalName"</span>);
}
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_stack_tags" href="#_stack_tags"></a>4.6&nbsp;Stack Tags</h2></div></div></div><p>Like for the Amazon EC2 instances, CloudFormation also provides stack specific tags that can be used to
configure stack specific configuration information and receive them inside the application. This can for example be a
stage specific configuration property (like DEV, INT, PRD).</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">...</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">&lt;aws-context:stack-configuration</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">"stackTags"</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>The application can then access the stack tags with an expression like <code class="literal">#{stackTags.key1}</code>.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_using_custom_cloudformation_client" href="#_using_custom_cloudformation_client"></a>4.7&nbsp;Using custom CloudFormation client</h2></div></div></div><p>Like for the EC2 configuration setup, the <code class="literal">aws-context:stack-configuration</code> element supports a custom CloudFormation client
with a special setup. The client itself can be configured using the <code class="literal">amazon-cloud-formation</code> attribute as shown in the example:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;beans&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;aws-context:stack-configuration</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">amazon-cloud-formation</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">""</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">class</span>=<span xmlns:d="http://docbook.org/ns/docbook" class="hl-value">"com.amazonaws.services.cloudformation.AmazonCloudFormationClient"</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><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__cloud_environment.html">Prev</a>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;<a accesskey="n" href="multi__messaging.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.&nbsp;Cloud environment&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;5.&nbsp;Messaging</td></tr></table></div></body></html>