<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: raisel melian</title>
    <description>The latest articles on DEV Community by raisel melian (@raiselmelian).</description>
    <link>https://dev.to/raiselmelian</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%2F451715%2F0b61da5f-a35f-4cb1-8d4d-f828fd272eff.jpg</url>
      <title>DEV Community: raisel melian</title>
      <link>https://dev.to/raiselmelian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raiselmelian"/>
    <language>en</language>
    <item>
      <title>Trigger step functions from lambda after uploading files to S3 using Serverless Framework and Python</title>
      <dc:creator>raisel melian</dc:creator>
      <pubDate>Fri, 14 Aug 2020 23:07:48 +0000</pubDate>
      <link>https://dev.to/raiselmelian/trigger-step-functions-from-lambda-after-uploading-files-to-s3-using-serverless-framework-and-python-5051</link>
      <guid>https://dev.to/raiselmelian/trigger-step-functions-from-lambda-after-uploading-files-to-s3-using-serverless-framework-and-python-5051</guid>
      <description>&lt;p&gt;In this post, I'm gonna show you how to execute a step function from a lambda function after a file is uploaded to an S3 bucket.&lt;/p&gt;

&lt;p&gt;You can watch the video version here:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/KcoLDAhLpbg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;In the image, we can see we have three AWS services, S3, Lambda Functions, and Step Functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tCDTh2DO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dvj2xtaews4e91a6qb7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tCDTh2DO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dvj2xtaews4e91a6qb7x.png" alt="Trigger step function"&gt;&lt;/a&gt;&lt;br&gt;
After a file is uploaded to an S3 bucket, AWS will send an event to a lambda function passing the bucket and file name as part of the event.&lt;/p&gt;
&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;p&gt;Let's start by creating a serverless application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;serverless create --template aws-python3 --path my-state-machine
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's go inside the created folder &lt;strong&gt;my-state-machine&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-state-machine
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And open it up in your favorite IDE. I'm using &lt;a href="https://code.visualstudio.com/"&gt;vscode&lt;/a&gt;&lt;br&gt;
With vscode you can open your project directly from the terminal with the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;code .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's open the serverless.yml file and remove all comments for clarity.&lt;br&gt;
After removing all comments we'll have a file similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-state-machine&lt;/span&gt;

&lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3.8&lt;/span&gt;

&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;hello&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;handler.hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our next step is to create and configure our Lambda function. &lt;br&gt;
Let's replace the &lt;strong&gt;functions&lt;/strong&gt; section with the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;functions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;StateMachineTrigger&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;handler.upload&lt;/span&gt;
    &lt;span class="na"&gt;events&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;s3&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;bucket&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:service}-${self:provider.stage}-upload-bucket&lt;/span&gt;
          &lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;s3:ObjectCreated:*&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MY_STATE_MACHINE_ARN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${self:resources.Outputs.MyStateMachine.Value}&lt;/span&gt;          
    &lt;span class="na"&gt;iamRoleStatements&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Effect&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Allow"&lt;/span&gt;        
        &lt;span class="na"&gt;Action&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;states:*&lt;/span&gt;
        &lt;span class="na"&gt;Resource&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*"&lt;/span&gt;

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



&lt;p&gt;The &lt;strong&gt;events&lt;/strong&gt; section is declaring that every time a file is uploaded (&lt;strong&gt;ObjectCreated&lt;/strong&gt;) into our bucket &lt;strong&gt;${self:service}-${self:provider.stage}-upload-bucket&lt;/strong&gt; AWS will send a message to our lambda function &lt;strong&gt;lib/trigger.handler&lt;/strong&gt; with the bucket and file information.&lt;br&gt;
You can see we are declaring an environment variable &lt;strong&gt;MY_STATE_MACHINE_ARN&lt;/strong&gt;. This is for our lambda function to know what is the step function's ARN.&lt;/p&gt;

&lt;p&gt;Now we need to create the lambda handler we just declared so lets&lt;br&gt;
replace the file handler.py with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;boto3&lt;/span&gt;

&lt;span class="n"&gt;my_state_machine_arn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'MY_STATE_MACHINE_ARN'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'stepfunctions'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;upload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Records'&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_execution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;stateMachineArn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;my_state_machine_arn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'s3'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are using &lt;a href="https://github.com/boto/boto3"&gt;AWS boto3&lt;/a&gt; to trigger our step function that we are going to declare in just a second, and so we are creating a &lt;strong&gt;client&lt;/strong&gt; for that. We are also declaring a variable &lt;strong&gt;my_state_machine_arn&lt;/strong&gt; to access the environment variable we declared in our serverless.yml. And finally, we are using the &lt;strong&gt;start_execution&lt;/strong&gt; method to execute our lambda function passing the event's s3 (&lt;strong&gt;event_record['s3']&lt;/strong&gt;) object as argument.&lt;/p&gt;

&lt;p&gt;Our final step is to declare the step function. &lt;br&gt;
To do that we are going to use a plugin named &lt;a href="https://github.com/serverless-operations/serverless-step-functions"&gt;serverless step functions&lt;/a&gt; so let's add the following to the end of our serverless.yml:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;stepFunctions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stateMachines&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;MyStateMachine&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;MyStateMachine&lt;/span&gt;
      &lt;span class="na"&gt;definition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;StartAt&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ValidateImage&lt;/span&gt;
        &lt;span class="na"&gt;States&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;ValidateImage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Pass&lt;/span&gt;
            &lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Valid"&lt;/span&gt;
            &lt;span class="na"&gt;Next&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;GenerateThumbnail&lt;/span&gt;
          &lt;span class="na"&gt;GenerateThumbnail&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Pass&lt;/span&gt;
            &lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Thumbnail&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;created"&lt;/span&gt;
            &lt;span class="na"&gt;Next&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;NotifyUser&lt;/span&gt;
          &lt;span class="na"&gt;NotifyUser&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;Type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Pass&lt;/span&gt;
            &lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Notification&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;sent"&lt;/span&gt;
            &lt;span class="na"&gt;End&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;

&lt;span class="na"&gt;resources&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;Outputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;MyStateMachine&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;The ARN of the example state machine&lt;/span&gt;
      &lt;span class="na"&gt;Value&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;Ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;MyStateMachine&lt;/span&gt;            


&lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;serverless-step-functions&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;serverless-iam-roles-per-function&lt;/span&gt;

&lt;span class="na"&gt;package&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;individually&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;**/*'&lt;/span&gt;
  &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;handler.py&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is just a dummy state machine to simulate a use case when we have do the following tasks &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ValidateImage: you could validate if the image is from a celebrity, or cats, dogs, etc &lt;/li&gt;
&lt;li&gt;GenerateThumbnail: generate a thumbnail of the image&lt;/li&gt;
&lt;li&gt;NotifyUser: send an email to the user with validation feedback&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;Outputs&lt;/strong&gt; section is to get the step function's ARN we are passing in the lambda environment variable&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is recommended and a best practice to always use the plugin &lt;a href="https://github.com/functionalone/serverless-iam-roles-per-function"&gt;serverless-iam-roles-per-function&lt;/a&gt; for all your serverless applications&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we deploy our application we need to install our serverless plugins and for that, we need to create a package.json file first, so let's do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now we install the plugins:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;serverless-step-functions
npm &lt;span class="nb"&gt;install &lt;/span&gt;serverless-iam-roles-per-function
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After that, we are ready to deploy our application so let's go ahead and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;serverless deploy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's go to the AWS console, and upload a file in the bucket created and you will see that your state machine is executed &lt;/p&gt;

&lt;p&gt;if you liked this post don't forget to click on ❤️&lt;/p&gt;

&lt;h1&gt;
  
  
  About me
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://twitter.com/raiselmelian"&gt;Raisel Melian’s Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/raisel-melian-78831895/"&gt;Raisel Melian’s Linkedin&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/channel/UCOJei3jcJXhtAuzPLOLtDBg?view_as=subscriber"&gt;Raisel Melian’s YouTube channel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>serverless</category>
      <category>stepfunction</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Manage dependencies like a pro when using Serverless Framework, serverless-python-requirements and Pipenv</title>
      <dc:creator>raisel melian</dc:creator>
      <pubDate>Thu, 13 Aug 2020 14:35:51 +0000</pubDate>
      <link>https://dev.to/raiselmelian/development-workflow-with-serverless-framework-serverless-python-requirements-and-pipenv-bm2</link>
      <guid>https://dev.to/raiselmelian/development-workflow-with-serverless-framework-serverless-python-requirements-and-pipenv-bm2</guid>
      <description>&lt;p&gt;In this post, I'm gonna show you how to manage python dependencies when using the Serverless Framework. We'll use &lt;a href="https://pipenv-fork.readthedocs.io/en/latest/"&gt;Pipenv&lt;/a&gt; but you can also apply a similar workflow if using other tools like &lt;a href="https://python-poetry.org/"&gt;poetry&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm assuming you have already installed &lt;a href="https://www.serverless.com/framework/docs/providers/aws/guide/installation/"&gt;serverless framework&lt;/a&gt; and have set up your credentials.&lt;/p&gt;

&lt;h1&gt;
  
  
  Setup
&lt;/h1&gt;

&lt;p&gt;Let's start by setting up our environment.&lt;/p&gt;

&lt;p&gt;If you are on a mac(you should):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install pipenv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;if you are using Windows(why?) or another OS you can check &lt;a href="https://pipenv-fork.readthedocs.io/en/latest/install.html#installing-pipenv"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the purpose of demonstration lets create a new project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;serverless create --template aws-python3 --path manage-dep-workflow
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This generates a new python v3 project inside a folder named &lt;strong&gt;manage-dep-workflow&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Development workflow
&lt;/h2&gt;

&lt;p&gt;The first time you navigate to your project's folder, in this case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd manage-dep-workflow
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you need to activate the pipenv shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipenv shell
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can start adding dependencies to your project with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipenv install &amp;lt;package&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;so let's add &lt;a href="https://docs.pytest.org/en/stable/"&gt;pytest&lt;/a&gt; as a dependency&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipenv install pytest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because we didn't specify a version for the dependency the latest is installed. If we wanted to do that we could do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipenv install pytest~=6.0.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After doing this, pipenv will create two files: one named Pipfile and another Pipfile.lock. If you are familiar with NPM these are similar to package.json and package-lock.json&lt;/p&gt;

&lt;p&gt;Let's take a look at Pipfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
pytest = "~=6.0.0"

[requires]
python_version = "3.8"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we can see the pytest library installed under the &lt;strong&gt;[packages]&lt;/strong&gt; section. There is another section &lt;strong&gt;[dev-packages]&lt;/strong&gt; intended for development only libraries like pytest for example, so if we want to move it we can do it manually or use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipenv install pytest~=6.0.0 --dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Integrate Pipenv and Serverless Framework
&lt;/h2&gt;

&lt;p&gt;Now that we know how to install libraries into our project the final step is to integrate Pipenv with the Serverless Framework so that when we deploy our application our dependencies are bundled into the lambda packages.&lt;/p&gt;

&lt;p&gt;For this, we are going to use the plugin &lt;a href="https://github.com/UnitedIncome/serverless-python-requirements#readme"&gt;serverless-python-requirements&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This plugin will automatically detect &lt;code&gt;Pipfile&lt;/code&gt; and &lt;code&gt;pipenv&lt;/code&gt; installed and generate a requirements.txt file.&lt;/p&gt;

&lt;p&gt;Let's go ahead and install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls plugin install -n serverless-python-requirements
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will add a new entry in your serverless.yml plugin section like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins:
  - serverless-python-requirements
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To verify everything is working together we can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls deploy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sls package
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In your terminal you will get an output like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Serverless: Generating requirements.txt from Pipfile...
Serverless: Parsed requirements.txt from Pipfile in /Users/melianr/dev/python/manage-dep-workflow/.serverless/requirements.txt...
Serverless: Installing requirements from /Users/melianr/Library/Caches/serverless-python-requirements/24891d0bfa66a0144c82d36d710b97b82ea9498d08e4b45957ab78e085a1fdf7_slspyc/requirements.txt ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see the Serverless Framework is effectively creating a    requirements.txt file on the fly and making it available for packaging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To get more information about Pipenv commands you can read the &lt;a href="https://pipenv-fork.readthedocs.io/en/latest/basics.html"&gt;official doc&lt;/a&gt; and to see more configuration options for the Serverless Python Requirements you can read more &lt;a href="https://github.com/UnitedIncome/serverless-python-requirements#readme"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  About me
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://twitter.com/raiselmelian"&gt;Raisel Melian’s Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/raisel-melian-78831895/"&gt;Raisel Melian’s Linkedin&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/channel/UCOJei3jcJXhtAuzPLOLtDBg?view_as=subscriber"&gt;Raisel Melian’s YouTube channel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>python</category>
      <category>devops</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
