<?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: Suparn</title>
    <description>The latest articles on DEV Community by Suparn (@suparnatural).</description>
    <link>https://dev.to/suparnatural</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%2F510525%2F811f7027-8c68-41c9-988f-3c5c2eae9c34.jpeg</url>
      <title>DEV Community: Suparn</title>
      <link>https://dev.to/suparnatural</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suparnatural"/>
    <language>en</language>
    <item>
      <title>AWS Elasticsearch Access with Serverless Lambda</title>
      <dc:creator>Suparn</dc:creator>
      <pubDate>Sun, 08 Nov 2020 18:39:02 +0000</pubDate>
      <link>https://dev.to/suparnatural/aws-elasticsearch-with-serverless-lambda-2m9b</link>
      <guid>https://dev.to/suparnatural/aws-elasticsearch-with-serverless-lambda-2m9b</guid>
      <description>&lt;p&gt;Elasticsearch is a well known search solution and AWS offers a &lt;a href="https://aws.amazon.com/elasticsearch-service/"&gt;fully-managed service&lt;/a&gt; for it. The managed service has the exact same API to interact with just like an unmanaged cluster which is great because you can use all the available tooling as it is.&lt;/p&gt;

&lt;p&gt;There are a couple of different options when it comes to deploying a business service backed by &lt;a href="http://elastic.co/"&gt;Elasticsearch&lt;/a&gt;. For example,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can expose the ES endpoint directly so that clients can invoke the ES rest API.&lt;/li&gt;
&lt;li&gt;Create a business centric API around ES and expose it as the only way to consume ES service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Which approach is better than the other depends on your needs. Additionally, AWS ES offers Kibana to visualize your search data. To protect Kibana from unauthorized access, you can integrate it with Cognito. Once you do that, only authenticated users from your Cognito User Pool will be able to access the Kibana Dashboard.&lt;/p&gt;

&lt;p&gt;In my case, I wanted to deploy an ES domain on AWS with lambda and API Gateway in front. And I am using &lt;a href="https://www.serverless.com/"&gt;Serverless Framework&lt;/a&gt; to manage my lambda functions. Additionally, I wanted to secure the access to Kibana using Cognito User Pools. When I first started with this, I face a lot of challenges to set up my ES domain in AWS with proper access policies. Merely defining an &lt;a href="https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-identity"&gt;Identity Based Policy&lt;/a&gt; did not work.  There is a great article by &lt;a href="https://aws.amazon.com/blogs/database/author/handler/"&gt;Jon Handler&lt;/a&gt; on &lt;a href="https://aws.amazon.com/blogs/database/get-started-with-amazon-elasticsearch-service-use-amazon-cognito-for-kibana-access-control/"&gt;Getting started with Elasticsearch and Cognito&lt;/a&gt; which talks about a lot of things in great detail. However, there were still some missing pieces given the limited knowledge I had at the time. This post outlines some of the concepts which may help you understand what makes Elasticsearch access control different than other &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html"&gt;IAM policies&lt;/a&gt; you are used to defining in your &lt;code&gt;serverless.yml&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resource vs Identity Based Policy
&lt;/h2&gt;

&lt;p&gt;This is one of the most important concepts I think, which you’d have to understand before you configure Elasticsearch Access Policy.&lt;/p&gt;

&lt;p&gt;A &lt;a href="https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-resource"&gt;&lt;em&gt;Resource Based Policy&lt;/em&gt;&lt;/a&gt; is attached to the resource which you are trying to secure. That means, you have to know upfront &lt;strong&gt;who&lt;/strong&gt; can access the resource. The &lt;em&gt;who&lt;/em&gt; may mean many different things depending on what you want to do. For example, you can allow everything under one &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/console_account-alias.html"&gt;AWS Account ID&lt;/a&gt; to have access to ES.&lt;/p&gt;

&lt;p&gt;An &lt;a href="https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-types-identity"&gt;&lt;em&gt;Identity Based Policy&lt;/em&gt;&lt;/a&gt; is attached to the entity accessing your resource. For example an indexing lambda function to insert data in Elasticsearch. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The main difference between the two is that for the &lt;em&gt;Resource Based Policy&lt;/em&gt;, you have to plan ahead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the many ways you can do this is by &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html"&gt;creating an IAM Role&lt;/a&gt;, assigning it to your lambda and granting it access in the ES Access Policy. So the policy document may look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:sts::400176810461:...ROLE ARN"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:us-east-1:400176810461:domain/my-es-domain/*"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/intro-structure.html#intro-structure-principal"&gt;￼&lt;code&gt;Principal&lt;/code&gt;￼&lt;/a&gt; key here. It means that we are specifically telling Elasticsearch to allow all access to everyone who is assigned this role.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial plan
&lt;/h2&gt;

&lt;p&gt;In my first iteration, I planned this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create two separate roles (one for read and other for read and write). The actions in the role don’t matter (I think) because ES will override those.&lt;/li&gt;
&lt;li&gt;Add these additional roles to my &lt;code&gt;serverless.yml&lt;/code&gt; and attach it to functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But I couldn’t find a way to keep the default roles which &lt;em&gt;Serverless&lt;/em&gt; creates and add this additional role. So that plan failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second Iteration
&lt;/h2&gt;

&lt;p&gt;Then I created an access policy in Elasticsearch which allowed access to everything under my account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:root"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:us-east-1:400176810461:domain/my-es-domain/*"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this was still not enough as I kept getting error that the lambda didn’t have access to invoke &lt;code&gt;es:HttpGet&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making it work with Serverless
&lt;/h2&gt;

&lt;p&gt;I finally found the reason why my second attempt failed in the &lt;a href="https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-ac.html#es-ac-conflict"&gt;this section of Identity and Access Management document for Elasticsearch&lt;/a&gt;. Essentially, if you have a &lt;em&gt;Resource Based Policy&lt;/em&gt; which allows access to everything under the account, you must also create an &lt;em&gt;Identity Based Policy&lt;/em&gt; for the entity accessing the service. That means, in addition to my second attempt, I also had to configure the &lt;code&gt;serverless.yml&lt;/code&gt; with the following block under &lt;code&gt;iamRoleStatements&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Effect: Allow
  Action:
    - es:*
  Resource:
    - arn:aws:es:us-east-1:400176810461:domain/my-es-domain/*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Signing the requests
&lt;/h2&gt;

&lt;p&gt;As you know, a client (lambda in this case) talks to Elasticsearch via REST APIs. That means, the requests don't go internally in AWS (like calling a lambda via ARN). So how does ES differentiate between a request coming from your lambda vs a request from some other source (because they are both HTTP).&lt;/p&gt;

&lt;p&gt;Well, once you allow your lambda to access the Elasticsearch instance, you must sign the HTTP requests with AWS V4 signing as well. Otherwise the request will appear as if it is coming from an unauthorized user. To sign a request in NodeJS, checkout the following two libraries.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/compwright/aws-elasticsearch-connector"&gt;aws-elasticsearch-connector&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mhart/aws4"&gt;aws4&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Configuring Cognito
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://aws.amazon.com/blogs/database/get-started-with-amazon-elasticsearch-service-use-amazon-cognito-for-kibana-access-control/"&gt;Getting started with Elasticsearch and Cognito&lt;/a&gt; article mentioned before has a step by step guide to configure Cognito for Kibana. However, I found somethings which didn’t work for me:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I didn’t see the need for checking “Enable access to unauthenticated identities”. Because I don’t fully understand the &lt;a href="https://docs.aws.amazon.com/cognito/latest/developerguide/identity-pools.html"&gt;Cognito Identity Pools&lt;/a&gt; yet, this seemed dangerous. The permissions that you assign to the &lt;code&gt;Unauth_Role&lt;/code&gt; don’t matter to ES because the ES access policy supersedes. This is only needed (I think) if you want to create a dashboard in Kibana which is accessible without authentication.&lt;/li&gt;
&lt;li&gt;If you follow the steps in “Change the Principal to the ARN for the assumed Auth role.”, it will allow Kibana access to only the Identity Pool users. You can always add additional role here but then you may run into the same problem as my attempt 1 about how to assign this extra role to your lambda.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create an access policy with a &lt;em&gt;Principal&lt;/em&gt; value which allows everything under your account to access Elasticsearch.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:root"
      },
      "Action": "es:*",
      "Resource": "arn:aws:es:us-east-1:400176810461:domain/my-es-domain/*"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Assign necessary permissions to your lambda in the &lt;code&gt;serverless.yml&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Effect: Allow
  Action:
    - es:*
  Resource:
    - arn:aws:es:us-east-1:400176810461:domain/my-es-domain/*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sign your request with V4 signing.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>elasticsearch</category>
      <category>serverless</category>
      <category>cognito</category>
    </item>
  </channel>
</rss>
