<?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: Robin Thomas</title>
    <description>The latest articles on DEV Community by Robin Thomas (@robinthomas_30).</description>
    <link>https://dev.to/robinthomas_30</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%2F1140673%2Fdb8340b7-db0b-4691-8564-97113d83adf6.jpeg</url>
      <title>DEV Community: Robin Thomas</title>
      <link>https://dev.to/robinthomas_30</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robinthomas_30"/>
    <language>en</language>
    <item>
      <title>Using Secrets in Serverless</title>
      <dc:creator>Robin Thomas</dc:creator>
      <pubDate>Sun, 17 Sep 2023 23:26:57 +0000</pubDate>
      <link>https://dev.to/robinthomas_30/using-secrets-in-serverless-50hf</link>
      <guid>https://dev.to/robinthomas_30/using-secrets-in-serverless-50hf</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;All you need to do is to create a &lt;code&gt;serverless.yml&lt;/code&gt; file, with some default configurations, and a deployment user all set up, and you shall be deploying in no time.&lt;/p&gt;

&lt;p&gt;Now imagine that you need to store credentials like MySQL user and password.&lt;/p&gt;

&lt;p&gt;You can store them in &lt;code&gt;.env.*&lt;/code&gt; files, and serverless can easily export them as environment variables for your AWS lambdas.&lt;/p&gt;

&lt;p&gt;But should you be doing so?!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--38e6Yxhr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujovw2heg7ob7mrye8w0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--38e6Yxhr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujovw2heg7ob7mrye8w0.jpg" alt="Don't expose your secrets publicly" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@luthermeb"&gt;Luther.M.E. Bottrill&lt;/a&gt; on &lt;a href="https://unsplash.com/"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;NO!!&lt;/p&gt;

&lt;p&gt;Why is that?!&lt;/p&gt;

&lt;p&gt;Because that means revealing your secrets in your GitHub repositories (or some other platforms) publicly (&lt;em&gt;if your repo is private/internal, then no issue&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;You can use GitHub secrets (&lt;em&gt;which are not revealed publicly in your GitHub repositories&lt;/em&gt;), but there is no easy integration with serverless. Moreover, if you are already using AWS (with serverless), chances are, you might be using AWS Secrets Manager already.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t85zslSp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h63v99mseusr6ccswako.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t85zslSp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h63v99mseusr6ccswako.jpg" alt="AWS Secrets Manager" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@tinaflour"&gt;Kristina Flour&lt;/a&gt; on &lt;a href="https://unsplash.com/"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Does serverless support this?
&lt;/h2&gt;

&lt;p&gt;Serverless does natively support AWS Secrets Manager in &lt;code&gt;serverless.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;You can use a configuration like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;custom:
  secret: ${ssm:/path/to/secureparam}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So that means you need to define a &lt;code&gt;custom.secret&lt;/code&gt; attribute, JSON parse it, and then write all the environment variables (that are used by AWS Lambdas) in &lt;code&gt;serverless.yml&lt;/code&gt; file referencing this secret.&lt;/p&gt;

&lt;p&gt;Seems like a lot of work.&lt;/p&gt;

&lt;p&gt;Any plugin for this?!&lt;/p&gt;

&lt;h2&gt;
  
  
  Plugin time
&lt;/h2&gt;

&lt;p&gt;I went through all serverless plugins that had remotely anything to do with AWS Secrets Manager.&lt;/p&gt;

&lt;p&gt;But I found none that served my use-cases. Which were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allows me to integrate directly with environment variables&lt;/li&gt;
&lt;li&gt;I'm already using &lt;code&gt;.env.*&lt;/code&gt; files. I don't want to redefine these variables in serverless.yml&lt;/li&gt;
&lt;li&gt;I need the plugin to run during all the serverless lifecycle hooks I want&lt;/li&gt;
&lt;li&gt;I need the secret integration to happen during build stage (and not during runtime)&lt;/li&gt;
&lt;li&gt;Easily determine the secrets from &lt;code&gt;.env.*&lt;/code&gt; files. Can use a prefix search for a keyword like: &lt;code&gt;secret:&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Works without much plugin configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, I found none.&lt;/p&gt;

&lt;p&gt;So I went ahead and decided to create a serverless plugin myself. Plugin time!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nU3G1IHl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wokp861inghvr7zzdn4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nU3G1IHl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wokp861inghvr7zzdn4.jpg" alt="Serverless AWS Secrets plugin" width="800" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Photo by &lt;a href="https://unsplash.com/@techivation"&gt;Techivation&lt;/a&gt; on &lt;a href="https://unsplash.com/"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Hello, Plugin!
&lt;/h2&gt;

&lt;p&gt;Without much further ado, here is my plugin: &lt;a href="https://github.com/robin-thomas/serverless-aws-secrets"&gt;https://github.com/robin-thomas/serverless-aws-secrets&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Serves all my use-cases, and more. Feedbacks are welcome.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Show me some love by starring the project on GitHub!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you like to contribute, take a look at some of the open issues!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
