<?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: Seifolah Ghaderi</title>
    <description>The latest articles on DEV Community by Seifolah Ghaderi (@seifolahghaderi).</description>
    <link>https://dev.to/seifolahghaderi</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%2F575630%2F36ead116-c885-4dd6-aa9a-90148dfd1d87.jpg</url>
      <title>DEV Community: Seifolah Ghaderi</title>
      <link>https://dev.to/seifolahghaderi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/seifolahghaderi"/>
    <language>en</language>
    <item>
      <title>Aggregation query in Cosmos DB</title>
      <dc:creator>Seifolah Ghaderi</dc:creator>
      <pubDate>Wed, 26 Jun 2024 17:40:40 +0000</pubDate>
      <link>https://dev.to/seifolahghaderi/aggregation-query-in-cosmos-db-366b</link>
      <guid>https://dev.to/seifolahghaderi/aggregation-query-in-cosmos-db-366b</guid>
      <description>&lt;p&gt;Aggregation Queries in Cosmos DB with Ternary: A Workaround for Performance Concerns&lt;br&gt;
Due to potential performance issues, I understand it's not typical to write aggregation queries like SUM and AVG on NoSQL databases. However, sometimes it's necessary to find a workaround for temporary situations. With my extensive experience in database development and providing SQL reports in operational and data warehouse databases, I know how useful aggregation queries can be.&lt;/p&gt;

&lt;p&gt;In my special case, I needed a combination of SUM and CASE, common in Oracle and SQL Server databases. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(CASE WHEN status = 'approved' THEN 1 ELSE 0 END) AS approved FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, Cosmos DB does not support the CASE operation. Instead, it introduces the &lt;code&gt;Ternary&lt;/code&gt; operator. &lt;code&gt;Ternary&lt;/code&gt; works like &lt;code&gt;iif&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;bool_expr&amp;gt; ?  &lt;br&gt;
    &amp;lt;expr_true&amp;gt; : &lt;br&gt;
    &amp;lt;expr_false&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here's how I constructed my query to get the sum of orders for today and this month:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
    SUM((c.orderDate &amp;gt; '{yesterday:O}' AND c.orderDate &amp;lt;= '{today:O}') ? c.price : 0) AS todayPrice,
    SUM((c.orderDate &amp;gt;= '{thisMonthStart:O}' AND c.orderDate &amp;lt; '{thisMonthStart.AddMonths(1):O}') ? c.price : 0) AS thisMonthPrice
FROM c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ternary Operator: Used to replace the CASE statement in Cosmos DB.&lt;/li&gt;
&lt;li&gt;todayPrice: Sums the prices of orders placed today.&lt;/li&gt;
&lt;li&gt;thisMonthPrice: Sums the prices of orders placed this month.
I eliminate real date calue but you can provide them if you need a direct query or use placeholders if you run the code from an API.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>SSH keep Alive !</title>
      <dc:creator>Seifolah Ghaderi</dc:creator>
      <pubDate>Mon, 06 Mar 2023 13:50:29 +0000</pubDate>
      <link>https://dev.to/seifolahghaderi/ssh-keep-alive--48ae</link>
      <guid>https://dev.to/seifolahghaderi/ssh-keep-alive--48ae</guid>
      <description>&lt;p&gt;Usualy we need a live connection when working with &lt;strong&gt;ssh&lt;/strong&gt; .&lt;br&gt;
Here is short description. Note you can use &lt;strong&gt;'*'&lt;/strong&gt; to apply all hosts:&lt;br&gt;
&lt;code&gt;mkdir -p ~/.ssh&lt;/code&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cat &amp;gt; ~/.ssh/config &amp;lt;&amp;lt; EOF 
Host host_or_ip
ServerAliveInterval 120 
EOF 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>ssh</category>
      <category>linux</category>
      <category>terminal</category>
    </item>
    <item>
      <title>Terraform aws codebuild : embed buildspec.yml</title>
      <dc:creator>Seifolah Ghaderi</dc:creator>
      <pubDate>Mon, 30 Aug 2021 06:35:37 +0000</pubDate>
      <link>https://dev.to/seifolahghaderi/terraform-aws-codebuild-embed-buildspec-yml-1ci0</link>
      <guid>https://dev.to/seifolahghaderi/terraform-aws-codebuild-embed-buildspec-yml-1ci0</guid>
      <description>&lt;p&gt;If you're working with aws and using terraform for ci/cd automation, you have the option to work with CodePipeline and CodeBuild. In CodeBuild you should define your buildspec.yml file as a command list to make your build process.&lt;br&gt;
You have two options: first, read the buildspec.yml file from the source and the second is to use an embedded file.&lt;/p&gt;

&lt;p&gt;Using the second option when using terraform has many advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you are using a single file for many pipelines&lt;/li&gt;
&lt;li&gt;changing and maintenance is easy &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had to do this for some of my projects. I had several repos with the same technology and pipeline read &lt;code&gt;buildspec.yml&lt;/code&gt; from code. Later we had to do some changes and I was forced to modify all of them.I decided to embed &lt;code&gt;buildspec.yml&lt;/code&gt; file in my terraform and manage it using parameters.&lt;/p&gt;

&lt;p&gt;The first step was to load &lt;code&gt;buildspec.yml&lt;/code&gt; from local and as my previous experiences i tried to use &lt;code&gt;template_file&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;data "template_file" "buildspec" {
  template = "${file("${path.module}/buildspec.yml")}"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But it failed!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Error: failed to render : &amp;lt;template_file&amp;gt;:16,32-33: Extra characters after interpolation expression; Expected a closing brace to end the interpolation expression, but found extra characters.
│
│   with module.pipeline.data.template_file.buildspec,
│   on ../../../../modules/cicd_ecs_github_embed/main.tf line 172, in data "template_file" "buildspec":
│  172: data "template_file" "buildspec" {
│
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used it before for loading CodeBuild Policy and CodePipleine policy templates. Anyway, by searching more I found &lt;code&gt;template_file&lt;/code&gt; use to see files as a template and except some placeholder like &lt;code&gt;${var}&lt;/code&gt; to fill it. and my file has some &lt;code&gt;$var&lt;/code&gt; placeholder that would be filled from CodeBuild!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution 1: use local_file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;you can ignore using template_file for this step if you dont want to pass any params to it at this stage.&lt;/p&gt;

&lt;p&gt;So I moved to use another terraform object &lt;code&gt;local_file&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;data "local_file" "buildspec_local" {
    filename = "${path.module}/buildspec.yml.tmpl"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then in my CodeBuild :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source {
    buildspec           = data.local_file.buildspec_local.content
    git_clone_depth     = 0
    insecure_ssl        = false
    report_build_status = false
    type                = "CODEPIPELINE"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;solution 2: escape ${} in buildspec&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you really want to pass some dynamic parameters in your &lt;code&gt;buildspec.yml&lt;/code&gt; file , you should escape &lt;code&gt;${var:option}&lt;/code&gt; parameters. because terraform expect all variable in simple &lt;code&gt;${var}&lt;/code&gt; format ,when you're using some special tag like &lt;code&gt;IMAGE_TAG=${COMMIT_HASH:=latest}&lt;/code&gt; it gives error, so escape this parameter by double $ character s below :&lt;br&gt;
&lt;code&gt;- IMAGE_TAG=$${COMMIT_HASH:=latest}&lt;/code&gt;&lt;br&gt;
Now you can pass your desired variables in template file and escape CodeBuild specific variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data "template_file" "buildspec" {
  template = "${file("${path.module}/buildspec.yml")}"
  vars = {
  #feed dynamic variables I need 
  ps_asset_s3_bucket="${var.ps_asset_s3_bucket_key}"
  ...
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then load it in &lt;code&gt;source&lt;/code&gt; section of your code build&lt;br&gt;
I hope this experience be useful for you.&lt;/p&gt;

</description>
      <category>terraform</category>
      <category>aws</category>
    </item>
    <item>
      <title>aws codebuild continues on failure</title>
      <dc:creator>Seifolah Ghaderi</dc:creator>
      <pubDate>Wed, 21 Apr 2021 10:32:51 +0000</pubDate>
      <link>https://dev.to/seifolahghaderi/aws-codebuild-continues-on-failure-d2i</link>
      <guid>https://dev.to/seifolahghaderi/aws-codebuild-continues-on-failure-d2i</guid>
      <description>&lt;p&gt;Suppose you have some buildspec.yml file like this that build your project and upload it on some where :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build:
     commands:
       - ng build --configuration=$BUILD_ENV  
post_build:
     commands:
       - aws s3 rm s3://$ENV_BUCKET  --recursive
       - aws s3 cp dist s3://$ENV_BUCKET  --recursive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the Problem here is that if build step fails codebuild continue and  &lt;em&gt;post_build&lt;/em&gt; will be executed and &lt;code&gt;aws s3 rm&lt;/code&gt; will clean your bucket !&lt;/p&gt;

&lt;p&gt;I had this issue in my aws codepipeline project (fortunately none-production env) and tried to fixing it.&lt;/p&gt;

&lt;p&gt;So here is workaround:&lt;br&gt;
&lt;code&gt;on-failure: ABORT&lt;/code&gt;&lt;br&gt;
just put in build step  . i tried it and it works like a charm !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;build:
     on-failure: ABORT
     commands:
       - ng build --configuration=$BUILD_ENV  
post_build:
     commands:
       - aws s3 rm s3://$ENV_BUCKET  --recursive
       - aws s3 cp dist s3://$ENV_BUCKET  --recursive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second solution is to add another Deploy Stage in aws code pipeline and do deploy action (cp to s3 bucket ) at there .&lt;/p&gt;

</description>
      <category>aws</category>
      <category>codebuild</category>
      <category>codepipeline</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
