<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brett Andrews</title>
    <description>The latest articles on DEV Community by Brett Andrews (@brettstack).</description>
    <link>https://dev.to/brettstack</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F347075%2Fcbd4bb6f-17f2-4085-b36c-5b9ef1466434.png</url>
      <title>DEV Community: Brett Andrews</title>
      <link>https://dev.to/brettstack</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brettstack"/>
    <language>en</language>
    <item>
      <title>Configuring Serverless Framework for multiple stages</title>
      <dc:creator>Brett Andrews</dc:creator>
      <pubDate>Fri, 20 Mar 2020 22:55:28 +0000</pubDate>
      <link>https://dev.to/brettstack/configuring-serverless-framework-for-multiple-stages-218a</link>
      <guid>https://dev.to/brettstack/configuring-serverless-framework-for-multiple-stages-218a</guid>
      <description>&lt;p&gt;&lt;a href="https://aws.amazon.com/cloudformation/"&gt;AWS CloudFormation&lt;/a&gt; lets you define "variables" in your templates by specifying &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html"&gt;Parameters&lt;/a&gt; along with a &lt;code&gt;Default&lt;/code&gt; value. However, they're limited to static values. That is, you can't provide dynamic values based on stage values or other inputs&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; &lt;a href="https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html"&gt;use dynamic values stored in other AWS services within your CloudFormation template&lt;/a&gt;, but that's a little different.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of my favorite &lt;a href="https://serverless.com/"&gt;Serverless Framework&lt;/a&gt; features is its &lt;code&gt;custom&lt;/code&gt; section. Not only does it let you specify static values and values based on inputs, but it also enables you to compose variables and even create maps so that we can have different values based on stage/environment.&lt;/p&gt;

&lt;p&gt;Here's a simplified serverless.yaml showing how I configure and use environment-specific values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;profile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:custom.stages.${self:provider.stage}.profile}&lt;/span&gt;

&lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;dev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;profile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;halfstack_software_dev&lt;/span&gt;
      &lt;span class="na"&gt;domainEnabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="na"&gt;staging&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;profile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;halfstack_software_staging&lt;/span&gt;
      &lt;span class="na"&gt;domainEnabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
      &lt;span class="na"&gt;domain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;staging.halfstack.software&lt;/span&gt;
    &lt;span class="na"&gt;prod&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;profile&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;halfstack_software_prod&lt;/span&gt;
      &lt;span class="na"&gt;domainEnabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
      &lt;span class="na"&gt;domain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;halfstack.software&lt;/span&gt;
  &lt;span class="na"&gt;domainName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:custom.stages.${self:provider.stage}.domain}&lt;/span&gt;
  &lt;span class="na"&gt;domainEnabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:custom.stages.${self:provider.stage}.domainEnabled}&lt;/span&gt;

&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Conditions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;UseDomainName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="kt"&gt;!Equals&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;${self:custom.domainEnabled}&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To access a stage-specific value, I use &lt;code&gt;${self:custom.stages.${self:provider.stage}.domainName}&lt;/code&gt; -- quite wordy. To make this more accessible throughout the template, I often duplicate the value into a second variable so that I can access it via &lt;code&gt;${self:custom.domainName}&lt;/code&gt;. Annoying, but manageable.&lt;/p&gt;

&lt;p&gt;You can see the example above also includes a &lt;code&gt;profile&lt;/code&gt; config for each stage. These all have an accompanying profile entry in &lt;code&gt;~/.aws/credentials&lt;/code&gt; that lets me easily &lt;a href="https://aws.amazon.com/answers/account-management/aws-multi-account-billing-strategy/"&gt;deploy to different AWS accounts based on stage&lt;/a&gt;. Keep in mind that for any production system that's receiving traffic, &lt;strong&gt;it's dangerous to have production profiles on your local development machines&lt;/strong&gt; lest you accidentally deploy to production (plus, I hear it's a security risk? 🤷‍♂️).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# ~/.aws/credentials
[halfstack_software_dev]
aws_access_key_id = ABC123DEF456GHI789JK
aws_secret_access_key = aB1Cd2aB1Cd2aB1Cd2aB1Cd2+73f8+nWFQ
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One alternative for stage-specific values is the &lt;a href="https://www.npmjs.com/package/serverless-dotenv-plugin"&gt;serverless-dotenv-plugin&lt;/a&gt; package. This plugin even allows you to create files like &lt;code&gt;.env.development&lt;/code&gt; and &lt;code&gt;.env.production&lt;/code&gt; and it automatically uses the appropriate file when running &lt;code&gt;NODE_ENV=production sls deploy&lt;/code&gt;. 👍&lt;/p&gt;

&lt;p&gt;My personal preference is to store my non-secret values in serverless.yaml and resort to &lt;code&gt;.env&lt;/code&gt; for secrets only (even better, &lt;a href="https://aws.amazon.com/secrets-manager/"&gt;Use AWS Secrets Manager&lt;/a&gt; 🔒). This approach makes it easier for developers to set up the project on their machine (they don't need to create a &lt;code&gt;.env&lt;/code&gt; file), and grants you version control over these values since it's checked into your source code repository.&lt;/p&gt;

&lt;p&gt;Have any other tips for multi-stage deployments? Comment below or @ me on &lt;a href="https://twitter.com/AWSbrett"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>aws</category>
      <category>cloudformation</category>
      <category>amplify</category>
    </item>
  </channel>
</rss>
