<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ajaydhungel23</title>
    <description>The latest articles on Forem by ajaydhungel23 (@ajaydhungel23).</description>
    <link>https://forem.com/ajaydhungel23</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1037754%2F8637fef1-a20c-463d-a019-aa5d6b60b520.png</url>
      <title>Forem: ajaydhungel23</title>
      <link>https://forem.com/ajaydhungel23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ajaydhungel23"/>
    <language>en</language>
    <item>
      <title>AWS Step Functions: Creating a ‘Busy Waiting’ flow to wait for successful lambda executions.</title>
      <dc:creator>ajaydhungel23</dc:creator>
      <pubDate>Thu, 01 Jun 2023 18:45:43 +0000</pubDate>
      <link>https://forem.com/aws-builders/aws-step-functions-creating-a-busy-waiting-flow-to-wait-for-successful-lambda-executions-576d</link>
      <guid>https://forem.com/aws-builders/aws-step-functions-creating-a-busy-waiting-flow-to-wait-for-successful-lambda-executions-576d</guid>
      <description>&lt;p&gt;Hi everyone, welcome to this article. In the past few days, I had to create a logic in AWS Step functions which involved waiting for a lambda that queries athena before going forward in the flow. &lt;/p&gt;

&lt;p&gt;When I was discussing the logic, it felt like an easy thing to accomplish in minutes but I was surprised upon knowing that Step Functions does not have its native technique/solve to wait for a state to succeed before starting another. This made me search for hours before coming up with a solution.&lt;/p&gt;

&lt;p&gt;Here, I will share with you a technique which will help you in the future.&lt;/p&gt;

&lt;p&gt;First of all, I will create a simple lambda function. All we need to know from the lambda is whether it did its job. So, this simple function will make sure to return its status.&lt;/p&gt;

&lt;p&gt;Lambda function:&lt;br&gt;
&lt;/p&gt;

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

def lambda_handler(event, context):
    response = {
        'status': 'success',
        'message': 'Hello, World!'
    }

    return response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can copy both the function and the Step function definition which I will share below to replicate all of this in your own environment. &lt;/p&gt;

&lt;p&gt;Now, lets go ahead and create a state machine in AWS Step Functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3vqducgkmq06ude209gj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3vqducgkmq06ude209gj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on ‘Create state machine’. &lt;/p&gt;

&lt;p&gt;Choose the option to write your own definition and choose ‘standard’.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mcoo8lc3omc8jbhnh7h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mcoo8lc3omc8jbhnh7h.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, in the definition section, copy and paste the following definition.&lt;/p&gt;

&lt;p&gt;Step Functions definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Comment": "A Hello World example of the Amazon States Language using Pass states",
  "StartAt": "Main",
  "States": {
    "Main": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "OutputPath": "$.Payload",
      "Parameters": {
        "Payload.$": "$",
        "FunctionName": "arn:aws:lambda:us-east-1:031342435657:function:stepfunctionlambda_test:$LATEST"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 6,
          "BackoffRate": 2
        }
      ],
      "Next": "WaitforInvocation"
    },
    "WaitforInvocation": {
      "Type": "Wait",
      "Seconds": 2,
      "Next": "CheckInvocationsStatus"
    },
    "CheckInvocationsStatus": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.status",
          "StringEquals": "success",
          "Next": "Final"
        }
      ],
      "Default": "WaitforInvocation"
    },
    "Final": {
      "Type": "Succeed"
    }
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the definition and click next.&lt;/p&gt;

&lt;p&gt;Specify the name of your state machine and then leave everything else as default. A new execution role will be created if you don’t specify any.&lt;/p&gt;

&lt;p&gt;Save it and then you will have created a state machine which looks like this:&lt;/p&gt;

&lt;p&gt;Visual flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh567w7r69dda3w38t36g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh567w7r69dda3w38t36g.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To explain the definition above, I have pointed out how this state machine works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The state machine starts at the "Main" state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The "Main" state is a Task state that invokes a Lambda function specified by the "FunctionName" parameter. The input to the Lambda function is the entire input passed to the state machine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The "OutputPath" parameter is  set to "$.Payload" to capture the output of the Lambda function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the Lambda function invocation fails with certain exceptions specified in the "Retry" section (Lambda.ServiceException, Lambda.AWSLambdaException, Lambda.SdkClientException,  Lambda.TooManyRequestsException), the state machine will retry the invocation up to 6 times with a backoff interval of 2 seconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After invoking the Lambda function, the state machine proceeds to the "WaitforInvocation" state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The "WaitforInvocation" state is a Wait state that pauses the execution for 2 seconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the wait period,  the state machine goes  to the "CheckInvocationsStatus" state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The "CheckInvocationsStatus" state is a Choice state that checks  the value of the "$.status" field in the output of the Lambda function. If the status is "success", the state machine proceeds to  the "Final" state. Otherwise, it goes back to the "WaitforInvocation" state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The "Final" state is a Succeed state that indicates the successful completion of the state machine.&lt;/p&gt;

&lt;p&gt;Now, lets go ahead and execute it!&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famyu3l4skfdr3os7owe5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famyu3l4skfdr3os7owe5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on ‘Start execution’ and  leave the input field empty.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hib8yqc4i674d0132r8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hib8yqc4i674d0132r8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Incase of a lambda that requires input, you can insert your input here as a Json&lt;/p&gt;

&lt;p&gt;After you start your execution, each state will turn into different colours which indicates the following.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Green: Indicates successful completion of a state or step.&lt;/li&gt;
&lt;li&gt;Red: Indicates a failure or error occurred during the execution of a state or step.&lt;/li&gt;
&lt;li&gt;Yellow: Indicates a state is in a waiting or paused state.&lt;/li&gt;
&lt;li&gt;Blue: Represents a decision or choice point where the state machine evaluates conditions to determine the next state.&lt;/li&gt;
&lt;li&gt;Gray: Represents a passive state that simply passes input to output without performing significant work.&lt;/li&gt;
&lt;li&gt;White: Represents transitions or connections between states, indicating the flow of execution from one state to another.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally this is how the execution will look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffp8g4hthee521cc883w4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffp8g4hthee521cc883w4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you. Hope you can now go ahead and apply the  “Busy Waiting” logic in your step functions.&lt;/p&gt;

</description>
      <category>stepfunctions</category>
      <category>lambda</category>
      <category>aws</category>
      <category>loop</category>
    </item>
    <item>
      <title>Leveraging AWS CodePipeline pipeline and Serverless Framework to deploy a Flask API.</title>
      <dc:creator>ajaydhungel23</dc:creator>
      <pubDate>Tue, 28 Mar 2023 03:30:52 +0000</pubDate>
      <link>https://forem.com/aws-builders/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api-3jha</link>
      <guid>https://forem.com/aws-builders/leveraging-aws-codepipeline-pipeline-and-serverless-framework-to-deploy-a-flask-api-3jha</guid>
      <description>&lt;p&gt;For the past couple of days. I have been working with the Serverless framework. I came to know that you can actually host your flask API that integrates several AWS services such as Lambda, API Gateway, and DynamoDB using this framework. I had to spend a lot of time to understand the process at first since there is not a lot of content regarding this implementation. So, here’s me contributing a little. &lt;/p&gt;

&lt;p&gt;To make this swift, I wanted to integrate the entire process with a pipeline that contains the code and a build environment. Here’s how everything is carried out. Please understand that this is not a beginner level tutorial and the whole process is written in assumption that the readers have knowledge and experience with AWS services and Python Flask.&lt;/p&gt;

&lt;p&gt;First, you need to have a flask api. An API that works. I couldn’t share the actual code that I first used since it is confidential . So, I have decided to go with a template of serverless framework. You can also convert your flask api to be compatible with serverless using the serverless-wsgi plugin. For this, you may need to make some changes to your serverless.yaml configurations. You can check it &lt;a href="https://www.serverless.com/blog/flask-python-rest-api-serverless-lambda-dynamodb/#converting-an-existing-flask-application" rel="noopener noreferrer"&gt;here&lt;/a&gt; to achieve that&lt;/p&gt;

&lt;p&gt;Now that you have your api , lets upload it again to an aws codecommit repository. You can clone an empty repo, add the files, commit and push. Check it &lt;a href="https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-file.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next, we need to create a build project in AWS CodeBuild.&lt;/p&gt;

&lt;p&gt;In this codebuild project, use the following buildspec.yaml file. You can choose an Ubuntu environment for the build project to run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 14
    commands:
        - npm install -g serverless
        - npm install serverless-compose
        - serverless --version
        - npm install serverless-deployment-bucket
        - sls plugin install -n serverless-wsgi
        - sls plugin install -n serverless-python-requirements

  build: 
    commands: 
      - serverless deploy 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember: In this case, Codebuild is only used to run the deploy command. All the resources are built by serverless by automatically running a cloudformation.&lt;/p&gt;

&lt;p&gt;Now, we’re also gonna need another yaml file. This one is the configuration file for the serverless framework which will be deployed once we deploy it using the Aws pipeline.&lt;/p&gt;

&lt;p&gt;Here’s the configuration code.&lt;br&gt;
&lt;/p&gt;

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

frameworkVersion: '3'

custom:
  wsgi:
    app: app.hello

provider:
  name: aws
  runtime: python3.9

functions:
  api:
    handler: wsgi_handler.handler
    events:
      - httpApi: '*'

plugins:
  - serverless-wsgi
  - serverless-python-requirements


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also keep your flask api file in the same directory. Make sure to configure codebuild to look for the specific branch in the specific repo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5n51ulma3twzipf83pt6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5n51ulma3twzipf83pt6.png" alt="File structure in the repository"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once everything is created, we need to make sure that our Codebuild service role has enough permission to use cloudformation and all the other services associated with the resources built through the cloudformation stack.&lt;/p&gt;

&lt;p&gt;In my case, I faced a lot of failed builds due to permission issues.Always make sure to check your build logs when your build fails, so that you can get a clear understanding of the underlying reasons and issues.&lt;/p&gt;

&lt;p&gt;So, to make it quick I added some permission to the codebuild service role. I suggest to create your own permission policies based on the least privilege access policy rather than assigning managed policies with Full access. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx3xyr8r64is33y00qjqd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx3xyr8r64is33y00qjqd.png" alt="Listed AIM Policies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the permissions are set, your pipeline will run swiftly. Here's how my pipeline looks like with the two phases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnce9vyaltow0y5ewyq2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnce9vyaltow0y5ewyq2o.png" alt="CodePipeline success"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the outputs from the cloudformation stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxjs2kp5mb19f4yc8v19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foxjs2kp5mb19f4yc8v19.png" alt="Cloudformation outputs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You invoke URL will be generated in Amazon API Gateway. Here in the build logs, you can see an invoke URL for the api. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgxl7i306ordfkeq3uzvb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgxl7i306ordfkeq3uzvb.png" alt="Codbuild logs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The serverless Framework will also help you monitor your resources and their performance through the console. All you need to do is login to the console and connect to your aws account. The console will run another cloudformation stack to integrate with the resources in our aws account. Here is how the dashboard looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvyw2ug7qovo6nabvfmut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvyw2ug7qovo6nabvfmut.png" alt="Serverless console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for making it to the end, hope You found what you were looking for!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>awscommunity</category>
      <category>serverless</category>
      <category>devtool</category>
    </item>
  </channel>
</rss>
