<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tarlan Huseynov on Medium]]></title>
        <description><![CDATA[Stories by Tarlan Huseynov on Medium]]></description>
        <link>https://medium.com/@tarlan_59778?source=rss-7d0345c6fa0d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9xgDyEm6gPMu9g2auTA6lA.jpeg</url>
            <title>Stories by Tarlan Huseynov on Medium</title>
            <link>https://medium.com/@tarlan_59778?source=rss-7d0345c6fa0d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 03:33:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tarlan_59778/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[One tool to rule them all — Terraform: EKS Golang Client & E2E AWS Lambda CI/CD via IaC]]></title>
            <link>https://blog.stackademic.com/one-tool-to-rule-them-all-terraform-eks-golang-client-e2e-aws-lambda-ci-cd-via-iac-6e9cf1a14a51?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e9cf1a14a51</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Tue, 09 Jul 2024 08:25:29 GMT</pubDate>
            <atom:updated>2024-07-11T00:06:50.481Z</atom:updated>
            <content:encoded><![CDATA[<h3>One tool to rule them all — Terraform: EKS Golang Client &amp; E2E AWS Lambda CI/CD via IaC</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*zWq0NuwPs1v_nozgKLNFxw.png" /></figure><p>Hey Folks! In this article, we delve into the seamless integration of Terraform for deploying compiled Lambda functions. During the process I will showcase how AWS Lambda can be effectively used as a Kubernetes client. And as final layer of our sandwich — E2E Terraform CI/CD with GitHub Actions to facilitate continuous infrastructure provisioning and software delivery all in one!</p><p>So, here is some backstory. Previously, I wondered how to facilitate a more reactive and event driven interaction with my EKS cluster in a cloud-native environment. And naturally when I think of event-driven approach in AWS context — lambdas and EventBridge are one of the first things that come to mind, and for a good reason 😁 right? Combination of these 2 can provide endless number of solutions for various problems — where an action is needed based on some event or some specific schedule.<br> We will be looking at one of them, particularly — “How to scale down EKS workloads on a scheduled basis”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/0*4ryYFMLgooEL1JYE.png" /></figure><p>We will be using my humble EKS-downscaler app. As a client, it provides similar functionality to an <a href="https://codeberg.org/hjacobs/kube-downscaler">operator with the same name</a>. This app is conceptual and serves as an example of how AWS Lambdas can interact with Kubernetes. With our solution, we will specify namespaces and cron expressions, and Terraform coupled with downscaler lambda will handle everything. This article will showcase Terraform from a perfect angle — as it will be our main tool to manage continuous configuration changes, provision and manage cloud resources, and deliver seamless code-to-Lambda deployment. This approach can be used as a standalone terraform project or integrated into more complex IaC projects by passing inputs directly (e.g., “var.cluster_name” that is required as input could have been passed via remote state or directly from root/child module where we create the eks cluster).</p><h4>Prerequisites</h4><p>As you might have guessed, we have some prerequisites:</p><ul><li>Terraform</li><li>AWS CLI</li><li>Go runtime</li><li>EKS cluster</li></ul><p>You can provision and <a href="https://docs.aws.amazon.com/eks/latest/userguide/create-cluster.html">create and manage your EKS cluster</a> in any way you prefer. It might be App of Apps, eksctl, or manual provisioning, as it is very individual. However, I will be showcasing example kubernetes resources with Terraform code examples.</p><p>So, as mentioned earlier — with our EKS cluster managed via Terraform, this project can be a part of it as a separate module or a standalone project decoupled from main code basis. In this article, it will be separate.</p><h4>Lambda RBAC permissions</h4><p>To enable our Lambda function to interact with the EKS cluster, we need to grant it specific permissions using Role-Based Access Control (RBAC). This involves defining a Kubernetes group, by creating a cluster role, and establishing a cluster role binding. I am creating those using my main EKS Terraform project. You can go ahead and do the same manually 🙃</p><p>Further, We can associate this group with our Lambda role by <a href="https://docs.aws.amazon.com/eks/latest/userguide/access-entries.html">creating an access entry</a> in the separate project where we will provision the Lambda itself or right in the main eks project or once again go and do it manually via console. For now let’s just create RBAC components:</p><pre>resource &quot;kubernetes_cluster_role&quot; &quot;lambda&quot; {<br>  metadata {<br>    name = &quot;lambda-clusterrole&quot;<br>  }<br>  rule {<br>    api_groups = [&quot;*&quot;]<br>    resources  = [&quot;deployments&quot;, &quot;deployments/scale&quot;, &quot;statefulsets&quot;, &quot;daemonsets&quot;, &quot;jobs&quot;]<br>    verbs      = [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;, &quot;patch&quot;, &quot;delete&quot;]<br>  }<br>}<br><br>resource &quot;kubernetes_cluster_role_binding&quot; &quot;lambda&quot; {<br>  metadata {<br>    name = &quot;lambda-clusterrolebinding&quot;<br>  }<br>  role_ref {<br>    api_group = &quot;rbac.authorization.k8s.io&quot;<br>    kind      = &quot;ClusterRole&quot;<br>    name      = kubernetes_cluster_role.lambda.metadata[0].name<br>  }<br>  subject {<br>    kind      = &quot;Group&quot;<br>    name      = &quot;lambda-group&quot;<br>    api_group = &quot;rbac.authorization.k8s.io&quot;<br>  }<br>}</pre><h4><strong>EKS Lambda Client</strong></h4><p>Now, let’s dive into the heart of our project: the EKS Lambda Client. You’ll find all the relevant code to deploy our Lambda function using Terraform in <a href="https://github.com/WarDove/eks-downscaler">this GitHub repo</a>.</p><pre>eks-downscaler<br>├── .github<br>│   └── workflows<br>│       └── terraform.yml<br>├── lambdas<br>│   └── downscaler<br>│       ├── go.mod<br>│       └── main.go<br>│       <br>├── modules<br>│   └── downscaler<br>│       ├── iam.tf<br>│       ├── lambda.tf<br>│       ├── locals.tf<br>│       ├── scheduler.tf<br>│       └── variables.tf<br>├── backend.sh<br>├── backend.tf<br>├── main.tf<br>├── readme.md<br>├── terraform.tfvars<br>└── variables.tf</pre><p>Our repository contains both the Golang Lambda code and the Terraform scripts that manage its deployment. We’ll be exploring each component step-by-step, starting with the Terraform S3 backend bootstrapper script. As always this is where the magic begins, setting up the backend for our infrastructure ( backend.sh file).</p><pre>#!/bin/bash<br>set -euo pipefail<br><br>PROJECT_NAME=$(basename &quot;$(dirname \&quot;${PWD}\&quot;)&quot;)<br>AWS_REGION=${AWS_REGION:-us-east-2}<br>AWS_PROFILE=${AWS_PROFILE:-default}<br>AWS_ACCOUNT_ID=&quot;$(aws sts get-caller-identity --query Account --output text --profile ${AWS_PROFILE})&quot;<br>export AWS_PAGER=&quot;&quot;<br><br>echo -e &quot;Bootstraping terraform backend...\n&quot;<br>echo PROJECT_NAME: &quot;${PROJECT_NAME}&quot;<br>echo AWS_REGION: &quot;${AWS_REGION}&quot;<br>echo AWS_PROFILE: &quot;${AWS_PROFILE}&quot;<br>echo AWS_ACCOUNT_ID: &quot;${AWS_ACCOUNT_ID}&quot;<br>echo BUCKET NAME: &quot;terraform-tfstate-${PROJECT_NAME}&quot;<br>echo DYNAMODB TABLE NAME: terraform-locks<br>echo -e &quot;\n&quot;<br><br>aws s3api create-bucket \<br>    --region &quot;${AWS_REGION}&quot; \<br>    --create-bucket-configuration LocationConstraint=&quot;${AWS_REGION}&quot; \<br>    --bucket &quot;terraform-tfstate-${PROJECT_NAME}&quot; \<br>    --profile &quot;${AWS_PROFILE}&quot;<br><br>aws dynamodb create-table \<br>    --region &quot;${AWS_REGION}&quot; \<br>    --table-name terraform-locks \<br>    --attribute-definitions AttributeName=LockID,AttributeType=S \<br>    --key-schema AttributeName=LockID,KeyType=HASH \<br>    --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \<br>    --profile &quot;${AWS_PROFILE}&quot;<br><br>cat &lt;&lt;EOF &gt; ./backend.tf<br>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 5.57&quot;<br>    }<br>  }<br><br>  required_version = &quot;&gt;=1.9.0&quot;<br><br>  backend &quot;s3&quot; {<br>    bucket         = &quot;terraform-tfstate-${PROJECT_NAME}&quot;<br>    key            = &quot;${PROJECT_NAME}&quot;<br>    region         = &quot;${AWS_REGION}&quot;<br>    dynamodb_table = &quot;terraform-locks&quot;<br>  }<br>}<br><br>provider &quot;aws&quot; {}<br>EOF<br><br>echo -e &quot;\nBackend configuration created successfully!\n&quot;<br>cat ./backend.tf</pre><p>This script bootstraps a Terraform backend for managing state files. It captures the project name, AWS region, profile, and account ID. Then, it creates an S3 bucket and a DynamoDB table for state file storage and locking, respectively. Finally, it generates a backend.tf configuration file, linking Terraform to the newly created S3 bucket and DynamoDB table, ensuring secure and organized state management. So to initialize first you will need to define your Access credentials either by using AWS_PROFILE or AWS_SECRET_ACCESS_KEY &amp; AWS_ACCESS_KEY_ID.</p><h4>Main Configuration File</h4><blockquote><em>It would be redundant to go over every file in our repo, as the hcl + go code is defined in idiomatic way, and all the nitty-gritty part should be clear already (iam role and policies for lambda to describe our cluster, EventBridge scheduler resource and its permissions.. all the extras and stuff..)</em></blockquote><p>Our main.tf is the main spot where we define all the configuration for our client. This file references the downscaler module, specifying the EKS cluster name, Lambda source path, scaling schedules, and namespaces to manage. It’s like the conductor of an orchestra, ensuring every component works in perfect harmony.</p><pre>module &quot;downscaler_lambda_client&quot; {<br>  source             = &quot;./modules/downscaler&quot;<br>  eks_cluster_name   = var.cluster_name<br>  ci_env             = var.ci_env<br>  lambda_source      = &quot;${path.root}/lambdas/downscaler&quot;<br>  scale_out_schedule = &quot;cron(00 09 ? * MON-FRI *)&quot;<br>  scale_in_schedule  = &quot;cron(00 18 ? * MON-FRI *)&quot;<br>  eks_groups         = [&quot;lambda-group&quot;]<br>  namespaces         = [&quot;development&quot;, &quot;test&quot;]<br>}</pre><h4>Lambda Deployment Process</h4><p>Now, let’s talk about how we build, zip, and deploy our Lambda function in modules/downscaler/lambda.tf.</p><pre>resource &quot;null_resource&quot; &quot;lambda_build&quot; {<br>  provisioner &quot;local-exec&quot; {<br>    working_dir = var.lambda_source<br>    command     = &quot;go mod tidy &amp;&amp; GOARCH=amd64 GOOS=linux go build -o bootstrap main.go&quot;<br>  }<br><br>  triggers = {<br>    ci_env    = var.ci_env<br>    file_hash = md5(file(&quot;${var.lambda_source}/main.go&quot;))<br>  }<br>}<br><br>data &quot;archive_file&quot; &quot;lambda_zip&quot; {<br>  depends_on  = [null_resource.lambda_build]<br>  type        = &quot;zip&quot;<br>  source_file = &quot;${var.lambda_source}/bootstrap&quot;<br>  output_path = &quot;${var.lambda_source}/main.zip&quot;<br>}<br><br>resource &quot;aws_lambda_function&quot; &quot;downscaler_lambda&quot; {<br>  filename         = data.archive_file.lambda_zip.output_path<br>  source_code_hash = data.archive_file.lambda_zip.output_base64sha256<br>  function_name    = var.project_name<br>  handler          = &quot;main&quot;<br>  runtime          = &quot;provided.al2023&quot;<br>  role             = aws_iam_role.lambda_role.arn<br><br>  environment {<br>    variables = { CLUSTER_NAME = var.eks_cluster_name }<br>  }<br>}<br><br>resource &quot;aws_eks_access_entry&quot; &quot;lambda&quot; {<br>  cluster_name      = var.eks_cluster_name<br>  principal_arn     = aws_iam_role.lambda_role.arn<br>  kubernetes_groups = var.eks_groups<br>  type              = &quot;STANDARD&quot;<br>}</pre><ul><li>Building the Lambda: We begin by checking for changes in our main.go file. Using a hash function, we detect any modifications and trigger a rebuild of the Lambda function. This ensures our deployment is always up-to-date with the latest code changes.</li><li>We also have a cheeky ci_env = var.ci_env trigger here, which identifies if we are running the project locally or from CI. Motive is to rebuild our application every time if we are applying in Github Actions CI Context.</li><li>Zipping the Lambda: After building the Lambda, we zip the compiled executable. This zipped file becomes the core package for our Lambda function, ready for deployment.</li><li>Deploying the Lambda: With Terraform, we deploy the Lambda function using the zipped file. Terraform handles all the heavy lifting, ensuring the Lambda is correctly set up and configured to interact with our EKS cluster.</li><li>Assigning Permissions: We also need to attach the necessary permissions (previously created RBAC resources) to local Lambda role by using <a href="https://docs.aws.amazon.com/eks/latest/userguide/access-entries.html#creating-access-entries">Access Entries</a>.</li><li>All AWS sided permissions -&gt; iam resources — roles, necessary policies are defined in <a href="https://github.com/WarDove/eks-downscaler/edit/main/modules/downscaler/iam.tf">iam.tf</a> file</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*LVmTtnNh3PON80iv.png" /></figure><p>This elegant process, managed by Terraform, ensures that our Lambda function is built, zipped, and deployed seamlessly.</p><h4>Lambda client code</h4><pre>package main<br><br>import (<br>    &quot;context&quot;<br>    &quot;encoding/json&quot;<br>    &quot;github.com/aws/aws-lambda-go/lambda&quot;<br>    eksauth &quot;github.com/chankh/eksutil/pkg/auth&quot;<br>    log &quot;github.com/sirupsen/logrus&quot;<br>    autoscalingv1 &quot;k8s.io/api/autoscaling/v1&quot;<br>    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;<br>    &quot;k8s.io/client-go/kubernetes&quot;<br>    &quot;os&quot;<br>)<br><br>type Payload struct {<br>    ClusterName string   `json:&quot;clusterName&quot;`<br>    Namespaces  []string `json:&quot;namespaces&quot;`<br>    Replicas    int32    `json:&quot;replicas&quot;`<br>}<br><br>func main() {<br>    if os.Getenv(&quot;ENV&quot;) == &quot;DEBUG&quot; {<br>        log.SetLevel(log.DebugLevel)<br>    }<br><br>    lambda.Start(handler)<br>}<br><br>func handler(ctx context.Context, payload Payload) (string, error) {<br>    cfg := &amp;eksauth.ClusterConfig{<br>        ClusterName: payload.ClusterName,<br>    }<br><br>    clientset, err := eksauth.NewAuthClient(cfg)<br>    if err != nil {<br>        log.WithError(err).Error(&quot;Failed to create EKS client&quot;)<br>        return &quot;&quot;, err<br>    }<br><br>    scaled := make(map[string]int32)<br><br>    for _, ns := range payload.Namespaces {<br>        deployments, err := clientset.AppsV1().Deployments(ns).List(ctx, metav1.ListOptions{})<br>        if err != nil {<br>            log.WithError(err).Errorf(&quot;Failed to list deployments in namespace %s&quot;, ns)<br>            continue<br>        }<br><br>        for _, deploy := range deployments.Items {<br>            if err := scaleDeploy(clientset, ctx, ns, deploy.Name, payload.Replicas); err == nil {<br>                scaled[ns+&quot;/&quot;+deploy.Name] = payload.Replicas<br>            }<br>        }<br>    }<br><br>    scaledJSON, err := json.Marshal(scaled)<br>    if err != nil {<br>        log.WithError(err).Error(&quot;Failed to marshal scaled deployments to JSON&quot;)<br>        return &quot;&quot;, err<br>    }<br><br>    log.Info(&quot;Scaled Deployments: &quot;, string(scaledJSON))<br>    return &quot;Scaled Deployments: &quot; + string(scaledJSON), nil<br>}<br><br>func scaleDeploy(client *kubernetes.Clientset, ctx context.Context, namespace, name string, replicas int32) error {<br>    scale := &amp;autoscalingv1.Scale{<br>        ObjectMeta: metav1.ObjectMeta{<br>            Name:      name,<br>            Namespace: namespace,<br>        },<br>        Spec: autoscalingv1.ScaleSpec{<br>            Replicas: replicas,<br>        },<br>    }<br><br>    _, err := client.AppsV1().Deployments(namespace).UpdateScale(ctx, name, scale, metav1.UpdateOptions{})<br>    if err != nil {<br>        log.WithError(err).Errorf(&quot;Failed to scale deployment %s in namespace %s&quot;, name, namespace)<br>    } else {<br>        log.Infof(&quot;Successfully scaled deployment %s in namespace %s to %d replicas&quot;, name, namespace, replicas)<br>    }<br>    return err<br>}</pre><p>Our Lambda client code adds the last piece of logic behind scaling operations in the EKS cluster. It starts by defining a Payload structure, which includes the cluster name, namespaces, and desired replicas. The main function sets up the Lambda handler, which initiates the scaling process. And obviously we are picking the name of the cluster as an environment variable — which is originally propagated via terraform resource.</p><p>The handler creates an EKS client, lists deployments in the specified namespaces, and scales each deployment to the desired number of replicas. The scaled deployments are then logged and returned as a JSON response. This ensures our deployments are dynamically scaled based on the defined schedules, providing efficient resource management.</p><p>Naturally, all logs flow directly to CloudWatch Logs, where we can observe all the details regarding invocations and it’s output.</p><h4>Terraform CI/CD</h4><p>It’s continuous delivery and continuous provisioning time! 🚀🚀🚀 <br> Our CI/CD workflow is defined in the terraform.yml file within the .github/workflows directory. This workflow ensures that our Terraform configurations are automatically applied whenever changes are pushed to the main branch.</p><pre>name: Terraform CI/CD<br>run-name: &quot;Terraform CI/CD | triggered by @${{ github.actor }}&quot;<br><br>on:<br>  push:<br>    branches:<br>      - &#39;main&#39;<br><br>jobs:<br>  terraform-apply:<br>    runs-on: ubuntu-latest<br><br>    env:<br>      TF_VAR_cluster_name: ${{ secrets.CLUSTER_NAME }}<br>      TF_VAR_ci_env: true<br>      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br>      AWS_REGION: us-east-2<br><br>    steps:<br>      - uses: actions/checkout@v4<br><br>      - uses: hashicorp/setup-terraform@v3<br>        with:<br>          terraform_version: 1.9.0<br><br>      - name: Terraform Init<br>        run: terraform init<br><br>      - name: Terraform Validate<br>        run: terraform validate<br><br>      - name: Terraform Apply<br>        run: terraform apply -auto-approve</pre><p>The CI/CD pipeline kicks off by checking out the code from the repository and setting up Terraform. It then initializes Terraform, validates the configuration, and applies the changes to deploy our infrastructure.</p><blockquote><em>It is always better to have extra linting/testing/scanning in terraform CI, another topic for another day maybe😉</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*95jTHaGle9Hsr-5J.png" /></figure><h4>Secrets and Environment Variables</h4><p>Key secrets and environment variables are passed into the workflow to ensure secure and proper configuration. We use the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY for authenticating with AWS, and CLUSTER_NAME to specify the EKS cluster name. These secrets are securely stored in GitHub&#39;s repository settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*kvSY4C4m0BG8qyOS.png" /></figure><p>As you may already know TF_VAR_ prefix, is handy to override/define values via environment variables - especially in CI environment. This ensures that even if variables are defined elsewhere or gitignored (in our case tfvars files are gitignored), our CI/CD pipeline uses the CI values. For example, TF_VAR_ci_env is set to true in the CI environment, enforcing rebuilds (via local-provisioner trigger) and ensuring changes are accurately reflected in the deployment.</p><h4>Farewell 😊</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*2F7032a_M3MghiFH.gif" /></figure><p>In this article, we’ve explored a comprehensive method for deploying and managing AWS resources using Terraform, AWS Lambda, and EventBridge. We delved into the seamless integration of Terraform for deploying compiled Lambda functions, showcased how AWS Lambda can effectively interact with EKS, and implemented full-scale automation with GitHub Actions. These three pearls highlight the power of Terraform in creating a cohesive and dynamic deployment strategy.</p><p>Thank you for following along. I hope this guide has provided you with valuable insights and can serve as a reference for your future projects. Keep exploring, keep learning, and continue refining your cloud practices!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/0*XWX2vR-Qt3Pf4V7p.gif" /></figure><blockquote><em>“One tool to rule them all, one tool to deploy them, One tool to automate ’em, and with terraform apply them!”</em></blockquote><h3>Stackademic 🎓</h3><p>Thank you for reading until the end. Before you go:</p><ul><li>Please consider <strong>clapping</strong> and <strong>following</strong> the writer! 👏</li><li>Follow us <a href="https://twitter.com/stackademichq"><strong>X</strong></a> | <a href="https://www.linkedin.com/company/stackademic"><strong>LinkedIn</strong></a> | <a href="https://www.youtube.com/c/stackademic"><strong>YouTube</strong></a> | <a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a></li><li>Visit our other platforms: <a href="https://plainenglish.io/"><strong>In Plain English</strong></a> | <a href="https://cofeed.app/"><strong>CoFeed</strong></a> | <a href="https://differ.blog/"><strong>Differ</strong></a></li><li>More content at <a href="https://stackademic.com/"><strong>Stackademic.com</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e9cf1a14a51" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/one-tool-to-rule-them-all-terraform-eks-golang-client-e2e-aws-lambda-ci-cd-via-iac-6e9cf1a14a51">One tool to rule them all — Terraform: EKS Golang Client &amp; E2E AWS Lambda CI/CD via IaC</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GitOps for CloudFront and S3]]></title>
            <link>https://medium.com/cloud-native-daily/gitops-for-cloudfront-and-s3-9a29fa01fd39?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a29fa01fd39</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[cloudfront]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[gitops]]></category>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Wed, 05 Jun 2024 12:34:14 GMT</pubDate>
            <atom:updated>2024-07-11T09:45:37.389Z</atom:updated>
            <content:encoded><![CDATA[<h4>How we can manage front-end deployments in GitOps style: This strategy uses ArgoCD, EKS, and AWS services like CloudFront and S3 to streamline deployments, improve performance, and maintain best practices.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*EvQqKo-flxCcTU_PrV05zg.png" /></figure><p>In the world of modern web development, deploying front-end applications efficiently and reliably is a key challenge. As teams adopt GitOps strategies to streamline and automate deployments, certain complexities arise, particularly when integrating with AWS services like CloudFront and S3.</p><p>So let’s consider that ideally all our workloads are containerized and all run on Kubernetes (EKS) platform, we have all security checks, automations, tests and pipelines in place, and already have leveraged ArgoCD and supplementary tools for deployments.</p><p>Now one common dilemma is deciding how to manage front-end deployments consistently in GitOps style while maintaining the benefits of using CloudFront for caching and performance optimization. Some teams consider moving front-end assets to containers for consistency, but this can introduce unnecessary complexity and deviate from best practices.</p><p>When employing a centralized GitOps strategy, it’s crucial to keep the deployment process consistent and manageable. However, front-end applications often require specific considerations:</p><ul><li><strong>Caching and Performance</strong>: CloudFront provides a robust solution for caching and delivering static assets, ensuring high performance and low latency.</li><li><strong>Artifact Management</strong>: Synchronizing build artifacts to the correct S3 paths while managing different versions can be challenging.</li><li><strong>Deployment Automation</strong>: Automating the deployment process while ensuring the correct paths and versions are updated in CloudFront.</li><li><strong>Consistency and Reproducibility</strong>: Maintaining consistent and reproducible deployments across environments.</li><li><strong>Easy and rapid Rollbacks</strong> — if possible ofc. 😌</li></ul><p>In this article, I will share a solution I implemented to address these challenges. This approach leverages ArgoCD, EKS, AWS CloudFront, and S3, integrating them seamlessly into a GitOps workflow. By using a Kubernetes job with AWS CLI, we can manage CloudFront paths dynamically, ensuring our front-end application is always up-to-date and efficiently delivered.</p><h3>Release Flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*onab3_xSokUT6rjmFjPpDg.gif" /></figure><ol><li><em>Release branch is merged to main</em></li><li><em>New release tag created from main</em></li><li><em>GHA is triggered on release to test and build the code</em></li><li><em>Generated artifacts are tagged and synced to s3 with respective path</em></li><li><em>Developer creates a pull request to pass the new version to GitOps repo</em></li><li><em>PR is merged and values file is updated with new version</em></li><li><em>ArgoCD picks up the changes after being triggered via Webhook or by polling</em></li><li><em>Values diff triggered a new job creation by ArgoCD</em></li><li><em>Kubernetes Job sends api call to CF to swap the origin path based on the new version</em></li></ol><h3>Building and Syncing to S3</h3><p>To deploy our front-end application, we use GitHub Actions to handle the build and deployment process. The workflow triggers on new version tags, checks out the repository, sets up build environment, and configures AWS credentials. It retrieves secrets, installs dependencies, runs tests, builds the application, and syncs the output to an S3 bucket. We can of-course have multiple parallel workflows for each environment and sync to different s3 buckets with dedicated path that reflects release tag, or even sync to the same s3 bucket with dedicated path that reflects target environment + release tag ( I like the 1st option better for total segregation ).</p><p>For instance, if we’re deploying a version tagged v1.0.0 to the production environment, the path in S3 would be s3://frontend-production-artifacts/production/v1.0.0.</p><pre>name: Publish Release Artifact Version<br>run-name: &quot;Production Release ${{ github.ref_name }} | Publishing Artifact Version | triggered by @${{ github.actor }}&quot;<br><br>on:<br>  push:<br>    tags:<br>      - &#39;v[0-9]+.[0-9]+.[0-9]+&#39;<br><br>env:<br>  ARTIFACTS_BUCKET: frontend-production-artifacts<br>  AWS_REGION: us-east-2<br>  ENVIRONMENT: production<br><br>jobs:<br>  build-and-publish:<br>    runs-on: ubuntu-latest<br><br>    steps:<br>      - name: Checkout repository<br>        uses: actions/checkout@v4<br><br>      - name: Set up Node.js<br>        uses: actions/setup-node@v4<br>        with:<br>          node-version: &#39;20&#39;<br><br>      - name: Configure AWS credentials<br>        uses: aws-actions/configure-aws-credentials@v4<br>        with:<br>          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}<br>          aws-secret-access-key:  ${{ secrets.AWS_SECRET_ACCESS_KEY }}<br>          aws-region: ${{ env.AWS_REGION }}<br><br>      - name: Get Secrets<br>        uses: aws-actions/aws-secretsmanager-get-secrets@v2<br>        with:<br>          secret-ids: frontend-secrets-${{ env.ENVIRONMENT }}<br>          parse-json-secrets: true<br><br>      - name: Install dependencies<br>        run: yarn install<br><br>      - name: Run tests<br>        run: yarn test<br><br>      - name: Build<br>        run: yarn build<br><br>      - name: Sync files to Artifacts bucket<br>        run: aws s3 sync build/ s3://${{ env.ARTIFACTS_BUCKET }}/${{ env.ENVIRONMENT }}/${{ github.ref_name }} --delete</pre><h3>Deploying with Helm Chart and Kubernetes Job</h3><p>To automate the deployment process further, we can use a Helm chart that defines a Kubernetes job. This job handles updating the CloudFront origin path for the new version of our application using a Docker image with AWS CLI installed.</p><p>We have a values file that provides parameters like the application name, version, Docker image, S3 bucket name, and AWS region.</p><pre>global:<br>  region: &quot;us-east-2&quot;<br><br>app:<br>  name: &quot;store-ui&quot;<br>  version: v1.0.10<br>  backOffLimit: &quot;4&quot;<br>  jobImage: &quot;amazon/aws-cli:2.16.1&quot;<br>  originS3: &quot;frontend-production-artifacts&quot;</pre><p>The Kubernetes job uses these values to dynamically set its configuration. It includes the job name, the container image, and environment variables for the S3 bucket, origin path, and AWS region.</p><p>When the job runs, it installs jq for JSON processing, retrieves the CloudFront distribution ID based on the S3 bucket name, fetches the current CloudFront configuration, updates the origin path to the new version, and invalidates the CloudFront cache to ensure the latest version is served to users. Of course you can always build your own lightweight docker image with all dependencies already installed (aws-cli and jq), or even build your own solution by leveraging AWS SDK directly.</p><pre>apiVersion: batch/v1<br>kind: Job<br>metadata:<br>  name: swap-cf-origin-path-{{ .Values.app.name }}-{{ .Values.app.version }}<br>spec:<br>  template:<br>    spec:<br>      serviceAccountName: {{ .Values.app.name }}<br>      containers:<br>        - name: aws-cli<br>          image: {{ .Values.app.jobImage }}<br>          env:<br>            - name: S3_BUCKET_NAME<br>              value: {{ .Values.app.originS3 }}<br>            - name: ORIGIN_PATH<br>              value: /{{ .Release.Namespace }}/{{ .Values.app.version }}<br>            - name: AWS_REGION<br>              value: {{ .Values.global.region }}<br>          command: [&quot;/bin/sh&quot;,&quot;-c&quot;]<br>          args:<br>            - |<br>              set -e<br>              yum install jq -y<br><br>              CF_DIST_ID=$(aws cloudfront list-distributions --query &quot;DistributionList.Items[?contains(Origins.Items[].DomainName, &#39;${S3_BUCKET_NAME}.s3.${AWS_REGION}.amazonaws.com&#39;)].Id | [0]&quot; --output text)<br><br>              OUTPUT=$(aws cloudfront get-distribution-config --id $CF_DIST_ID)<br>              ETAG=$(echo &quot;$OUTPUT&quot; | jq -r &#39;.ETag&#39;)<br>              DIST_CONFIG=$(echo &quot;$OUTPUT&quot; | jq &#39;.DistributionConfig&#39;)<br><br>              UPDATED_CONFIG=$(echo &quot;$DIST_CONFIG&quot; | jq --arg path &quot;${ORIGIN_PATH}&quot; &#39;.Origins.Items[0].OriginPath = $path&#39;)<br><br>              aws cloudfront update-distribution --id $CF_DIST_ID --if-match $ETAG --distribution-config &quot;$UPDATED_CONFIG&quot;<br><br>              aws cloudfront create-invalidation --distribution-id $CF_DIST_ID --paths &quot;/*&quot;<br>      restartPolicy: Never<br>  backoffLimit: {{ .Values.app.backOffLimit }}</pre><p>To allow our Kubernetes job to interact with AWS services like CloudFront and S3, we need to grant it the necessary permissions to the job’s service account. We can achieve this by using <a href="https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html">IAM Roles for Service Accounts</a> (IRSA) or <a href="https://docs.aws.amazon.com/eks/latest/userguide/pod-identities.html">Pod identities</a>. Here’s how you can configure IRSA option using Terraform. This setup allows the Kubernetes job to securely perform actions required for updating the CloudFront origin path and invalidating the cache.</p><pre>data &quot;aws_iam_policy_document&quot; &quot;service_account_assume_role&quot; {<br>  statement {<br>    actions = [&quot;sts:AssumeRoleWithWebIdentity&quot;]<br>    effect  = &quot;Allow&quot;<br><br>    condition {<br>      test     = &quot;StringEquals&quot;<br>      variable = &quot;${replace(aws_iam_openid_connect_provider.oidc_provider_sts.url, &quot;https://&quot;, &quot;&quot;)}:aud&quot;<br>      values   = [&quot;sts.amazonaws.com&quot;]<br>    }<br><br>    condition {<br>      test     = &quot;StringEquals&quot;<br>      variable = &quot;${replace(aws_iam_openid_connect_provider.oidc_provider_sts.url, &quot;https://&quot;, &quot;&quot;)}:sub&quot;<br>      values   = [&quot;system:serviceaccount:${var.namespace}:store-ui&quot;]<br>    }<br><br>    principals {<br>      identifiers = [aws_iam_openid_connect_provider.oidc_provider_sts.arn]<br>      type        = &quot;Federated&quot;<br>    }<br>  }<br>}<br><br>resource &quot;aws_iam_role&quot; &quot;service_account_role&quot; {<br>  assume_role_policy = data.aws_iam_policy_document.service_account_assume_role.json<br>  name               = &quot;store-ui-sa-role-${var.namespace}&quot;<br>  tags               = local.default_tags<br><br>  lifecycle {<br>    create_before_destroy = false<br>  }<br>}<br><br>resource &quot;aws_iam_policy&quot; &quot;store_ui_swap_origin_policy&quot; {<br>  name        = &quot;store-ui-policy-${var.namespace}&quot;<br>  path        = &quot;/&quot;<br>  description = &quot;IAM policy for store-ui job service account&quot;<br>  <br>  policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action   = [<br>          &quot;cloudfront:GetDistribution&quot;,<br>          &quot;cloudfront:UpdateDistribution&quot;,<br>          &quot;cloudfront:CreateInvalidation&quot;,<br>          &quot;s3:ListBucket&quot;,<br>          &quot;s3:GetObject&quot;<br>        ]<br>        Effect   = &quot;Allow&quot;<br>        Resource = &quot;*&quot;<br>      }<br>    ]<br>  })<br>}<br><br># Attach policies to the service account role<br>resource &quot;aws_iam_role_policy_attachment&quot; &quot;service_account_role&quot; {<br>  depends_on = [<br>    aws_iam_role.service_account_role,<br>    aws_iam_policy.store_ui_policy<br>  ]<br><br>  role       = aws_iam_role.service_account_role.name<br>  policy_arn = aws_iam_policy.store_ui_swap_origin_policy.arn<br>}</pre><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_y3pt0oPUsWV4pzcAGJ_Ag.png" /></figure><p>At this point, all we need to do is push the new version after building and syncing it to S3. The job will handle updating the CloudFront origin path and invalidating the cache, ensuring that users always get the latest version of our front-end application. For an even more cosmetically satisfying approach, we could implement an additional Continuous Deployment (CD) solution on top of ArgoCD, such as OctopusDeploy. However, that is a topic for another day and another discussion 😉 Farewell Folks! 😊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*cFENWd5Zs9WMOt3WgJAnlA.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a29fa01fd39" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/gitops-for-cloudfront-and-s3-9a29fa01fd39">GitOps for CloudFront and S3</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EKS ZeroScaler: Scheduled auto-scaling to zero with Lambda GO client]]></title>
            <link>https://aws.plainenglish.io/eks-zeroscaler-scheduled-auto-acaling-to-zero-with-lambda-go-client-882b192ef7d1?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/882b192ef7d1</guid>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Fri, 19 Jan 2024 09:48:03 GMT</pubDate>
            <atom:updated>2024-02-21T08:38:15.238Z</atom:updated>
            <content:encoded><![CDATA[<h4>A seamless integration of Golang Lambda kubernetes client, EventBridge, and EKS, all efficiently deployed with Terraform, for dynamic, cost-effective auto-scaling in development environments.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*xE9fRGL8XXVsL8cmvmCDRw.png" /></figure><p>In this insightful piece, we will delve into a cutting-edge solution designed to enhance DevOps efficiency and reduce costs in development environments. Leveraging the power of AWS technologies, we will explore how a Golang-based Lambda function, orchestrated by an EventBridge scheduler, can dynamically scale down EKS Fargate namespace deployments to zero. This strategy proves highly effective not only with Fargate but also when paired with autoscaler/karpenter solutions, offering a versatile approach to cost reduction and resource optimization. Significantly lowering resource utilization during non-peak hours, this method is both cost-effective and efficient. Also worth to highlight that we will touch a streamlined way of deploying this Lambda function using Terraform, showcasing an infrastructure-as-code approach that ensures consistency and ease of management. Whether you’re a seasoned DevOps engineer or just starting out, this article offers valuable insights into automating and optimizing cloud resources in a Kubernetes environment, with a focus on practical and effective deployment strategies.</p><blockquote>The Golang Lambda Function - A Deep Dive</blockquote><p>The heart of this solution is a simple Golang-based Lambda function designed to dynamically scale down deployments in specific namespaces of an EKS cluster. Here’s a closer look at how this function operates.</p><p>Our Lambda function is crafted in Go, a language known for its efficiency and suitability for concurrent tasks, making it a perfect choice for cloud-native applications.</p><blockquote>main.go</blockquote><pre>package main<br><br>import (<br> &quot;context&quot;<br> &quot;encoding/json&quot;<br> &quot;os&quot;<br><br> eksauth &quot;github.com/chankh/eksutil/pkg/auth&quot;<br><br> autoscalingv1 &quot;k8s.io/api/autoscaling/v1&quot;<br> metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;<br> &quot;k8s.io/client-go/kubernetes&quot;<br><br> &quot;github.com/aws/aws-lambda-go/lambda&quot;<br> log &quot;github.com/sirupsen/logrus&quot;<br>)<br><br>type Payload struct {<br> ClusterName string   `json:&quot;clusterName&quot;`<br> Namespaces  []string `json:&quot;namespaces&quot;`<br> Replicas    int32    `json:&quot;replicas&quot;`<br>}<br><br>func main() {<br> if os.Getenv(&quot;ENV&quot;) == &quot;DEBUG&quot; {<br>  log.SetLevel(log.DebugLevel)<br> }<br><br> lambda.Start(handler)<br>}<br><br>func handler(ctx context.Context, payload Payload) (string, error) {<br> cfg := &amp;eksauth.ClusterConfig{<br>  ClusterName: payload.ClusterName,<br> }<br><br> clientset, err := eksauth.NewAuthClient(cfg)<br> if err != nil {<br>  log.WithError(err).Error(&quot;Failed to create EKS client&quot;)<br>  return &quot;&quot;, err<br> }<br><br> scaled := make(map[string]int32)<br><br> for _, ns := range payload.Namespaces {<br>  deployments, err := clientset.AppsV1().Deployments(ns).List(ctx, metav1.ListOptions{})<br>  if err != nil {<br>   log.WithError(err).Errorf(&quot;Failed to list deployments in namespace %s&quot;, ns)<br>   continue<br>  }<br><br>  for _, deploy := range deployments.Items {<br>   if err := scaleDeploy(clientset, ctx, ns, deploy.Name, payload.Replicas); err == nil {<br>    scaled[ns+&quot;/&quot;+deploy.Name] = payload.Replicas<br>   }<br>  }<br> }<br><br> scaledJSON, err := json.Marshal(scaled)<br> if err != nil {<br>  log.WithError(err).Error(&quot;Failed to marshal scaled deployments to JSON&quot;)<br>  return &quot;&quot;, err<br> }<br><br> log.Info(&quot;Scaled Deployments: &quot;, string(scaledJSON))<br> return &quot;Scaled Deployments: &quot; + string(scaledJSON), nil<br>}<br><br>func scaleDeploy(client *kubernetes.Clientset, ctx context.Context, namespace, name string, replicas int32) error {<br> scale := &amp;autoscalingv1.Scale{<br>  ObjectMeta: metav1.ObjectMeta{<br>   Name:      name,<br>   Namespace: namespace,<br>  },<br>  Spec: autoscalingv1.ScaleSpec{<br>   Replicas: replicas,<br>  },<br> }<br><br> _, err := client.AppsV1().Deployments(namespace).UpdateScale(ctx, name, scale, metav1.UpdateOptions{})<br> if err != nil {<br>  log.WithError(err).Errorf(&quot;Failed to scale deployment %s in namespace %s&quot;, name, namespace)<br> } else {<br>  log.Infof(&quot;Successfully scaled deployment %s in namespace %s to %d replicas&quot;, name, namespace, replicas)<br> }<br> return err<br>}</pre><p><strong>Key Components</strong></p><p>1. Dependencies and Libraries:<br> The function uses the eksauth package for authenticating with EKS, and the Kubernetes client-go library for interacting with the cluster. AWS Lambda Go SDK is employed for Lambda functionalities, whilelogrus provides robust logging capabilities.</p><p>2. Payload Structure:<br> We define a Payload struct to encapsulate the input, including the target cluster’s name, the namespaces to scale, and the desired number of replicas.</p><p>3. The Main Function:<br> It initializes the log level based on an environment variable and launches the Lambda handler, demonstrating a simple yet effective setup.</p><p>4. Lambda Handler:<br>This function takes the Payload, authenticates with the EKS cluster, and iterates over namespaces to scale down deployments. It’s a demonstration of how straightforward interactions with Kubernetes can be orchestrated in a serverless environment.</p><p>5. Scaling Logic with `scaleDeploy` function:<br>Here, we actually adjust the scale of each deployment. This function showcases the practical application of the Kubernetes API in managing cluster resources.</p><p><strong>Project Structure</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*sSiB3gQ9Q0bodoL2bofP7Q.png" /></figure><p>For seamless integration and management, we’ll be placing our Lambda function within a specific directory structure in our Terraform project. This approach not only keeps our project organized but also simplifies the deployment process.</p><p>We’ll store the Lambda function in files/lambdas/pod_zeroscaler (some fancy naming won’t hurt 😉) . This dedicated directory within our Terraform project ensures that our Lambda code is logically separated and easily accessible in a location where we can edit our code, build it and deploy it as a lambda function by applying terraform continuously ( we will look into how-to do that later ). Let’s initially set up the lambda function’s code basis.</p><p><strong>Initialize the Go Module</strong> — Navigate to the pod_zeroscaler directory and run go mod init pod_zeroscaler followed by go mod tidy. This commands set up the necessary Go module files, helping manage dependencies and versioning.</p><p><strong>Build the Lambda Function</strong> — After initializing the module, we compile our Lambda function using GOOS=linux go build -o main</p><blockquote><strong>Note</strong>: JetBrains Goland IDE - For managing projects that involve both Go code and Terraform modules, I highly recommend using JetBrains Goland IDE. Its integrated environment simplifies handling multiple aspects of the project, from coding to deployment. The IDE’s robust features enhance coding efficiency, debugging, and version control management, making it an invaluable tool for any DevOps engineer working in a cloud-native ecosystem. You can get the mentioned Terraform plugin <a href="https://plugins.jetbrains.com/plugin/7808-terraform-and-hcl">here</a>.</blockquote><blockquote>Deploying and Orchestrating the EKS Auto-Scaling Solution with Terraform</blockquote><p>In this section, we delve into the infrastructure setup for our auto-scaling solution, using Terraform to deploy and orchestrate the components. This setup is a crucial part of our strategy to manage resources efficiently in a cloud-native DevOps environment.</p><p><strong>Pre-requisites and Setup</strong></p><p>Before we dive into the specifics, let’s establish our foundational pre-requisites:<br>- Existing Terraform Project: Our setup assumes that there’s an existing Terraform project with either a remote or local state setup.<br>- Pre-configured EKS Cluster: An EKS cluster is already in place, and our solution is designed to integrate with it seamlessly.<br>- Modular Code Format: The provided Terraform code is in a scratch format, ready to be modularized as per the project’s requirements.</p><p><strong>Terraform codebase</strong></p><pre>  locals {<br>  <br>  cluster_name = &quot;my-eks-cluster&quot;<br>  <br>  pod_zeroscaler = {<br>    enabled            = true<br>    scale_out_schedule = &quot;cron(00 09 ? * MON-FRI *)&quot; # At 09:00:00 (UTC), Monday through Friday<br>    scale_in_schedule  = &quot;cron(00 18 ? * MON-FRI *)&quot; # At 18:00:00 (UTC), Monday through Friday<br>    namespaces         = [&quot;default&quot;, &quot;development&quot;]<br>  }<br>  <br>  default_tags = {<br>    Environment       = var.env<br>    Owner             = &quot;Devops&quot;<br>    Managed-by        = &quot;Terraform&quot;<br>  }  <br> } <br> <br>   data &quot;aws_iam_policy_document&quot; &quot;lambda_logging_policy&quot; {<br>    statement {<br>      actions = [<br>        &quot;logs:CreateLogGroup&quot;,<br>        &quot;logs:CreateLogStream&quot;,<br>        &quot;logs:PutLogEvents&quot;<br>      ]<br>      effect    = &quot;Allow&quot;<br>      resources = [&quot;*&quot;]<br>    }<br>  }<br><br>  resource &quot;aws_iam_policy&quot; &quot;lambda_logging_policy&quot; {<br>    name   = &quot;${local.cluster_name}-lambda-logging&quot;<br>    policy = data.aws_iam_policy_document.lambda_logging_policy.json<br>    tags   = local.default_tags<br>  }<br>  <br>  data &quot;aws_iam_policy_document&quot; &quot;describe_cluster_lambda_policy&quot; {<br>    statement {<br>      actions   = [&quot;eks:DescribeCluster&quot;]<br>      resources = [aws_eks_cluster.eks_cluster.arn]<br>    }<br>  }<br><br>  resource &quot;aws_iam_policy&quot; &quot;describe_cluster_lambda_policy&quot; {<br>    name   = &quot;describe-cluster-policy&quot;<br>    policy = data.aws_iam_policy_document.describe_cluster_lambda_policy.json<br>    tags   = local.default_tags<br>  }<br><br>  resource &quot;aws_iam_role&quot; &quot;eks_lambda_role&quot; {<br>    name = &quot;${local.cluster_name}-lambda-role&quot;<br>    tags = local.default_tags<br>    assume_role_policy = jsonencode({<br>      Version = &quot;2012-10-17&quot;<br>      Statement = [<br>        {<br>          Action = &quot;sts:AssumeRole&quot;<br>          Effect = &quot;Allow&quot;<br>          Principal = {<br>            Service = [&quot;lambda.amazonaws.com&quot;]<br>          }<br>        },<br>      ]<br>    })<br>  }<br><br>  resource &quot;aws_iam_role_policy_attachment&quot; &quot;describe_cluster_policy&quot; {<br>    role       = aws_iam_role.eks_lambda_role.name<br>    policy_arn = aws_iam_policy.describe_cluster_lambda_policy.arn<br>  }<br><br>  resource &quot;aws_iam_role_policy_attachment&quot; &quot;lambda_logging_policy&quot; {<br>    count      = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    role       = aws_iam_role.eks_lambda_role.name<br>    policy_arn = aws_iam_policy.lambda_logging_policy.arn<br>  }<br><br><br>  data &quot;archive_file&quot; &quot;pod_zeroscaler_lambda_zip&quot; {<br>    count       = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    type        = &quot;zip&quot;<br>    source_file = &quot;${path.root}/files/lambdas/pod_zeroscaler/main&quot;<br>    output_path = &quot;/tmp/main.zip&quot;<br>  }<br><br>  resource &quot;aws_lambda_function&quot; &quot;pod_zeroscaler_lambda&quot; {<br>    count            = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    filename         = data.archive_file.pod_zeroscaler_lambda_zip[0].output_path<br>    source_code_hash = data.archive_file.pod_zeroscaler_lambda_zip[0].output_base64sha256<br>    function_name    = &quot;${local.cluster_name}-zeroscaler&quot;<br>    handler          = &quot;main&quot;<br>    runtime          = &quot;go1.x&quot;<br>    role             = aws_iam_role.eks_lambda_role.name<br>    environment {<br>      variables = { CLUSTER_NAME = local.cluster_name }<br>    }<br>  }<br><br>  // AWS EventBridge Scheduler<br>  data &quot;aws_iam_policy_document&quot; &quot;pod_zeroscaler_invoke_policy&quot; {<br>    count = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    statement {<br>      effect    = &quot;Allow&quot;<br>      actions   = [&quot;lambda:InvokeFunction&quot;]<br>      resources = [aws_lambda_function.pod_zeroscaler_lambda[0].arn]<br>    }<br>  }<br><br>  resource &quot;aws_iam_policy&quot; &quot;pod_zeroscaler_invoke_policy&quot; {<br>    name        = &quot;${local.cluster_name}-zeroscaler-scheduler-policy&quot;<br>    count       = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    description = &quot;Allow function execution for scheduler role&quot;<br>    policy      = data.aws_iam_policy_document.pod_zeroscaler_invoke_policy[0].json<br>  }<br><br>  data &quot;aws_iam_policy_document&quot; &quot;pod_zeroscaler_invoke_role&quot; {<br>    count = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    statement {<br>      actions = [&quot;sts:AssumeRole&quot;]<br>      principals {<br>        type        = &quot;Service&quot;<br>        identifiers = [&quot;scheduler.amazonaws.com&quot;]<br>      }<br>    }<br>  }<br><br>  resource &quot;aws_iam_role&quot; &quot;pod_zeroscaler_invoke_role&quot; {<br>    count              = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    name               = &quot;${local.cluster_name}-zeroscaler-scheduler-role&quot;<br>    assume_role_policy = data.aws_iam_policy_document.pod_zeroscaler_invoke_role[0].json<br>  }<br><br>  resource &quot;aws_iam_role_policy_attachment&quot; &quot;pod_zeroscaler_invoke_role&quot; {<br>    count      = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    policy_arn = aws_iam_policy.pod_zeroscaler_invoke_policy[0].arn<br>    role       = aws_iam_role.pod_zeroscaler_invoke_role[0].name<br>  }<br><br>  resource &quot;aws_scheduler_schedule&quot; &quot;pod_zeroscaler_scale_in&quot; {<br>    count      = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    name       = &quot;${local.cluster_name}-zeroscaler-scale-in&quot;<br>    group_name = &quot;default&quot;<br><br>    flexible_time_window {<br>      mode = &quot;OFF&quot;<br>    }<br><br>    schedule_expression = local.pod_zeroscaler[&quot;scale_in_schedule&quot;] #&quot;cron(0 8 * * ? *)&quot;<br><br>    target {<br>      arn      = aws_lambda_function.pod_zeroscaler_lambda[0].arn<br>      role_arn = aws_iam_role.pod_zeroscaler_invoke_role[0].arn<br>      input = jsonencode({<br>        &quot;clusterName&quot; = local.cluster_name<br>        &quot;namespaces&quot;  = local.pod_zeroscaler[&quot;namespaces&quot;]<br>        &quot;replicas&quot;    = 0<br>      })<br>    }<br>  }<br><br>  resource &quot;aws_scheduler_schedule&quot; &quot;pod_zeroscaler_scale_out&quot; {<br>    count      = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>    name       = &quot;${local.cluster_name}-zeroscaler-scale-out&quot;<br>    group_name = &quot;default&quot;<br><br>    flexible_time_window {<br>      mode = &quot;OFF&quot;<br>    }<br><br>    schedule_expression = local.pod_zeroscaler[&quot;scale_out_schedule&quot;]<br><br>    target {<br>      arn      = aws_lambda_function.pod_zeroscaler_lambda[0].arn<br>      role_arn = aws_iam_role.pod_zeroscaler_invoke_role[0].arn<br>      input = jsonencode({<br>        &quot;clusterName&quot; = local.cluster_name<br>        &quot;namespaces&quot;  = local.pod_zeroscaler[&quot;namespaces&quot;]<br>        &quot;replicas&quot;    = 1<br>      })<br>    }<br>  }t</pre><p><strong>Terraform Infrastructure Overview</strong></p><p>Our Terraform configuration is tailored to deploy the Lambda function for scaling EKS Fargate deployments and schedule its execution using EventBridge. Here’s how each component fits into the big picture:</p><p>1. Local Variables and Configuration<br> — Cluster Name and Lambda Settings: We define key local variables, including the name of the EKS cluster and configurations for the Lambda function, such as its schedule and target namespaces.<br> — Default Tags: These tags are applied to all created resources for easy tracking and management.</p><p>2. IAM Policies and Roles<br> — Lambda Logging and EKS Access: Policies are set up to allow the Lambda function to log activities and interact with the EKS cluster ( we will configureaws-auth configmap in a separate section to provide a complete granting necessary access to our lambda ).<br> — Policy Attachments: We attach these policies to the Lambda function’s IAM role, ensuring it has the necessary permissions.</p><p>3. Lambda Function Deployment<br> — Function Packaging: The Lambda function, developed in Go and placed in files/lambdas/pod_zeroscaler, is packaged into a zip file.<br> — Deployment: This package is then deployed as a Lambda function in AWS, complete with environment variables and IAM role associations.</p><p>4. EventBridge Scheduler<br> — Invoke Policy and Role: We create an IAM policy and role that allows EventBridge to invoke our Lambda function.<br> — Scheduling Resources: Two EventBridge schedules are set up — one for scaling down (scaling in) and the other for scaling up (scaling out) — each targeting our Lambda function with specific payloads.</p><p>This Terraform setup not only deploys our Lambda function but also orchestrates its operation, aligning with our goal of automating and optimizing resource utilization in a Kubernetes environment. By utilizing Terraform, we ensure a reproducible and efficient deployment process, encapsulating complex cloud operations in a manageable codebase.</p><blockquote>Access Configuration for Lambda in EKS</blockquote><p>The final step in our setup involves configuring the AWS EKS cluster to grant the necessary permissions to our Lambda function. This is done by editing the `aws-auth` ConfigMap in the `kube-system` namespace and setting up appropriate Kubernetes roles and bindings using Terraform. This section ensures our Lambda function can interact effectively with the EKS cluster by using the lambda role attached to it.</p><p>Reference: <a href="https://docs.aws.amazon.com/eks/latest/userguide/add-user-role.html">Enabling IAM principal access to your cluster</a></p><p>We need to update the aws-auth ConfigMap in Kubernetes. This ConfigMap is crucial for controlling access to your EKS cluster. Here’s a sample snippet where the lambda role arn is added to a specific RBAC group named lambda-group ( we will create this group and role binding ).</p><pre>apiVersion: v1<br>kind: ConfigMap<br>metadata:<br>  name: aws-auth<br>  namespace: kube-system<br>data:<br>  mapRoles: |<br>    - rolearn: arn:aws:iam::&lt;ACCOUNT_ID&gt;:role/my-eks-cluster-development-lambda-role<br>      groups:<br>        - lambda-group<br>    - groups:<br>      - system:bootstrappers<br>      - system:nodes<br>      - system:node-proxier<br>      rolearn: arn:aws:iam::&lt;ACCOUNT_ID&gt;:role/my-eks-cluster-development-fargate-pod-execution-role<br>      username: system:node:{{SessionName}}<br>    - groups:yam<br>        - system:bootstrappers<br>        - system:nodes<br>      rolearn: arn:aws:iam::&lt;ACCOUNT_ID&gt;:role/my-eks-cluster-development-node-pod-execution-role<br>      username: system:node:{{EC2PrivateDNSName}}</pre><p>In this ConfigMap, we add a new role entry for our Lambda function’s IAM role. This entry assigns the Lambda role to a group, lambda-group, which we will reference in our Kubernetes role and binding.</p><p><strong>Terraform codebase</strong></p><pre># Cluster Role and Cluster Role Binding for Lambda<br>resource &quot;kubernetes_cluster_role&quot; &quot;lambda_cluster_role&quot; {<br>  count      = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>  metadata {<br>    name = &quot;lambda-clusterrole&quot;<br>  }<br>  rule {<br>    api_groups = [&quot;*&quot;]<br>    resources  = [&quot;deployments&quot;, &quot;deployments/scale&quot;]<br>    verbs      = [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;, &quot;patch&quot;, &quot;delete&quot;]<br>  }<br>  rule {<br>    api_groups = [&quot;&quot;]<br>    resources  = [&quot;pods&quot;]<br>    verbs      = [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;]<br>  }<br>}<br><br>resource &quot;kubernetes_cluster_role_binding&quot; &quot;lambda_cluster_role_binding&quot; {<br>  count  = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br>  metadata {<br>    name = &quot;lambda-clusterrolebinding&quot;<br>  }<br>  role_ref {<br>    api_group = &quot;rbac.authorization.k8s.io&quot;<br>    kind      = &quot;ClusterRole&quot;<br>    name      = kubernetes_cluster_role.lambda_cluster[0]_role.metadata[0].name<br>  }<br>  subject {<br>    kind      = &quot;Group&quot;<br>    name      = &quot;lambda-group&quot;<br>    api_group = &quot;rbac.authorization.k8s.io&quot;<br>  }<br>}</pre><p>We define a Kubernetes Cluster Role and a Cluster Role Binding in Terraform to grant our Lambda function the necessary permissions within the cluster.</p><p>The kubernetes_cluster_role resource defines the permissions our Lambda function needs. We grant it a wide range of verbs on deployments and pods, ensuring it can perform its scaling actions.</p><p>The kubernetes_cluster_role_binding resource binds the above role to the lambda-group. This binding ensures that any IAM role in the `lambda-group` (which includes our Lambda function’s IAM role) gets the permissions defined in the cluster role.</p><blockquote>Automating ConfigMap Update with Terraform and Bash script</blockquote><p>To streamline the process of updating the `aws-auth` ConfigMap in Kubernetes, we can automate this step using Terraform coupled with a local provisioner and a Bash script. This automation not only saves time but also reduces the potential for manual errors.</p><p>We’ll use a Bash script, aws_auth_patch.sh, located in files/scripts, to modify the aws-auth ConfigMap. This script checks if the necessary permissions are already in place and, if not, adds them.</p><pre>#!/bin/bash<br>set -euo pipefail<br><br># Script to update the aws-auth ConfigMap<br><br>AWS_AUTH=$(kubectl get -n kube-system configmap/aws-auth -o yaml)<br><br>if echo &quot;${AWS_AUTH}&quot; | grep -q &quot;${GROUP}&quot;; then<br>  echo &quot;Permission is already added for ${GROUP}&quot;<br>else<br>  NEW_ROLE=&quot;    - rolearn: ${ROLE_ARN}\n      groups:\n        - ${GROUP}&quot;<br>  PATCH=$(kubectl get -n kube-system configmap/aws-auth -o yaml | awk &quot;/mapRoles: \|/{printf \&quot;%s\n%s\n\&quot;, \$0, \&quot;${NEW_ROLE}\&quot;;next}1&quot;)<br>  kubectl patch configmap/aws-auth -n kube-system --patch &quot;${PATCH}&quot;<br>fi</pre><p>This script retrieves the current `aws-auth` ConfigMap, checks if the required role is already added, and if not, it patches the ConfigMap with the new role.</p><p>In our Terraform setup, we’ll use a null_resource with a local-exec provisioner to execute this script automatically.</p><pre>resource &quot;null_resource&quot; &quot;patch_aws_auth&quot; {<br>  count = local.pod_zeroscaler[&quot;enabled&quot;] ? 1 : 0<br><br>  triggers = {<br>    api_endpoint_up      = aws_eks_cluster.eks_cluster.endpoint<br>    lambda_bootstrapped = aws_iam_role.eks_lambda_role.id<br>    script_hash          = filemd5(&quot;${path.cwd}/files/scripts/aws_auth_patch.sh&quot;)<br>  }<br><br>  provisioner &quot;local-exec&quot; {<br>    working_dir = &quot;${path.cwd}/files/scripts&quot;<br>    command     = &quot;./aws_auth_patch.sh&quot;<br>    interpreter = [&quot;bash&quot;]<br>    environment = {<br>      ROLE_ARN = aws_iam_role.eks_lambda_role.arn<br>      GROUP    = kubernetes_cluster_role_binding.lambda_cluster_role_binding.subject[0].name<br>    }<br>  }<br>}</pre><p>This Terraform resource triggers the script execution whenever there are changes to the EKS cluster endpoint, the Lambda IAM role, or the script itself. The script is executed in the appropriate directory, and necessary environment variables are passed to it.</p><h3>Farewell 😊</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*cFENWd5Zs9WMOt3WgJAnlA.gif" /></figure><p>We’ve journeyed through an innovative approach to auto-scaling EKS deployments using Golang, AWS Lambda, EventBridge, and Terraform. Thank you for taking the time to read this article. I trust it has offered you valuable insights and practical knowledge to implement in your own DevOps endeavors. As we continue to navigate the ever-evolving landscape of cloud infrastructure and serverless architectures, remember the power of automation and efficient resource management. Keep pushing the boundaries, keep learning, and always strive for excellence!</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a><strong> | </strong><a href="https://blog.cubed.run/"><strong>Cubed</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=882b192ef7d1" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/eks-zeroscaler-scheduled-auto-acaling-to-zero-with-lambda-go-client-882b192ef7d1">EKS ZeroScaler: Scheduled auto-scaling to zero with Lambda GO client</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EKS Secret Management -with Golang, AWS ParameterStore and Terraform]]></title>
            <link>https://medium.com/cloud-native-daily/eks-secret-management-with-golang-aws-parameterstore-and-terraform-b4c8c7ee1f9?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4c8c7ee1f9</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[secrets]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devsecops]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Fri, 28 Jul 2023 08:36:52 GMT</pubDate>
            <atom:updated>2023-07-31T13:53:37.203Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>EKS Secret Management — </strong>with Golang, AWS ParameterStore and Terraform</h3><h4>An effective method of Kubernetes secret management using Golang, AWS ParameterStore, OIDC, and Terraform</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*YdiUEyIXfxsM309FNkgGcQ.png" /><figcaption>Solution Architecture</figcaption></figure><p>Hey Folks! In this article, we are going to delve into a robust approach to Kubernetes secret management by utilizing the efficiency of Golang, the security and flexibility of AWS ParameterStore, the authentication power of OIDC, and the infrastructure-as-code advantages of Terraform. We will explore ways to enhance your cloud-based applications and significantly bolster your security posture, providing you with a comprehensive understanding of this innovative strategy for revolutionizing your secret management processes.</p><p>Keep in mind that we have a few prerequisites. To fully engage with the material and examples we provide, you’ll need an <strong>AWS Account, an EKS Cluster, and a configured Terraform project</strong> with the AWS provider.</p><h3><strong>SSM Parameters</strong></h3><p>To kick things off, let’s explore how we can manage secrets using AWS Systems Manager (SSM) Parameter Store. This service from AWS provides secure, hierarchical storage for configuration data management and secrets. Leveraging the Parameter Store can significantly enhance the security posture of your applications by segregating and securing sensitive information like database passwords, license codes, and API keys.</p><p>Let’s consider a Terraform script to create these SSM parameters, starting with a locals block. This block includes the projects in which we want to manage the secrets and the keys that need to be stored securely.</p><pre>locals {<br>  projects = {<br>    demo-project = {<br>      team_id                 = &quot;demo_team&quot;<br>      namespace               = &quot;demo&quot;<br>      platform                = &quot;fargate&quot;<br>      fqdn                    = &quot;test.development.kelvatek.com&quot;<br>      ssm_secret_keys         = [&quot;AMQ_USER&quot;, &quot;AMQ_PASS&quot;]<br>      deployment_grace_period = 60<br>      vpa_update_mode         = &quot;Initial&quot;<br>      svc_config              = {<br>        service_port   = 8080<br>        target_port    = 80<br>        type           = &quot;NodePort&quot;<br>        lb_access_mode = &quot;internet-facing&quot;<br>        alb_group      = &quot;demo&quot;<br>      }<br>      hpa_config = {<br>        min           = 1<br>        max           = 3<br>        mem_threshold = 80<br>        cpu_threshold = 60<br>      }<br>    }<br>  }<br><br>  ssm_secret_keys = { for v in flatten([<br>    for project_name, parameters in local.projects : [<br>      for key in try(parameters.ssm_secret_keys, []) : {<br>        key          = key,<br>        namespace    = parameters.namespace,<br>        project_name = project_name,<br>        team_id      = try(parameters.team_id, var.cluster_namespace)<br>      }<br>    ]<br>    ]) : &quot;${v.namespace}.${v.project_name}.${v.key}&quot; =&gt; v<br>  }<br>}<br><br><br>resource &quot;aws_ssm_parameter&quot; &quot;project_ssm_secrets&quot; {<br>  for_each = local.ssm_secret_keys<br>  name     = &quot;/eks/${var.cluster_name}/${each.value.namespace}/${each.value.project_name}/${each.value.key}&quot;<br>  type     = &quot;SecureString&quot;<br>  value    = &quot;placeholder&quot;<br>  tags = merge(local.default_tags, {<br>    &quot;eks.namespace&quot; = each.value.namespace<br>    &quot;eks.project&quot;   = each.value.project_name<br>    &quot;eks.team_id&quot;   = each.value.team_id<br>    }<br>  )<br>  lifecycle {<br>    ignore_changes  = [value]<br>    prevent_destroy = true<br>  }<br>}</pre><p>Please note that in our locals block, several parameters are of particular importance for this tutorial:</p><ol><li>ssm_secret_keys: This specifies the secrets that we need to securely manage. These are essentially the names of the secrets we are storing in the AWS ParameterStore.</li><li>namespace: This identifies the Kubernetes namespace where our resources will be located.</li><li>team_id: It&#39;s used to denote the team that owns the particular resource. It is optional, but further it can be used for <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction_attribute-based-access-control.html">ABAC for AWS</a>.</li><li>project_name: This is the name of the project under which the resources will fall.</li></ol><p>These are used in our aws_ssm_parameter block to create secure parameters, which are initially set with placeholder values.</p><p>The remaining parameters, such as platform, fqdn, deployment_grace_period, svc_config, and hpa_config, although not explicitly used in our SSM parameters creation script, can be utilized within the same Terraform project to create various resources with AWS and Kubernetes providers. These could include load balancers, Horizontal Pod Autoscalers, and other vital components for our cloud infrastructure, and they contribute to the flexibility and comprehensiveness of the system we are setting up.</p><p>The aws_ssm_parameter block creates the SSM parameters. Each SSM parameter is given a placeholder value to initialize it. The actual values will be input later by a dedicated team member, such as a developer or a DevOps engineer, with sufficient permissions. This can be done via the AWS console or command-line interface (CLI).</p><p>It’s important to note that storing these values directly from the Terraform project is not advisable because they would end up in the Terraform state. This is a situation we want to avoid for security reasons, as we don’t want sensitive information like secrets stored in the Terraform state.</p><h3>InitContainer with GO binary</h3><p>Moving on to the next crucial step, we need to prepare the init container Golang binary. This binary will fetch our secrets from the AWS Parameter Store and write them into a file. Kubernetes will then load these secrets from the file into environment variables for use by our applications.</p><p>For this, we’ll write a small program in Go. The script’s operation can be summarized as follows: It creates an AWS session, uses the SSM client to fetch the secrets we stored earlier, writes the secrets into an environment file (.env), and then stores this file in a specific path (/etc/ssm/). The environment variables stored in this file will be available to other containers in the pod once loaded.</p><blockquote>Here is the Go code for our init container</blockquote><pre>package main<br><br>import (<br> &quot;bufio&quot;<br> &quot;fmt&quot;<br> &quot;github.com/aws/aws-sdk-go/aws&quot;<br> &quot;github.com/aws/aws-sdk-go/aws/session&quot;<br> &quot;github.com/aws/aws-sdk-go/service/ssm&quot;<br> &quot;os&quot;<br> &quot;path&quot;<br>)<br><br>func main() {<br> sess, err := session.NewSession(&amp;aws.Config{<br>  Region: aws.String(os.Getenv(&quot;AWS_REGION&quot;)),<br> })<br><br> if err != nil {<br>  fmt.Println(&quot;Failed to create session,&quot;, err)<br>  return<br> }<br><br> ssmSvc := ssm.New(sess)<br><br> eks_cluster := os.Getenv(&quot;EKS_CLUSTER&quot;)<br> namespace := os.Getenv(&quot;NAMESPACE&quot;)<br> ci_project_name := os.Getenv(&quot;CI_PROJECT_NAME&quot;)<br><br> ssmPath := fmt.Sprintf(&quot;/eks/%s/%s/%s&quot;, eks_cluster, namespace, ci_project_name)<br><br> paramDetails := &amp;ssm.GetParametersByPathInput{<br>  Path:           aws.String(ssmPath),<br>  WithDecryption: aws.Bool(true),<br> }<br><br> resp, err := ssmSvc.GetParametersByPath(paramDetails)<br> if err != nil {<br>  fmt.Println(&quot;Failed to get parameters,&quot;, err)<br>  return<br> }<br><br> file, err := os.Create(&quot;/etc/ssm/.env&quot;)<br> if err != nil {<br>  fmt.Println(&quot;Failed to create file,&quot;, err)<br>  return<br> }<br> defer file.Close()<br><br> writer := bufio.NewWriter(file)<br> for _, param := range resp.Parameters {<br>  name := path.Base(*param.Name)<br>  value := *param.Value<br>  writer.WriteString(fmt.Sprintf(&quot;export %s=%s\n&quot;, name, value))<br> }<br><br> err = writer.Flush()<br> if err != nil {<br>  fmt.Println(&quot;Failed to write to file,&quot;, err)<br> }<br> fmt.Println(&quot;env file created successfully&quot;)<br>}</pre><blockquote>dockerfiile</blockquote><pre>FROM golang:1.20-alpine as BUILD<br>WORKDIR /app<br>COPY . .<br>RUN go build -o main<br><br>FROM alpine:3.16 AS RUNTIME<br>WORKDIR /app<br>COPY --from=BUILD /app/main .<br>CMD [&quot;./main&quot;]</pre><p>In this Go script:</p><p>- We start by creating a new AWS session and initializing an SSM client using the aws-sdk-go package.<br>- We retrieve the environment variables for the EKS cluster, the namespace, and the project name. These will be used to construct the path to our secrets stored in the SSM Parameter Store.<br>- With the SSM client, we fetch the secrets from the Parameter Store using the GetParametersByPath method. This method retrieves all parameters within the provided path.<br>- We then create a .env file and write the fetched parameters into this file. Each line in the file will contain one secret, with the syntax export SECRET_NAME=secret_value.</p><p>Great, with the init container’s Go script ready, the next step is building this into a Docker image and pushing it to the Amazon Elastic Container Registry (ECR). Ideally, this should be part of your CI/CD process. Here’s a condensed guide to help you achieve this manually:</p><pre># Build the image, authenticate Docker to ECR, and push the image<br>docker build -t ssm-init-container . <br>aws ecr get-login-password | docker login --username AWS --password-stdin your-account-id.dkr.ecr.region.amazonaws.com<br>aws ecr create-repository - repository-name ssm-init-container<br>docker tag ssm-init-container:latest your-account-id.dkr.ecr.region.amazonaws.comssm-init-container:latest <br>docker push your-account-id.dkr.ecr.region.amazonaws.com/ssm-init-container:latest</pre><p>Please replace ‘your-account-id’ and ‘region’ with your AWS account ID and your region, respectively.</p><h3>OIDC Federated Access for EKS Pods</h3><p>Before we proceed to actual deployments, we need to ensure the IAM roles are correctly associated with the Kubernetes service accounts. This is vital for the secure operation of our applications, allowing them to access necessary AWS resources.</p><p>Our Terraform scripts will take care of this association using the concept of IAM Roles for Service Accounts (IRSA) in AWS EKS, facilitated by OpenID Connect (OIDC) federation. It’s a recommended best practice to follow to ensure fine-grained access control to AWS services, directly from within the EKS environment. This eliminates the need to provide broad access permissions at the node level and greatly enhances our application’s security.</p><p>For enhanced security, AWS EKS allows IAM roles to be assigned to Kubernetes Service Accounts through a feature called IAM Roles for Service Accounts (IRSA). This mechanism uses OpenID Connect (OIDC) federation to drive the mapping between Kubernetes service accounts and AWS IAM roles. This section shows how to implement it using Terraform.</p><p>The following guides from AWS EKS are recommended reading to gain a deeper understanding of the topic:</p><ol><li><a href="https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html">Enabling IAM roles for service accounts on your cluster</a></li><li><a href="https://docs.aws.amazon.com/eks/latest/userguide/create-service-account-iam-policy-and-role.html">Creating an IAM role and policy for your service account</a></li></ol><p>Here’s the Terraform code that sets up the IAM roles, policies and associates them with Kubernetes service accounts:</p><pre># Assume Role Policy for service accounts<br>data &quot;aws_iam_policy_document&quot; &quot;service_account_assume_role&quot; {<br>  for_each = local.projects<br>  statement {<br>    actions = [&quot;sts:AssumeRoleWithWebIdentity&quot;]<br>    effect  = &quot;Allow&quot;<br>    condition {<br>      test     = &quot;StringEquals&quot;<br>      variable = &quot;${replace(aws_iam_openid_connect_provider.oidc_provider_sts.url, &quot;https://&quot;, &quot;&quot;)}:aud&quot;<br>      values   = [&quot;sts.amazonaws.com&quot;]<br>    }<br>    condition {<br>      test     = &quot;StringEquals&quot;<br>      variable = &quot;${replace(aws_iam_openid_connect_provider.oidc_provider_sts.url, &quot;https://&quot;, &quot;&quot;)}:sub&quot;<br>      values   = [&quot;system:serviceaccount:${each.value.namespace}:${each.key}&quot;]<br>    }<br>    principals {<br>      identifiers = [aws_iam_openid_connect_provider.oidc_provider_sts.arn]<br>      type        = &quot;Federated&quot;<br>    }<br>  }<br>}<br><br># SSM Access permissions<br>resource &quot;aws_iam_role&quot; &quot;service_account_role&quot; {<br>  for_each           = local.projects<br>  assume_role_policy = data.aws_iam_policy_document.service_account_assume_role[each.key].json<br>  name               = &quot;project-${each.key}-service-account-role&quot;<br>  tags               = local.default_tags<br>}<br><br># IAM Policy for SSM secrets<br>data &quot;aws_iam_policy_document&quot; &quot;ssm_secret_policy&quot; {<br>  for_each = local.projects<br>  statement {<br>    effect = &quot;Allow&quot;<br>    actions = [&quot;ssm:GetParametersByPath&quot;, &quot;ssm:GetParameters&quot;]<br>    resources = [<br>      &quot;arn:aws:ssm:${local.region}:${local.account_id}:parameter/eks/${var.cluster_name}/${each.value.namespace}/${each.key}*&quot;<br>    ]<br>  }<br>}<br><br>resource &quot;aws_iam_policy&quot; &quot;ssm_secret_policy&quot; {<br>  for_each    = local.projects<br>  name        = &quot;project-${each.key}-ssm-access&quot;<br>  description = &quot;Policy to allow EKS pods/projects to access respective SSM parameters&quot;<br>  policy      = data.aws_iam_policy_document.ssm_secret_policy[each.key].json<br>  tags        = local.default_tags<br>}<br><br>resource &quot;aws_iam_role_policy_attachment&quot; &quot;service_account_role_ssm&quot; {<br>  for_each   = local.projects<br>  role       = aws_iam_role.service_account_role[each.key].name<br>  policy_arn = aws_iam_policy.ssm_secret_policy[each.key].arn<br>}</pre><p>This code sets up an IAM role for each service account, granting it permissions to access specific SSM parameters. The service account is then mapped to the IAM role using OIDC. It’s a great approach to securely handle permissions and access secrets in EKS environments.</p><p>Remember to replace the placeholders with actual values before running the script. Make sure you also have the appropriate AWS access and permissions to execute these commands.</p><p>With this setup, your applications running in Kubernetes can securely and efficiently access the resources they need to function, all while adhering to the principle of least privilege.</p><h3>Application Deployment</h3><p>With our Go-based init container built and securely stored in ECR, let’s move on to deploying a demo application. For this, we’ll need an `entrypoint.sh` shell script, a Dockerfile for our application, and a Kubernetes Deployment manifest.</p><p><em>Here’s your `entrypoint.sh` script</em></p><pre>#!/bin/sh<br># Source the secret file<br>source /etc/ssm/.env<br># Remove .env file<br>rm /etc/ssm/.env<br># Execute the main command<br>exec /app/main</pre><p><em>And, your Dockerfile for the demo application is</em></p><pre>FROM golang:1.20-alpine as BUILD<br>WORKDIR /build<br>COPY . .<br>RUN go mod tidy<br>RUN go build -o main<br>FROM alpine:3.16 AS RUNTIME<br>WORKDIR /app<br>COPY - from=BUILD /build .<br>RUN chmod +x entrypoint.sh<br>ENTRYPOINT [&quot;./entrypoint.sh&quot;]</pre><p>To give you a head start, here is a simple Go application you can deploy for testing purposes: <a href="https://github.com/WarDove/go-resume">Go Resume Demo</a></p><blockquote>Lastly, we have our Kubernetes Deployment manifest</blockquote><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br> namespace: ${NAMESPACE}<br> name: ${PROJECT_NAME}<br>spec:<br> selector:<br> matchLabels:<br> app.kubernetes.io/name: ${PROJECT_NAME}<br> replicas: 1<br> template:<br> metadata:<br> labels:<br> app.kubernetes.io/name: ${PROJECT_NAME}<br> app.kubernetes.io/environment: ${EKS_CLUSTER}<br> app.kubernetes.io/owner: Devops<br> spec:<br> serviceAccountName: ${PROJECT_NAME}<br> initContainers:<br> - name: secret-gopher<br> image: ${ECR_REGISTRY}/&lt;INIT_CONTAINER_NAME&gt;:latest<br> env:<br> - name: EKS_CLUSTER<br> value: ${EKS_CLUSTER}<br> - name: NAMESPACE<br> value: ${NAMESPACE}<br> - name: CI_PROJECT_NAME<br> value: ${PROJECT_NAME}<br> volumeMounts:<br> - name: envfile<br> mountPath: /etc/ssm/<br> subPath: .env<br> containers:<br> - image: ${BUILD_IMAGE}:${BUILD_TAG}<br> volumeMounts:<br> - name: envfile<br> mountPath: /etc/ssm/<br> subPath: .env<br> imagePullPolicy: Always<br> name: ${PROJECT_NAME}<br> ports:<br> - containerPort: 80<br> volumes:<br> - name: envfile<br> emptyDir: {}</pre><p>This sets us up for a Kubernetes deployment, which uses the init container we built previously to populate our environment variables from AWS SSM, securely managing our application’s secrets.</p><p>Here’s how it works:</p><ol><li>When the Kubernetes Pod starts, the init container is the first to run. It fetches the secret data from AWS SSM and writes it to a file named .env located in the /etc/ssm/ directory.</li><li>This directory is a shared volume (emptyDir) that&#39;s accessible to all containers in the Pod. The emptyDir volume is created when a Pod is assigned to a Node, and it exists as long as that Pod is running on that Node. The data in emptyDir is safe across container crashes and restarts.</li><li>Once the init container successfully writes the .env file, it exits, and the main application container starts.</li><li>The main application container reads the .env file from the shared volume, thus having access to the secret data. The entrypoint.sh script in the main container sources the .env file to set the environment variables. Then, it removes the .env file for added security, ensuring the secrets do not persist in the file system once they&#39;ve been loaded into the application&#39;s environment.</li><li>The main application then continues to run with the environment variables securely set, all without the secrets having to be explicitly stored or exposed outside the application’s memory.</li></ol><p>The following block of code can be added to your application to debug and verify that you are retrieving the secret parameters correctly from the AWS Systems Manager (SSM) Parameter Store.</p><pre>// Debug ssm fetching <br>key1 := os.Getenv(&quot;AMQ_USER&quot;)<br>key2 := os.Getenv(&quot;AMQ_PASS&quot;)<br><br>filePath := &quot;/app/ssm-vars&quot;<br><br>file, err := os.Create(filePath)<br>if err != nil {<br>	log.Fatalf(&quot;Failed to create file: %v&quot;, err)<br>}<br>defer file.Close()<br><br>content := fmt.Sprintf(&quot;KEY1=%s\nKEY2=%s\n&quot;, key1, key2)<br>err = os.WriteFile(filePath, []byte(content), 0644)<br>if err != nil {<br>	log.Fatalf(&quot;Failed to write file: %v&quot;, err)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*SEFgMkTZVvZMlHdtInWxEA.png" /><figcaption>confirm ssm param fetching</figcaption></figure><p>This piece of code reads the values of two environment variables, AMQ_USER and AMQ_PASS, which have been populated from the SSM Parameter Store. It then writes these values to a file for debugging purposes.</p><p>It’s important to understand that in a production environment, writing secrets to a file may expose them to additional security risks. This should only be used for debugging purposes and removed from the production code.</p><p>We achieved this secure management of secrets by following these steps:</p><ol><li><strong>Storing Secrets in AWS SSM Parameter Store</strong>: We stored our secrets securely in the AWS Systems Manager Parameter Store, which is a managed service that provides a secure and scalable way to store and manage configuration data.</li><li><strong>Fetch Secrets with an Init Container</strong>: We used an init container in our Kubernetes pod, which runs before our main application starts. This init container runs a Go program that fetches the secrets from the AWS SSM Parameter Store and writes them to an environment file.</li><li><strong>Populate Environment Variables</strong>: In the main container where our application runs, we used an entrypoint script that sources the environment file, thereby populating the environment variables with the secrets.</li><li><strong>Removal of .env file</strong>: To ensure that our secrets are not written to disk in the main container, the entrypoint script removes the .env file after sourcing it.</li><li><strong>Secrets in Memory</strong>: As a result of these steps, the secrets are only present in the memory of the running application process and nowhere else. They are not written to disk in the main container, and are not included in any container layers. This is a robust way to keep secrets secure.</li><li><strong>Security Best Practices</strong>: We also ensured security best practices such as setting appropriate IAM roles for access to AWS SSM Parameter Store, and used Kubernetes RBAC for restricting access to Kubernetes resources.</li></ol><h3><strong>Farewell 😊</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*uv4h5cjNXuThPEb_" /></figure><p>We’ve explored an effective method of Kubernetes secret management using Golang, AWS ParameterStore, OIDC, and Terraform. Thank you for reading this article. I hope it has provided you with valuable insight and can serve as a handy reference for your future projects. Keep exploring, keep learning, and continue refining your security practices!</p><h4>Further Reading:</h4><ul><li><a href="https://gethelios.dev/blog/golang-distributed-tracing-opentelemetry-based-observability/?utm_source=medium&amp;utm_medium=referral&amp;utm_campaign=cloud+native+daily">Golang Distributed Tracing - OpenTelemetry Based Observability -</a></li><li><a href="https://gethelios.dev/blog/serverless-observability/?utm_source=medium&amp;utm_medium=cloud+native+daily">Serverless observability, monitoring, and debugging explained</a></li><li><a href="https://gethelios.dev/opentelemetry-a-full-guide/?utm_source=medium&amp;utm_medium=cloud+native+daily">OpenTelemetry: A full guide</a></li><li><a href="https://gethelios.dev/blog/kubernetes-monitoring-opentelemetry/?utm_source=medium&amp;utm_medium=cloud+native+daily">Kubernetes Monitoring with OpenTelemetry</a></li><li><a href="https://gethelios.dev/blog/lambda-monitoring-combining-the-three-pillars-of-observability-to-reduce-mttr/?utm_source=medium&amp;utm_medium=cloud+native+daily">Lambda monitoring: Combining the three pillars of observability to reduce MTTR</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4c8c7ee1f9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/eks-secret-management-with-golang-aws-parameterstore-and-terraform-b4c8c7ee1f9">EKS Secret Management -with Golang, AWS ParameterStore and Terraform</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Journey: Standalone Server to Scalable Golang App with CI/CD]]></title>
            <link>https://aws.plainenglish.io/aws-journey-standalone-server-to-scalable-golang-app-with-ci-cd-1be64094961a?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1be64094961a</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[scaling]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ci]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Mon, 24 Jul 2023 15:07:08 GMT</pubDate>
            <atom:updated>2024-02-28T18:04:30.058Z</atom:updated>
            <content:encoded><![CDATA[<h4>Architecture and implementation of AWS Developer Tools for continuous integration &amp; continuous delivery</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*g18j5iiGGlsda8Zd.png" /></figure><h3>An Evolutionary Path from Standalone Server to Scalable</h3><p>Architecture and implementation of AWS Developer Tools for continuous integration &amp; continuous delivery</p><h3>Introduction</h3><p>In today’s rapidly evolving technological landscape, designing scalable and resilient software applications is essential. The journey of scaling an application’s architecture involves various stages, from single-tier to multi-tier, with each tier bringing a unique set of complexities and advantages. In this blog, we’re going to delve into the process of transitioning a simple Golang application from a single-tier architecture to a more complex, but efficient, three-tier design.</p><p>Moreover, to ensure the stability and reliability of the application, a robust Continuous Integration and Continuous Delivery (CI/CD) pipeline becomes paramount. We’ll also discuss the implementation of an effective CI/CD system for our upgraded Golang application.</p><p>Join me in this exciting expedition, as we navigate the intricacies of application scaling and pipeline development, providing you with practical insights and methodologies for your own software engineering journeys.</p><p>As we undertake this transformation journey for our demo Golang application, we’re not working alone. We’ll be leveraging a host of powerful tools and services from Amazon Web Services (AWS).</p><p>For our user management, we’ll utilize AWS Cognito, which will act as our user pool, ensuring we can effectively manage, authenticate, and authorize our users. Handling secrets securely is a fundamental part of any application, for which we’ll employ the AWS Systems Manager Parameter Store.</p><p>Our data persistence layer will be anchored on AWS Relational Database Service (RDS) using the robust Postgres engine, providing us with scalable and reliable database services. To handle our application deployment, we’ll implement AWS CodePipeline, CodeBuild, and CodeDeploy, forming our Continuous Integration and Continuous Delivery (CI/CD) pipeline. These services will streamline our development process by automating build, test, and deployment phases.</p><p>Monitoring and logging are crucial aspects of maintaining the health of our application. Here, AWS CloudWatch steps in, offering us comprehensive visibility over our application and resources.</p><p>Our application will be hosted on EC2 instances, providing secure and resizable compute capacity in the cloud. To ensure our application can handle varying loads efficiently, we’ll incorporate AWS Auto Scaling groups. This service will dynamically adjust our EC2 instance count based on real-time demands.</p><p>Finally, to secure and route end-user requests to our application, we’ll leverage AWS Certificate Manager (ACM) and Route 53. ACM will take care of the SSL/TLS certificate needs for our application, while Route 53, AWS’s scalable Domain Name System (DNS) service, will handle the routing of end-user requests to our application hosted on EC2 instances.</p><p>In essence, we are going to fully immerse our application in a cloud-native environment, harnessing the power of AWS services to ensure scalability, reliability, and operational efficiency. Follow along as we detail this evolutionary journey of our application, providing valuable insights into the intricacies of multi-tier architecture and the practical application of AWS services.</p><h3>References:</h3><p>For further reading and deeper insights, feel free to check out the following resources:</p><p><strong>Amazon Web Services Documentation</strong>:<br> <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html">Instance Metadata and User Data</a>: Provides a comprehensive guide on how to retrieve instance metadata.<br> <a href="https://aws.amazon.com/blogs/database/securely-connect-to-an-amazon-rds-or-amazon-ec2-database-instance-remotely-with-your-preferred-gui/">Connecting to an Amazon RDS or Amazon EC2 Database Instance Remotely</a>: A blog post that guides you through securely connecting to a remote database instance.<br> <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/installing-cloudwatch-agent-ssm.html">Installing the CloudWatch Agent Using AWS Systems Manager</a>: Guides you through the installation process of the CloudWatch agent using AWS Systems Manager.</p><p><strong>GitHub Documentation</strong>:<br> <a href="https://docs.github.com/en/rest/commits/statuses">Commit Status API</a>: An API reference on how to check and set commit statuses.</p><p>PostgreSQL Documentation:<br> <a href="https://www.postgresql.org/docs/current/app-pg-dumpall.html">pg_dumpall</a>: Documentation on how to use the pg_dumpall utility.</p><p><strong>GitHub Repositories</strong>:</p><p>In the course of our transformation journey, we will be referencing two key GitHub repositories:</p><p><a href="https://github.com/WarDove/aws-go-demo">AWS Go Demo</a>: This repository plays a central role in our demonstration, featuring an AWS Golang application as a sample. Different branches of this repository will serve as references during various stages of our architectural evolution, providing you with practical examples and insights.</p><p><a href="https://github.com/WarDove/commit-status-lambda">Commit Status Lambda</a>: As we reach the final stages of our transformation, the implementation of this Lambda function becomes essential. This repository helps us set commit statuses on GitHub, a crucial component in our CI/CD pipeline that enhances transparency and facilitates better tracking of code changes.</p><p>These repositories will serve as a roadmap guiding us through our exploration. Their practical implementations offer invaluable insights and further deepen your understanding of the architectural transition and CI/CD pipeline establishment.</p><p>Happy learning!</p><h3>Part 1: Standalone application</h3><p>Our transformation journey begins from a simple standalone server architecture. A standalone server, essentially a single-tier architecture, is the most basic form of a computing system. Here, the user interface, application logic, and data storage all reside in the same environment.</p><h4>Architecture diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*UXfF8ki5OkBg_b3N.png" /></figure><p><strong>AWS Must-read Documentation references for Part 1.</strong></p><ul><li><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html">What is Amazon EC2?</a></li><li><a href="https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/instancedata-data-retrieval.html">Retrieve instance metadata</a></li><li><a href="https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html">What is Amazon VPC?</a></li><li><a href="https://docs.aws.amazon.com/vpc/latest/userguide/configure-subnets.html">Subnets for your VPC</a></li><li><a href="https://docs.aws.amazon.com/vpc/latest/userguide/vpc-security-groups.html">Control traffic to your AWS resources using security groups</a></li><li><a href="https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/Welcome.html">What is Amazon Route 53?</a></li><li><a href="https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html">Configure route tables</a></li></ul><p><strong>Note:</strong> <em>While the information provided here is based on AWS documentation, extra research is always welcome and recommended. Exploring additional resources can provide deeper insights and a broader understanding of the subject matter.</em></p><p><strong>Step-by-step Implementation</strong></p><p>In the first part of our application’s transformation journey, we’ll lay down the foundation of our infrastructure and deploy the initial version of our Golang application on an AWS EC2 instance. Let’s go through the steps:</p><ol><li>Create and configure a Virtual Private Cloud (VPC) in your AWS environment.</li><li>Enable DNS hostnames for your VPC. This is essential for our instances to communicate with each other using DNS hostnames.</li><li>Create an Internet Gateway (IGW) and attach it to the VPC. This gateway allows our VPC to communicate with the internet.</li><li>Set up Route Tables for your VPC to control the routing of traffic.</li><li>Configure public and private subnets within your VPC.</li><li>Create a Security Group for your EC2 instance. This will serve as a virtual firewall to control inbound and outbound traffic.</li><li>Generate or import a key-pair. This is crucial for secure SSH connections to your instance.</li><li>Launch an EC2 instance using the Ubuntu 20.04 LTS AMI and attach an Elastic IP (EIP) to it.</li><li>Connect to the instance via SSH over EIP and create a directory for your application:</li><li>directory for your application:</li></ol><pre>$ sudo mkdir /etc/demo-app<br>  $ sudo -i<br>  $ echo APPDIR=/etc/demo-app &gt;&gt; /etc/environment<br>  $ exit</pre><h4>Database &amp; Application</h4><ul><li>Install Postgres stable version on the instance and configure it:</li></ul><pre>$ sudo apt update &amp;&amp; sudo apt install -y postgresql postgresql-contrib<br>$ sudo systemctl start postgresql.service<br>$ sudo -u postgres createuser demouser<br>$ sudo -u postgres createdb awsgodemo<br>$ sudo -u postgres psql<br>postgres=# alter user demouser with encrypted password &#39;demopass&#39;;<br>postgres=# grant all privileges on database awsgodemo to demouser;<br>postgres=# \l<br>postgres=# \q</pre><ul><li>Check out the demo-1 branch from the <a href="https://github.com/WarDove/aws-go-demo">AWS Go Demo</a> repository. Build the application and deploy the executable main (make sure it&#39;s executable) to the APPDIR you created.</li></ul><p><a href="https://github.com/WarDove/aws-go-demo">https://github.com/WarDove/aws-go-demo</a></p><p>Run and test the application:</p><pre>$ sudo chmod +x main<br>$ ./main</pre><ul><li>Configure Route53 records for your domain and add user entries via the “/log” route of your application.</li></ul><p>By following these steps, you’ll have successfully deployed the initial version of your Golang application within a robust, secure AWS environment, setting the stage for further enhancements and the upcoming transition to a three-tier architecture.</p><h3>Part 2: Migration to RDS, User Pool Creation, Secrets Management and logging</h3><p>In the second part of our journey, we shift gears towards a more resilient and scalable configuration by migrating our standalone database to AWS RDS. Additionally, we’ll enhance our user management and security by creating a user pool via AWS Cognito and transferring our secrets to the AWS Systems Manager (SSM) Parameter Store. Let’s delve into these enhancements:</p><ol><li><strong>Migrating Database to AWS RDS</strong>: As a managed service, AWS RDS offers scalability, reliability, and ease of management, which is why we’ll migrate our standalone Postgres database to RDS. The process involves creating an RDS instance, exporting data from our standalone database, and importing it into the RDS instance.</li><li><strong>User Pool Creation with AWS Cognito</strong>: User management is a key aspect of any application. AWS Cognito provides an easy-to-use user identity and data synchronization service that helps us manage user pools. We’ll create a user pool for our application, allowing us to handle user registration, authentication, and account recovery seamlessly.</li><li><strong>Secrets Management with SSM Parameter Store</strong>: For improved security and centralized management, we’ll transfer our application secrets (such as database credentials) to the AWS SSM Parameter Store. It provides secure, hierarchical storage for configuration data management and secrets management.</li><li><strong>Deploying Application as a Linux Service</strong>: To ensure our application is always up and running, we’ll deploy it as a Linux service. This allows the application to start automatically upon system boot, restart after a crash, and benefit from various other systemd features.</li><li><strong>Logging with AWS CloudWatch</strong>: For efficient monitoring and debugging, we’ll set up our application to write logs to AWS CloudWatch. This service collects and tracks metrics, collects and monitors log files, sets alarms, and automatically reacts to changes in AWS resources.</li></ol><p>Stay tuned for the detailed steps of this part, as we further enhance the robustness and security of our application by harnessing the power of AWS services. By the end of this part, we’ll be one step closer to a full-fledged three-tier architecture, providing a scalable and reliable platform for our evolving Golang application.</p><h4>Architecture diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*7T5byQknftgVnzka.png" /></figure><p><strong>AWS Must-read Documentation references for Part 2.</strong></p><ul><li><a href="https://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html">What is Amazon Cognito?</a></li><li><a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html">AWS Systems Manager Parameter Store</a></li><li><a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html">What is Amazon CloudWatch Logs?</a></li><li><a href="https://aws.amazon.com/rds/">Amazon Relational Database Service (RDS) - AWS</a></li></ul><p><strong>Step-by-step Implementation</strong></p><p>Now that we have outlined our strategy, let’s delve into the step-by-step implementation of each aspect.</p><p><strong>Migrate Database to RDS</strong>:</p><ul><li>Start by creating a subnet group, security group, and a Multi-AZ RDS Database with Postgres Engine v 12.14.</li><li>Utilize the PostgreSQL native tools pg_dump and pg_restore to migrate the database:</li></ul><pre># Backup your database<br> $ pg_dump -Fc -b -v -f awsgodemo.sql -d awsgodemo<br> # Connect to your RDS instance and set up your database and user<br> $ psql -h &lt;rds endpoint&gt; -U postgres -d postgres<br> postgres=&gt; create database awsgodemo;<br> postgres=&gt; create role demouser;<br> postgres=&gt; alter user demouser with login;<br> postgres=&gt; alter user demouser with encrypted password &#39;demopass&#39;;<br> postgres=&gt; grant all privileges on database awsgodemo to demouser;<br> # Restore your database to your RDS instance<br> $ pg_restore -v -h &lt;rds endpoint&gt; -U &lt;username&gt; -d awsgodemo awsgodemo.sql<br> # Add new upcoming column to our table<br> $ psql -h &lt;rds endpoint&gt; -U demouser -d awsgodemo<br> awsgodemo=&gt; alter table userlog add column email varchar(255);<br> awsgodemo=&gt; update userlog set email = &#39;NOEMAIL&#39; where email is null;</pre><ul><li>Remove the local database from the instance:</li></ul><pre>$ apt list --installed | grep postgres<br> $ apt remove postgresql*<br> $ sudo apt-get autoremove<br> $ rm -rf /etc/postgresql<br> $ rm -rf /var/lib/postgresql<br> $ rm -rf /etc/postgresql-common/</pre><p><strong>Create Cognito user pool</strong>:</p><ul><li>Set up email and password authentication, enable the “forgot password” feature and email confirmation.</li><li>Generate a client secret and add authentication flows.</li><li>Note down the Client ID and Client Secret for future use.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/0*FNN85EKXHfsJAJsg.png" /></figure><p><strong>Create Instance Role</strong>:</p><ul><li>Attach the following IAM Policy to allow our instance to access SSM Parameters:</li></ul><pre>{<br>     &quot;Version&quot;: &quot;2012-10-17&quot;,<br>     &quot;Statement&quot;: [<br>        {<br>           &quot;Sid&quot;: &quot;AllowSSM&quot;,<br>           &quot;Effect&quot;: &quot;Allow&quot;,<br>           &quot;Action&quot;: [<br>              &quot;ssm:GetParameter&quot;<br>           ],<br>           &quot;Resource&quot;: [<br>              &quot;arn:aws:ssm:&lt;region&gt;:&lt;account_id&gt;:parameter/demo-app/*&quot;<br>           ]<br>        }<br>     ]<br>  }</pre><p><strong>Create SSM Parameters</strong>:</p><ul><li>Store your application secrets (like database credentials and Client ID/Secret from Cognito) in the SSM Parameter Store as SecureString.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/0*FOdZCQICwpWPbL6L.png" /></figure><p>NOTE: SESSION_ENCRYPTION_SECRET should be a random hashed string The sessionEncryptSecret in the NewCookieStore function is used as a secret key for encrypting and authenticating the session data stored in the cookie. This ensures that the session data cannot be tampered with or read by unauthorized parties. It&#39;s important to keep this secret key secure and not expose it in your codebase or version control.</p><p><strong>Deploy the application</strong>:</p><ul><li>Check out the demo-2 branch from the <a href="https://github.com/WarDove/aws-go-demo">AWS Go Demo</a> repository, build and deploy the application.</li></ul><pre>$ sudo chmod +x main<br>$ ./main</pre><p><strong>Create a SystemD service for the application</strong>:</p><ul><li>Set up a new service file for your application and manage it with systemd:</li></ul><pre>$ sudo vi /etc/systemd/system/demo-app.service<br><br>  [Unit]<br>  Description=My GO Service<br>  After=network.target<br><br>  [Service]<br>  User=root<br>  ExecStart=/etc/demo-app/main<br>  WorkingDirectory=/etc/demo-app<br>  Restart=always<br>  StandardOutput=file:/etc/demo-app/main.log<br>  StandardError=file:/etc/demo-app/main.log<br><br>  [Install]<br>  WantedBy=multi-user.target<br><br>  $ sudo systemctl daemon-reload<br>  $ sudo systemctl enable (--now) demo-app<br>  $ sudo systemctl start demo-app</pre><p><strong>Install and configure CloudWatch Logs with AWS Systems Manager</strong>:</p><ul><li>Follow the official AWS documentation to install and configure CloudWatch Logs with AWS Systems Manager. Official AWS documentation link is provided in <a href="https://dev.to/wardove/aws-journey-standalone-server-to-scalable-golang-app-with-cicd-3ohj#References">References</a>.</li><li>Check the ssm-agent version and install the CloudWatch agent, configure it, and store the configuration in the Parameter Store. Finally, verify your setup by checking the CloudWatch log group and stream.</li></ul><p>After following these steps, your Golang application is now running on a resilient and scalable setup, logging to CloudWatch, and using AWS Cognito for user management and SSM Parameter Store for secrets management. We’ve successfully achieved the second part of our journey to a three-tier architecture. Stay tuned for the final transformation!</p><h3>Part 3: Transitioning to a Scalable and Secure Infrastructure</h3><p>In the final part of our journey, we’ll adjust our networking resources for enhanced security and scalability, introduce Auto Scaling groups (ASGs) for resilience and elasticity, and implement secure HTTPs communication with an Application Load Balancer (ALB). This step will see our application evolve into a scalable, three-tier architecture.</p><h4>Architecture diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*1GvBF0v4UOHhKh-r.png" /></figure><p><strong>AWS Must-read Documentation references for Part 2.</strong></p><ul><li><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html">Elastic IP addresses</a></li><li><a href="https://docs.aws.amazon.com/vpc/latest/userguide/nat-gateway-scenarios.html">NAT gateway use cases</a></li><li><a href="https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/connect-to-an-amazon-ec2-instance-by-using-session-manager.html">Connect to an Amazon EC2 instance by using Session Manager</a></li><li><a href="https://docs.aws.amazon.com/acm/latest/userguide/acm-overview.html">What Is AWS Certificate Manager?</a></li></ul><p><strong>Step-by-step Implementation</strong></p><ol><li><strong>Adjust Networking Resources</strong>: Create Network Address Translation (NAT) gateways and Elastic IPs (EIPs). These will allow instances in the private subnets to connect to the internet or other AWS services. Adjust the routing tables of the private subnets to direct their default routes to these NAT gateways.</li><li><strong>Capture Amazon Machine Image (AMI) of the EC2 Instance</strong>: Create an Amazon Machine Image (AMI) of our EC2 instance. An AMI is like a blueprint of the running instance, which includes the instance’s operating system, the applications running on it, and their configurations.</li><li><strong>Create Launch Template from the AMI</strong>: Use the AMI to create a launch template. A launch template is a specification that contains the settings and configurations to launch instances. You can define settings such as the instance type, the AMI ID, the key pair, and many others.</li><li><strong>Create Auto Scaling Group</strong>: Create an Auto Scaling Group (ASG) using the launch template. ASGs enable you to ensure that you have the correct number of Amazon EC2 instances available to handle the load for your application. You create collections, or groups, of EC2 instances, and then set policies that control when instances are launched or terminated.</li><li><strong>Migrate Workload to Private Subnet 1</strong>: For enhanced security, migrate your workload to the first private subnet. Private subnets are not exposed directly to the internet, which helps reduce the potential attack surface for your application.</li><li><strong>Create ASG Target Group for ALB</strong>: Create a target group for the Application Load Balancer (ALB) and associate it with the ASG. The ALB will distribute incoming traffic across the EC2 instances that are registered with these target groups.</li><li><strong>Create ACM Certificate</strong>: Create an Amazon Certificate Manager (ACM) certificate for your domain and verify it. ACM handles the complexity of creating and managing public SSL/TLS certificates for your AWS based websites and applications.</li><li><strong>Set Up the Application Load Balancer (ALB)</strong>: Create the ALB, ALB listeners, and a dedicated security group for it. Adjust the security group attached to the instances accordingly. The ALB listener should be set up for HTTP (port 80) and redirect to HTTPS (port 443). Another listener for HTTPS (port 443) should target the target group with the ASG.</li><li><strong>Update Route 53 Record</strong>: Adjust an A Record in Route 53 to point to the ALB as an alias. This allows the domain to resolve to the ALB, and therefore to the instances that are serving our application.</li></ol><h3>Part 4: Implementing Continuous Integration/Continuous Deployment (CI/CD)</h3><h4>Architecture diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*xCTXg_MTrrLAD_i2.png" /></figure><p>In this section, we will set up a CI/CD pipeline using AWS CodeBuild, CodeDeploy, and CodePipeline. The pipeline will automatically build and deploy our application whenever changes are pushed to the GitHub repository.</p><p>To manage and visualize the whole workflow, we’ll leverage AWS CodePipeline, and to notify GitHub about the state of the deployment process, we will use a Lambda function.</p><p><strong>Step-by-Step Implementation</strong></p><p><strong>Step 1: Setup AWS CodeBuild</strong></p><p>CodeBuild will be used to build our application. We will instruct CodeBuild on how to handle the building of our application via a buildspec file. The file will list the commands to be executed during the build phase and specify the artifacts to be created. Here is the buildspec file that we&#39;ll use:</p><pre>version: 0.2<br><br>phases:<br>  build:<br>    commands:<br>      - echo Build started on `date`<br>      - go build -o main<br><br>  post_build:<br>    commands:<br>      - echo Build completed on `date`<br><br>artifacts:<br>  files:<br>    - main<br>    - templates/**/*<br>    - scripts/**/*<br>    - appspec.yml<br>    - .env</pre><p><strong>Step 2: Setup AWS CodeDeploy</strong></p><p>We will use AWS CodeDeploy to deploy our application. CodeDeploy will use the build artifacts created by CodeBuild and deploy them to the application servers managed by an Auto Scaling group. The deployment instructions are defined in the appspec.yml file in the demo-4-cicd-integration branch of our GitHub repository.</p><p><strong>Step 3: Setup AWS CodePipeline</strong></p><p>AWS CodePipeline will manage the overall workflow. It will listen for changes to the demo-4-cicd-integration branch of the GitHub repository, trigger a build on CodeBuild whenever changes are pushed, and if the build is successful, it will trigger a deployment on CodeDeploy. Note that demo-4 branch is already merged to main, and we are triggering deployments on push to main.</p><p>We will use AWS CodeStar connections to connect our GitHub repository to CodePipeline. This connection allows CodePipeline to get the source code from the GitHub repository using the GitHub version 2 source action.</p><p><strong>Step 4: Setup AWS Lambda for GitHub Commit Status updates</strong></p><p>The AWS Lambda function will listen to the state change events of CodePipeline, format the received data to be compatible with GitHub’s Commit Status API, and send the status updates to GitHub.</p><p>For this, we will use the code from the <a href="https://github.com/WarDove/commit-status-lambda">Commit Status Lambda</a> repository. Make sure to provide the necessary permissions for the Lambda function to interact with GitHub and CodePipeline. Analyze the readme carefully.</p><p><a href="https://github.com/WarDove/commit-status-lambda">GitHub - WarDove/commit-status-lambda</a></p><p>By following these steps, you can establish a CI/CD pipeline for your application. This pipeline will enable you to automatically build and deploy your application while also providing status updates directly to GitHub.</p><p>Remember that CI/CD pipelines are a fundamental part of modern application development practices. They allow for quicker, more reliable deployments and help maintain high standards of code quality. By integrating our pipeline with GitHub, we can ensure that our application remains up-to-date and ready for deployment at any time.</p><p><strong>Step 5: Seamless Scalability: Containerizing and Orchestrating with Amazon ECS</strong></p><h4>Architecture diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fn5pzpgwsDZ8WAUmJfEhFA.png" /></figure><p>In this next chapter we expand our journey into the containerized world, embracing the agility and efficiency of Docker containers managed by Amazon Elastic Container Service (ECS). Beyond the foundational steps of containerization and orchestration, we introduce a sophisticated integration with GitHub: Pull-request web-hook triggers designed to simulate test cases before deploying the application upon PR merge. This addition underscores our commitment to maintaining a robust, continuous integration and delivery pipeline, ensuring our application remains resilient and reliable through every change.</p><p>we’ll explore the transformative journey of our Golang application through containerization and its deployment using Amazon Elastic Container Service (ECS). This pivotal shift to a containerized workflow, facilitated by reconfigured AWS CodePipeline, promises enhanced consistency across development, testing, and production environments. We’ll delve into the myriad benefits of containerization and meticulously guide through the processes required to harness Amazon ECS’s full potential, ensuring a seamless, scalable application deployment framework.</p><h3>In Plain English 🚀</h3><p><em>Thank you for being a part of the </em><a href="https://plainenglish.io/"><strong><em>In Plain English</em></strong></a><em> community! Before you go:</em></p><ul><li>Be sure to <strong>clap</strong> and <strong>follow</strong> the writer ️👏<strong>️️</strong></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><strong>X</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/inplainenglish/"><strong>LinkedIn</strong></a><strong> | </strong><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>YouTube</strong></a><strong> | </strong><a href="https://discord.gg/in-plain-english-709094664682340443"><strong>Discord</strong></a><strong> | </strong><a href="https://newsletter.plainenglish.io/"><strong>Newsletter</strong></a></li><li>Visit our other platforms: <a href="https://stackademic.com/"><strong>Stackademic</strong></a><strong> | </strong><a href="https://cofeed.app/"><strong>CoFeed</strong></a><strong> | </strong><a href="https://venturemagazine.net/"><strong>Venture</strong></a><strong> | </strong><a href="https://blog.cubed.run/"><strong>Cubed</strong></a></li><li>More content at <a href="https://plainenglish.io/"><strong>PlainEnglish.io</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1be64094961a" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/aws-journey-standalone-server-to-scalable-golang-app-with-ci-cd-1be64094961a">AWS Journey: Standalone Server to Scalable Golang App with CI/CD</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Endpoint Health Monitoring with AWS Services and Terraform]]></title>
            <link>https://medium.com/cloud-native-daily/endpoint-health-monitoring-with-aws-services-and-terraform-e9061ed02765?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9061ed02765</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Sun, 23 Jul 2023 21:58:24 GMT</pubDate>
            <atom:updated>2023-07-31T14:03:42.484Z</atom:updated>
            <content:encoded><![CDATA[<h4>Endpoint monitoring by integrating AWS Route53 health checks with AWS CloudWatch, AWS SNS, and AWS Lambda — with Slack and Microsoft Teams for notification.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*oAyRBsSIGvV_CtcEiaODRw.png" /><figcaption>Route 53 health-check architecture</figcaption></figure><p>In an era where businesses rely on the digital world more than ever, it’s imperative to have robust and reliable systems monitoring our applications’ health. An unexpected outage or system failure can not only affect the user experience but can also lead to revenue loss and damage the company’s reputation. Hence, automated, efficient, and robust health checks and notifications are paramount.</p><p>Amazon’s Route53 Health Checks offer an excellent solution to this problem. By sending regular requests to your application endpoints, Route53 is designed to monitor the health of your applications continuously. If the application doesn’t respond within a specified period or the response doesn’t meet certain conditions, Route53 considers the endpoint unhealthy. But identifying the problem is only half the battle; the other half lies in efficient reporting and notification. This is where integrating CloudWatch alerts, SNS, Lambda, and communication tools like Slack and Microsoft Teams become incredibly valuable.</p><p>By setting up this kind of architecture, we ensure that we are notified the moment our systems detect any issues. We won’t have to wait for a user to report an issue or for a developer to stumble upon it during their work. This kind of proactive monitoring can save a lot of time and resources.</p><p>Furthermore, by automating the whole process using a modular Terraform project, we bring in immense scalability and consistency to our setup. As the infrastructure grows, the ability to manage resources as code becomes a significant advantage. It allows us to quickly deploy similar resources, maintain consistency across environments, and more efficiently manage and track infrastructure changes. We will add an extra layer of automation by implementing Terraform pipelines using GitLab CI. This will enable us to plan and deploy changes to our infrastructure automatically, making the maintenance and extension of our systems even more efficient.</p><p>In this article, we’ll explore how we can turbocharge our endpoint monitoring by integrating AWS Route53 health checks with AWS CloudWatch, AWS SNS, and AWS Lambda as our backbone and use Slack, Microsoft Teams, and email for notifications — with automated deployments and configuration changes via Terraform + GitLab CI.</p><p>To fully appreciate the level of automation we’re aiming for, it’s essential to first walk through the manual steps required in the process. This will provide the context needed to understand the tasks we’re targeting to automate.</p><p><strong>Setting up Route53 Health Checks</strong>: The first step is to create health checks for the application endpoints using Route53. These health checks monitor the health of the endpoints by sending regular requests and analyzing the responses based on pre-defined conditions.</p><p>AWS Developer Guide: <a href="https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/health-checks-creating.html">Creating and updating health checks</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*qKdZF_M5-ZMUUdTe8M3x1g.png" /></figure><p><strong>Creating CloudWatch Alarms</strong>: For each health check, a corresponding CloudWatch alarm is created. These alarms monitor the health check status metric and trigger when the endpoint’s status changes (i.e. when it becomes unhealthy).</p><p>AWS Developer Guide: <a href="https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/monitoring-health-checks.html">Monitoring health checks using CloudWatch</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hxS5-g6jeXxPeXwe1Tk_ng.png" /></figure><p><strong>Setting up SNS Topic</strong>: An Amazon Simple Notification Service (SNS) topic is created for the alarms. When an alarm changes state, it publishes a message to this topic. The topic has subscribers who will receive these messages, like our AWS Lambda function and any email addresses for direct email notifications.</p><p>AWS Developer Guide: <a href="https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html">Creating an Amazon SNS topic</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bvau2ASY83DdDk19L6NVFg.png" /></figure><p><strong>Building AWS Lambda Function</strong>: The Lambda function, which subscribes to the SNS topic, is built to handle the incoming alarm messages. This function parses the alarm data and prepares customized messages for Slack and Microsoft Teams.</p><p>AWS Developer Guide: <a href="https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html">Getting Started with Lambda</a></p><p><em>lambda code [runtime Python 3.7]</em></p><pre>import json<br>import os<br>import requests<br><br>def send_slack_message(message):<br>    slack_webhook_url = os.getenv(&#39;SLACK_WEBHOOK_URL&#39;)<br>    headers = {&#39;Content-type&#39;: &#39;application/json&#39;}<br>    response = requests.post(slack_webhook_url, headers=headers, data=json.dumps({&#39;text&#39;: message}))<br><br>    if response.status_code != 200:<br>        raise ValueError(f&#39;Request to Slack returned an error {response.status_code}, the response is:\n{response.text}&#39;)<br><br>def send_teams_message(message, color):<br>    teams_webhook_url = os.getenv(&#39;TEAMS_WEBHOOK_URL&#39;)<br>    headers = {&#39;Content-type&#39;: &#39;application/json&#39;}<br>    message = {<br>        &quot;@type&quot;: &quot;MessageCard&quot;,<br>        &quot;@context&quot;: &quot;http://schema.org/extensions&quot;,<br>        &quot;themeColor&quot;: color,<br>        &quot;summary&quot;: &quot;Health Check Status&quot;,<br>        &quot;sections&quot;: [{<br>            &quot;activityTitle&quot;: &quot;Health Check Status&quot;,<br>            &quot;text&quot;: message<br>        }],<br>        &quot;potentialAction&quot;: [{<br>            &quot;@type&quot;: &quot;OpenUri&quot;,<br>            &quot;name&quot;: &quot;Details&quot;,<br>            &quot;targets&quot;: [{&quot;os&quot;: &quot;default&quot;, &quot;uri&quot;: &quot;https://console.aws.amazon.com/route53/healthchecks/home#&quot;}]<br>        }]<br>    }<br>    response = requests.post(teams_webhook_url, headers=headers, data=json.dumps(message))<br><br>    if response.status_code != 200:<br>        raise ValueError(f&#39;Request to Teams returned an error {response.status_code}, the response is:\n{response.text}&#39;)<br><br><br>def handler(event, context):<br>    alarm_message = json.loads(event[&#39;Records&#39;][0][&#39;Sns&#39;][&#39;Message&#39;])<br>    endpoint = alarm_message[&#39;AlarmDescription&#39;]  # Assumes the endpoint URL is in the AlarmDescription<br><br>    if alarm_message[&#39;NewStateValue&#39;] == &#39;ALARM&#39;:<br>        formatted_message_slack = f&quot;*Endpoint:* {endpoint}\n&quot; \<br>                            f&quot;*State:* :elmofire: endpoint health check failed :warning:\n&quot; \<br>                            f&quot;&lt;https://console.aws.amazon.com/route53/healthchecks/home#|Details&gt;&quot;<br>        formatted_message_teams = f&quot;{endpoint} health check failed!&quot;<br>        teams_color = &quot;FF0000&quot;  # Red<br>    elif alarm_message[&#39;NewStateValue&#39;] == &#39;OK&#39;:<br>        formatted_message_slack = f&quot;*Endpoint:* {endpoint}\n&quot; \<br>                            f&quot;*State:* :baby-yoda-soup: endpoint recovered :white_check_mark:\n&quot; \<br>                            f&quot;&lt;https://console.aws.amazon.com/route53/healthchecks/home#|Details&gt;&quot;<br>        formatted_message_teams = f&quot;{endpoint} health check is ok!&quot;<br>        teams_color = &quot;00FF00&quot;  # Green<br><br>    slack_webhook_url = os.getenv(&#39;SLACK_WEBHOOK_URL&#39;)<br>    teams_webhook_url = os.getenv(&#39;TEAMS_WEBHOOK_URL&#39;)<br><br>    if slack_webhook_url and slack_webhook_url.strip():<br>        send_slack_message(formatted_message_slack)<br>    if teams_webhook_url and teams_webhook_url.strip():<br>        send_teams_message(formatted_message_teams, teams_color)</pre><p>Our Lambda function acts as a message interceptor and formatter for health checks. Here’s a brief description of its operation:</p><ul><li><strong>Environment Variables</strong>: Our function utilizes two environment variables: ‘SLACK_WEBHOOK_URL’ and ‘TEAMS_WEBHOOK_URL’. These variables hold the webhook URLs of our Slack and Teams channels, respectively.</li><li><strong>Event Handler</strong>: The main entry point of our Lambda function is the handler method. This method receives an event object which contains the message from Amazon SNS, informing about the health status of our endpoints.</li><li><strong>Message Processing</strong>: Our handler method processes the incoming SNS message, parsing it as JSON and extracting relevant information such as the endpoint (stored in AlarmDescription) and the new state of the alarm (NewStateValue).</li><li><strong>Notification Formatting</strong>: Depending on the state of the alarm, it constructs custom messages for both Slack and Teams. For Slack, it uses markdown syntax and includes relevant emojis. For Teams, it creates a simpler message and determines the color of the Teams card (red for ‘ALARM’ state, green for ‘OK’ state).</li><li><strong>Message Sending</strong>: If the respective webhook URL environment variable is set and not empty, it dispatches the corresponding formatted message to Slack or Teams using the send_slack_message or send_teams_message methods respectively.</li><li><strong>Sending Slack Message</strong>: The send_slack_message method sends a HTTP POST request to the Slack webhook URL with the constructed message as payload. If the request fails for any reason, it raises an exception with a detailed error message.</li><li><strong>Sending Teams Message</strong>: The send_teams_message method also sends a HTTP POST request, but to the Teams webhook URL, with a different payload structure suited for Teams.</li></ul><p>Through this Lambda function, we’re able to transform a raw health check alert from AWS into a rich, custom, human-readable notification in our communication channels.</p><p><strong>Configuring Slack and Microsoft Teams Webhooks</strong>: Webhooks in Slack and Microsoft Teams are set up to receive incoming messages from our AWS Lambda function. Each platform’s webhook URL is saved as an environment variable for the Lambda function to use.</p><p>Slack documentation: <a href="https://api.slack.com/messaging/webhooks">Sending messages using Incoming Webhooks</a><br>MS Teams documentation: <a href="https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook">Create Incoming Webhooks</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*pjETcvjQ5naPiarul5euhg.png" /><figcaption>slack channel view</figcaption></figure><p>Here is the slack channel view after few notifications 😅</p><h3>IaC automation with Terraform and GitLab CI</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*eg_R3acuLa9rZy8i.png" /></figure><p><strong>Terraform Setup</strong>: Now that the individual components are ready, they need to be managed efficiently. Terraform scripts will automate the creation and management of these resources.</p><p><em>Root Module</em></p><pre>module &quot;health-checks&quot; {<br>  source              = &quot;./modules/healthcheck&quot;<br>  endpoints           = {<br>    endpoint-1 = {<br>      fqdn = &quot;huseynov.net&quot;<br>      port          = 443<br>      path          = &quot;healthcheck&quot;<br>      search_string = &quot;status:ok&quot;<br>    },<br>    endpoint-2 = {<br>      fqdn = &quot;google.az&quot;<br>    }<br>  }<br>  notification_emails = [&quot;tarlan@huseynov.net&quot;]<br>  slack_webhook_url   = &quot;https://&lt;Webhook_url&gt;&quot;<br>  teams_webhook_url   = &quot;https://&lt;Webhook_url&gt;&quot;<br>}</pre><p>This root module brings together all the elements needed to set up and manage health checks for specified endpoints, send alerts and enable notifications.</p><p>You provide inputs to the module through variables:</p><ul><li>endpoints: This variable holds the list of endpoints that need to be monitored. Each endpoint is specified as a map with `fqdn` as the key for endpoint URL. If an endpoint includes the `search_string` key, the module creates a special kind of health check which not only checks the endpoint availability but also whether the response body contains the specified string.</li><li>notification_emails: A list of emails that should receive notifications about the health status of endpoints.</li><li>slack_webhook_url: The URL for the Slack channel where alerts will be posted.</li><li>teams_webhook_url: The URL for the Microsoft Teams channel where alerts will be posted.</li></ul><p>Once configured with appropriate inputs, this module takes care of creating Route53 health checks for the listed endpoints, setting up CloudWatch alarms to monitor these health checks, configuring SNS topics for notifications, and connecting the alerts to your Slack and Microsoft Teams channels as well as your email addresses. All of this complexity is hidden behind a simple and elegant interface, making your health checks management a breeze.</p><p><em>HealthCheck Module</em></p><pre>locals {<br>  endpoints_with_str_check   = { for name, parameters in var.endpoints : name =&gt; parameters if contains(keys(parameters), &quot;search_string&quot;) }<br>  endpoints_with_https_check = { for name, parameters in var.endpoints : name =&gt; parameters if !contains(keys(parameters), &quot;search_string&quot;) }<br>  route53_health_checks      = merge(aws_route53_health_check.https_check, aws_route53_health_check.https_str_check)<br>  lambda_alerts_enabled      = var.slack_webhook_url != &quot;&quot; || var.teams_webhook_url != &quot;&quot;<br>}<br><br># Route 53 health-checks<br>resource &quot;aws_route53_health_check&quot; &quot;https_check&quot; {<br>  fqdn              = each.value.fqdn<br>  reference_name    = each.key<br>  port              = each.value.port<br>  type              = &quot;HTTPS&quot;<br>  resource_path     = each.value.path<br>  failure_threshold = &quot;3&quot;<br>  request_interval  = &quot;30&quot;<br><br>  tags = {<br>    Name = &quot;${each.key}-hc&quot;<br>  }<br>}<br><br>resource &quot;aws_route53_health_check&quot; &quot;https_str_check&quot; {<br>  for_each          = local.endpoints_with_str_check<br>  fqdn              = each.value.fqdn<br>  reference_name    = each.key<br>  port              = each.value.port<br>  type              = &quot;HTTPS_STR_MATCH&quot;<br>  search_string     = each.value.search_string<br>  resource_path     = each.value.path<br>  failure_threshold = &quot;3&quot;<br>  request_interval  = &quot;30&quot;<br><br>  tags = {<br>    Name = &quot;${each.key}-hc&quot;<br>  }<br>}<br><br># Cloudwatch alarm<br>resource &quot;aws_cloudwatch_metric_alarm&quot; &quot;endpoint_hc_alarm&quot; {<br>  for_each            = local.route53_health_checks<br>  alarm_name          = &quot;${each.value.reference_name}-hc-alarm&quot;<br>  comparison_operator = &quot;LessThanThreshold&quot;<br>  evaluation_periods  = &quot;1&quot;<br>  metric_name         = &quot;HealthCheckStatus&quot;<br>  namespace           = &quot;AWS/Route53&quot;<br>  period              = &quot;60&quot;<br>  statistic           = &quot;Minimum&quot;<br>  threshold           = &quot;1&quot;<br>  alarm_actions       = [aws_sns_topic.alarm_sns_topic.arn]<br>  ok_actions          = [aws_sns_topic.alarm_sns_topic.arn]<br>  alarm_description   = each.value.fqdn # This is passed to lambda as endpoint value<br>  dimensions = {<br>    HealthCheckId = each.value.id<br>  }<br>}<br><br>data &quot;archive_file&quot; &quot;notifications_lambda_zip&quot; {<br>  type        = &quot;zip&quot;<br>  source_file = &quot;${path.root}/files/lambdas/healthcheck.py&quot;<br>  output_path = &quot;/tmp/healthcheck.zip&quot;<br>}<br><br>data &quot;aws_iam_policy_document&quot; &quot;lambda_assume_role_policy&quot; {<br>  statement {<br>    actions = [&quot;sts:AssumeRole&quot;]<br>    principals {<br>      type        = &quot;Service&quot;<br>      identifiers = [&quot;lambda.amazonaws.com&quot;]<br>    }<br>  }<br>}<br><br>resource &quot;aws_iam_role&quot; &quot;notifications_lambda_role&quot; {<br>  count              = local.lambda_alerts_enabled ? 1 : 0<br>  name               = &quot;health-check-notifications-lambda-role&quot;<br>  assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json<br>}<br><br>resource &quot;aws_lambda_function&quot; &quot;notifications_lambda&quot; {<br>  count            = local.lambda_alerts_enabled ? 1 : 0<br>  filename         = data.archive_file.notifications_lambda_zip.output_path<br>  source_code_hash = data.archive_file.notifications_lambda_zip.output_base64sha256<br>  function_name    = &quot;health-check-notifications&quot;<br>  # handler - &lt;python filename&gt;.&lt;handler function name&gt;<br>  handler = &quot;healthcheck.handler&quot;<br>  runtime = &quot;python3.7&quot;<br>  role    = aws_iam_role.notifications_lambda_role[0].arn<br><br>  environment {<br>    variables = {<br>      TEAMS_WEBHOOK_URL = var.teams_webhook_url<br>      SLACK_WEBHOOK_URL = var.slack_webhook_url<br>    }<br>  }<br><br>  lifecycle {<br>    ignore_changes = [source_code_hash, last_modified]<br>  }<br>}<br><br>resource &quot;aws_lambda_permission&quot; &quot;lambda_permission&quot; {<br>  count         = local.lambda_alerts_enabled ? 1 : 0<br>  statement_id  = &quot;AllowExecutionFromSNS&quot;<br>  action        = &quot;lambda:InvokeFunction&quot;<br>  function_name = aws_lambda_function.notifications_lambda[0].function_name<br>  principal     = &quot;sns.amazonaws.com&quot;<br>  source_arn    = aws_sns_topic.alarm_sns_topic.arn<br>}<br><br>data &quot;aws_iam_policy_document&quot; &quot;lambda_policy&quot; {<br>  count = local.lambda_alerts_enabled ? 1 : 0<br>  statement {<br>    actions = [<br>      &quot;logs:CreateLogGroup&quot;,<br>      &quot;logs:CreateLogStream&quot;,<br>      &quot;logs:PutLogEvents&quot;<br>    ]<br>    effect    = &quot;Allow&quot;<br>    resources = [&quot;*&quot;]<br>  }<br>}<br><br>resource &quot;aws_iam_policy&quot; &quot;lambda_policy&quot; {<br>  count  = local.lambda_alerts_enabled ? 1 : 0<br>  name   = &quot;health-check-notifications-lambda&quot;<br>  policy = data.aws_iam_policy_document.lambda_policy[0].json<br>}<br><br>resource &quot;aws_iam_role_policy_attachment&quot; &quot;lambda_role_attached_policy&quot; {<br>  count      = local.lambda_alerts_enabled ? 1 : 0<br>  role       = aws_iam_role.notifications_lambda_role[0].name<br>  policy_arn = aws_iam_policy.lambda_policy[0].arn<br>}<br><br><br># SNS Topic for healthcheck alerts<br>resource &quot;aws_sns_topic&quot; &quot;alarm_sns_topic&quot; {<br>  name                             = &quot;endpoint-health-check-alarm-topic&quot;<br>  lambda_failure_feedback_role_arn = aws_iam_role.delivery_feedback_role.arn<br>  lambda_success_feedback_role_arn = aws_iam_role.delivery_feedback_role.arn<br>}<br><br>resource &quot;aws_sns_topic_subscription&quot; &quot;lambda_topic_subscription&quot; {<br>  count     = local.lambda_alerts_enabled ? 1 : 0<br>  topic_arn = aws_sns_topic.alarm_sns_topic.arn<br>  protocol  = &quot;lambda&quot;<br>  endpoint  = aws_lambda_function.notifications_lambda[0].arn<br>}<br><br>resource &quot;aws_sns_topic_subscription&quot; &quot;email_topic_subscription&quot; {<br>  for_each  = toset(var.notification_emails)<br>  topic_arn = aws_sns_topic.alarm_sns_topic.arn<br>  protocol  = &quot;email&quot;<br>  endpoint  = each.value<br>}<br><br>resource &quot;aws_sns_topic_subscription&quot; &quot;sms_topic_subscription&quot; {<br>  for_each  = toset(var.notification_mobile)<br>  topic_arn = aws_sns_topic.alarm_sns_topic.arn<br>  protocol  = &quot;sms&quot;<br>  endpoint  = each.value<br>}<br><br># Feedback role<br>data &quot;aws_iam_policy_document&quot; &quot;feedback_assume_role_policy&quot; {<br>  statement {<br>    actions = [&quot;sts:AssumeRole&quot;]<br>    principals {<br>      type        = &quot;Service&quot;<br>      identifiers = [&quot;sns.amazonaws.com&quot;]<br>    }<br>  }<br>}<br><br>resource &quot;aws_iam_role&quot; &quot;delivery_feedback_role&quot; {<br>  name               = &quot;SNSFeedbackRole&quot;<br>  assume_role_policy = data.aws_iam_policy_document.feedback_assume_role_policy.json<br><br>  inline_policy {<br>    name = &quot;SNSFeedbackPolicy&quot;<br><br>    policy = jsonencode({<br>      &quot;Version&quot; : &quot;2012-10-17&quot;,<br>      &quot;Statement&quot; : [<br>        {<br>          &quot;Effect&quot; : &quot;Allow&quot;,<br>          &quot;Action&quot; : [<br>            &quot;logs:CreateLogGroup&quot;,<br>            &quot;logs:CreateLogStream&quot;,<br>            &quot;logs:PutLogEvents&quot;,<br>            &quot;logs:PutMetricFilter&quot;,<br>            &quot;logs:PutRetentionPolicy&quot;<br>          ],<br>          &quot;Resource&quot; : [<br>            &quot;*&quot;<br>          ]<br>        }<br>      ]<br>    })<br>  }<br>}</pre><p>The health-check module is a comprehensive health check solution. It splits the provided endpoints into two categories based on the presence of a search_string. It generates Route53 health checks, with HTTPS type for endpoints without search_string and HTTPS_STR_MATCH for those with search_string.</p><p>The module also creates CloudWatch alarms for each endpoint, monitoring health check status and triggering SNS topic notifications when a status change occurs. The SNS topic is linked to Lambda function for alerts on Teams or Slack, and also sends email notifications.</p><p>If either Teams or Slack webhook URLs are provided, a Lambda function is deployed. It has the necessary IAM role and permissions for execution and logging. The module also handles SNS topic subscriptions for both email and Lambda function.</p><p>Finally, the module provides an IAM role for SNS to handle delivery feedback.</p><p>The entire module is neatly encapsulated and parameterized, ready to be utilized in any Terraform project.</p><p><strong>Integrating with GitLab CI</strong>: The final step is to automate the Terraform workflows using GitLab CI. GitLab pipelines are created for planning and deploying changes to the infrastructure. With each git push, a pipeline is triggered that runs Terraform commands to apply the changes to the infrastructure.</p><p>Requirement: <a href="https://docs.gitlab.com/ee/user/infrastructure/iac/terraform_state.html">Gitlab managed terraform state</a></p><p><em>gitlab-ci.yml</em></p><pre>#################################################################################################################<br>variables:<br>  TF_ROOT: ${CI_PROJECT_DIR}<br>  TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/tfstate<br>  ################ AWS Credentials ####################<br>  TF_VAR_region: $AWS_REGION_DEV<br>  TF_VAR_access_key: $AWS_ACCESS_KEY_ID<br>  TF_VAR_secret_key: $AWS_SECRET_ACCESS_KEY<br>##################################################################################################################<br>before_script:<br>  - cd ${TF_ROOT}<br>  - echo &quot;SYSTEM INFO:&quot;<br>  - cat /etc/os-release<br>  - cat /etc/hostname<br>##################################################################################################################<br>stages:<br>  - init<br>  - validate<br>  - plan<br>  - apply<br>##################################################################################################################<br><br>##################################################################################################################<br>default:<br><br>  image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest<br><br>  cache:<br>    key: ${CI_PROJECT_ID}-terraform-cache<br><br>    paths:<br>      - ${TF_ROOT}/.terraform<br>##################################################################################################################<br><br>##################################################################################################################<br>INITIALIZE:<br><br>  stage: init<br><br>  script:<br>    - gitlab-terraform init<br>  <br>  only:<br>    - merge_requests<br>    - master<br>##################################################################################################################<br><br>##################################################################################################################<br>VALIDATE:<br><br>  stage: validate<br><br>  script:<br>    - gitlab-terraform validate<br>  <br>  only:<br>    - merge_requests<br>    - master  <br>##################################################################################################################<br><br>##################################################################################################################<br>PLAN:<br><br>  stage: plan<br><br>  script:<br>    - gitlab-terraform plan<br>    - gitlab-terraform plan-json<br><br>  artifacts:<br>    name: plan<br>    paths:<br>      - ${TF_ROOT}/plan.cache<br>    reports:<br>      terraform: ${TF_ROOT}/plan.json<br>      <br>  only:<br>    - merge_requests<br>    - master      <br>##################################################################################################################<br><br>##################################################################################################################<br>APPLY:<br><br>  stage: apply<br><br>  environment:<br>    name: production<br><br>  script:<br>    - gitlab-terraform apply<br><br>  dependencies:<br>    - PLAN<br><br>  when: manual<br><br>  only:<br>    - master<br>##################################################################################################################</pre><p>This GitLab CI/CD pipeline is configured to utilize a branch-based workflow with merge requests. It consists of four stages: `init`, `validate`, `plan`, and `apply`, that get triggered based on the branch and the action performed.</p><p>Here is the breakdown:</p><p>1. `INITIALIZE` stage: This is the first stage that runs `gitlab-terraform init`. It initializes the Terraform working directory, downloading necessary providers and modules, and sets up the backend. It’s executed for both merge requests and changes to the `master` branch.</p><p>2. `VALIDATE` stage: This stage runs `gitlab-terraform validate` which validates the Terraform configuration files for syntax and internal consistency. This also runs for both merge requests and changes to the `master` branch.</p><p>3. `PLAN` stage: This stage generates an execution plan with `gitlab-terraform plan` and outputs it as JSON with `gitlab-terraform plan-json`. The plan is saved as an artifact for inspection. It is triggered for merge requests and changes to the `master` branch.</p><p>4. `APPLY` stage: This stage is triggered manually and only on the `master` branch. It runs `gitlab-terraform apply` to apply the proposed changes from the execution plan. It depends on the `PLAN` stage.</p><p>Note that this pipeline configuration relies on GitLab’s managed Terraform state. The state is stored on GitLab’s infrastructure and is managed by GitLab, providing a secure and reliable backend for Terraform. This is specified by the `TF_ADDRESS` variable in the configuration, which points to GitLab’s API endpoint for managing Terraform state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*wkOrFHDtNUgB00d9" /></figure><p>In this comprehensive article, we delved into the depths of infrastructure as code (IaC) through Terraform, focusing on deploying and managing health checks for a system architecture, aided by GitLab’s CI/CD pipeline. By utilizing AWS services like Route53, CloudWatch, SNS, and Lambda, we constructed a robust system capable of monitoring server status, carrying out HTTP string-matching health checks, and notifying stakeholders via multiple channels when incidents occur.</p><p>Through this exploration, we hope you’ve gained valuable insights into how you can leverage these tools to enhance your systems’ reliability and maintainability. As always, keep exploring, learning, and innovating!</p><h4>Further Reading:</h4><ul><li><a href="https://gethelios.dev/blog/serverless-observability/?utm_source=medium&amp;utm_medium=cloud+native+daily">Serverless observability, monitoring, and debugging explained</a></li><li><a href="https://gethelios.dev/blog/api-monitoring-vs-observability-in-microservices-troubleshooting-guide/?utm_source=medium&amp;utm_medium=cloud+native+daily">API monitoring vs. observability in microservices</a></li><li><a href="https://gethelios.dev/blog/kubernetes-monitoring-opentelemetry/?utm_source=medium&amp;utm_medium=cloud+native+daily">Kubernetes Monitoring with OpenTelemetry</a></li><li><a href="https://aws.plainenglish.io/top-tools-for-aws-lambda-monitoring-8ea51b1f095c">Top Tools for AWS Lambda Monitoring</a></li><li><a href="https://gethelios.dev/blog/lambda-monitoring-combining-the-three-pillars-of-observability-to-reduce-mttr/?utm_source=medium&amp;utm_medium=cloud+native+daily">Lambda monitoring: Combining the three pillars of observability to reduce MTTR</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9061ed02765" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-native-daily/endpoint-health-monitoring-with-aws-services-and-terraform-e9061ed02765">Endpoint Health Monitoring with AWS Services and Terraform</a> was originally published in <a href="https://medium.com/cloud-native-daily">Cloud Native Daily</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Konteynerləşdirəbildiklərimizdənsinizmi?  Docker Əsasları]]></title>
            <link>https://medium.com/@tarlan_59778/konteynerl%C9%99%C5%9Fdir%C9%99bildikl%C9%99rimizd%C9%99nsinizmi-docker-%C9%99saslar%C4%B1-7e7dbfed97d2?source=rss-7d0345c6fa0d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e7dbfed97d2</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <dc:creator><![CDATA[Tarlan Huseynov]]></dc:creator>
            <pubDate>Wed, 19 Jul 2023 17:19:57 GMT</pubDate>
            <atom:updated>2023-07-19T19:23:32.733Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rweqj-8WKx8K92Y8.jpg" /></figure><p>Daima “Deployment” prosesinizi sürətləndirmək, sistem effektivliyini artırmaq və ya mövcüd mühitə “scalability” gətirmək istəyən bir mütəxəssis kimi siz artıq “Docker” haqqında eşitmiş olmalısınız. Əgər ki bu belə deyilsə sizə bu mövzuda lazım ola biləcək və docker-in istifadəsinə motivə edəcək ilkin informasiyanı bu kiçik məqalədə qeyd etməyə çalışacam.</p><p>Docker — “deployment”-ləri, miqyaslılığı (scalability), və proqram təminatının/tətbiqlərin (application) idarə olunmasını (manage — yavaş-yavaş keçirik ingiliscəyə 😅) portativ, yüngül (lightweight), izolyasiya olunmuş — “Konteyner”-lərin vasitəsi ilə avtomatlaşdırmağa imkan verən “opensource” platformadır.</p><p><strong>Docker-i istifadə etməyə sizi hansı reallıqlar motivə etməlidir?..</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*S2xPEHwLck9ZlAD3VT4twg.png" /></figure><ol><li>Təkrar-təkrar yenidən hasil oluna bilən mühit — Kod onu yazan proqramçının lokal mühitində mükəmməl işləyir, lakin başqa mühitə, maşına transfer olunduqda aləm dəyir bir-birinə… Tanış ssenaridir? Docker, development-dən production-a qədər stabil (consistent) və tam şəkildə təkrarlanan mühitləri yaratmaqla bu problemi aradan qaldırır, problemlərin təkrarlanmasının şansını 0-a endirir, və ən əsası <strong>“Mənim kompyuterimdə işləyir” </strong>— sindromuna qarşı allergik reaksiya, bunu arqument gətirən həmkarlara qarşı tolerantlıq göstərmək üçün səbrin yetməməsi və s. (özümü nəzərdə tutmuram 🙄) kimi istənilməyən ssenarilər “gorbagor” olur <a href="https://emojipedia.org/party-popper/">🎉</a>.</li><li>Scalability — Docker ilə siz asanlıqla app-lərinizi üfüqi olaraq miqyaslandıra bilərsiniz (horizontal scaling). Tələbat əsasında işləyən konteynerlərin sayını sürətlə artıra və ya azalda bilərsiniz. Müxtəlif application komponentlərinin müstəqil olaraq scaling-ini tələb edən mikroservis arxitekturasında bu xüsusilə faydalıdır.</li><li>Müasir Devops tool-ları ilə asan 2x2 inteqrasiyalar — demək olar ki bütün müasir tool-lar konteynerləşdirilmiş mühiti native olaraq dəstəkləyir.</li><li>Effektiv resurs utilizasiyası — Ənənəvi virtuallaşdırmadan fərqli olaraq (1 VM = 1 Kernel) konteynerlər özünün ayrıca kernel-inə ehtiyac duymur, əvəzində bütün konteynerlər host sisteminin OS Kernel-ini paylaşır, bu da onları daha yüngül və sürətli edir + sistem resurslarının səmərəli istifadəsinə gətirib çıxarır.</li><li>İzolyasiya və təklükəsizlik — Hər bir Docker konteyneri digərlərindən asılı olmayaraq çalışır. Bu o deməkdir ki, müxtəlif konteynerlərdə işləyən proqramlar bir-birinə müdaxilə edə bilməyəcək və bu da əlavə təhlükəsizlik təbəqəsini təmin edəcək və bir tətbiqin boşluqlarının digərinə təsir etmə riskini aradan qaldıracaq.</li><li>Proqramlaşdırmanın produktivliyi — Docker app-i dependency-ləri (asılı olduğu paketlər, mühit komponentləri) ilə birlikdə standart vahidə paketləyə bilir. Bu, development mühitlərinin qurulmasını və paylaşılmasını (share olunmasını) asanlaşdırır, beləliklə məhsuldarlığı artırır. Komandanın hər hansı bir üzvü Docker image yaradıb paylaşa bilər və əməliyyat sistemindən və lokal mühitindən asılı olmayaraq tətbiqi bir neçə dəqiqə ərzində istənilən docker engine-in mövcud olduğu maşında işə sala bilər, və ya test (debug) edə bilər.</li></ol><p>Güman edək ki, bu nöqtədə tam əmin olduq — Docker olmadan mümkün deyil, müasir development-in tərkib hissəsidir və geriyə yol yoxdur <a href="https://emojipedia.org/stop-sign/">🛑</a> — növbəti addım nə olmalıdır? təbii ki Docker-in qurulması 🙃 və bunun üçün aşağıda qeyd olunan rəsmi URL-lərə keçid alaraq instruksiyaları izləmək kifayətdir: <a href="https://www.docker.com/products/docker-desktop"><strong>Windows və macOS üçün</strong></a><strong> və </strong><a href="https://docs.docker.com/engine/install/"><strong>Linux üçün</strong></a><strong>.</strong></p><p>Başlanğıc üçün zəruri olan sadə komandalar:</p><ul><li>docker run hello-world hello world image-ini yükləyəcək və standart output-u ekrana çıxaracaq.</li><li>docker ps: Start edilmiş konteynerləri göstərəcək. docker ps -a bütün konteynerləri ekranda göstərəcək</li><li>docker images: Localdakı bütün docker image-lərini siyahı şəklində göstərəcək</li><li>docker rm [CONTAINER_ID]: Dayandırılmış konteynerin silinməsini təmin edir</li><li>docker rmi [IMAGE_ID]: Docker image-ini silmək üçün istifadə olunur</li></ul><p>Təbii ki, docker komandaları və funskionallığını detallı izah edən ən yaxşı resurs rəsmi dokumentasiyadır: <a href="https://docs.docker.com/">https://docs.docker.com/</a></p><p><strong>Dockerfile və ilk Docker image-inin yaradılması</strong></p><p>Dockerfile — Docker image yaratmaq üçün istifadə olunan mətn faylıdır. Özündə image də əksini tapmalı olan mühitin qurulması üçün zəruri olan addımları və komandalar siyahısını cəmləyir. Sadə Node.js app-inin Dockerfile-ını nümunə olaraq göstərək:</p><pre>#Specify the base image<br> FROM node:14<br># Create app directory<br> WORKDIR /usr/src/app<br># Install app dependencies<br> COPY package*.json ./<br> RUN npm install<br># Bundle app source<br> COPY . .<br># Expose port and start app<br> EXPOSE 8080<br> CMD [ &quot;node&quot;, &quot;server.js&quot; ]</pre><p>Birbaşa bu faylı istifadə edərək bir komanda ilə docker image-ini yarada bilərsiniz. Bunun üçün Dockerfile-ın yerləşdıyı lokasiyada (kontekstdə) terminaldan docker build komandasını daxil etmək kifayətdir . Burada -t arqumenti image-inizi istədiyiniz ad ilə tag etməyə xidmət edir:</p><p>docker build -t my-node-app:1.0 .</p><p>Ən nəhayət son olaraq qeyd edək ki, əgər effektiv docker istifadəçisi olmaq istəyiriksə, müasir development-ə ayaq uydurmaq şərtdirsə, mütləq docker-i real layihədə tətbiq etməli və docker-in tətbiq olunmasını özümüz üçün adət halına çevirməliyik.</p><p>Bol şans və uğurlar! 😉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e7dbfed97d2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>