<?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: Fatma Sena Ekiz</title>
    <description>The latest articles on DEV Community by Fatma Sena Ekiz (@neptunel).</description>
    <link>https://dev.to/neptunel</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%2F1055733%2F7a7abc2a-6af8-414f-9789-bcf9c2b47d7b.jpeg</url>
      <title>DEV Community: Fatma Sena Ekiz</title>
      <link>https://dev.to/neptunel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neptunel"/>
    <language>en</language>
    <item>
      <title>Store Thumbnails from Your Live Stream Using AWS SAM CLI to Set Up Lambda Function and API Gateway</title>
      <dc:creator>Fatma Sena Ekiz</dc:creator>
      <pubDate>Thu, 20 Apr 2023 22:50:51 +0000</pubDate>
      <link>https://dev.to/neptunel/using-aws-sam-cli-to-set-up-lambda-function-and-api-gateway-19c4</link>
      <guid>https://dev.to/neptunel/using-aws-sam-cli-to-set-up-lambda-function-and-api-gateway-19c4</guid>
      <description>&lt;p&gt;The AWS SAM Command Line Interface (CLI) lets you locally build, test, and debug serverless applications defined by AWS SAM templates. The SAM CLI provides a Lambda-like execution environment locally and helps you catch issues upfront by providing parity between the actual Lambda execution environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; &lt;a href="https://github.com/dolbyio-samples/streaming-webhook-thumbnails/tree/main/aws-sam-thumbnails" rel="noopener noreferrer"&gt;This repository&lt;/a&gt; contains the code you will implement, so you can apply the steps and configure your own SAM project by copying and changing the content in your SAM template project with the code in this folder.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 0 - Install AWS SAM CLI&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Complete all &lt;a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/prerequisites.html" rel="noopener noreferrer"&gt;prerequisites&lt;/a&gt; before moving forward with installing AWS SAM CLI. This includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Signing up for an AWS account.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating an administrative IAM user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating an access key ID and secret access key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Installing the AWS CLI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configuring AWS credentials.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then, follow &lt;a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html" rel="noopener noreferrer"&gt;this&lt;/a&gt; tutorial to install AWS SAM CLI. Then, create a &lt;a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html" rel="noopener noreferrer"&gt;sample Hello World&lt;/a&gt; application to start with a project template. Choose the options below to configure a project from the CLI.&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%2Ft2wtq64yklm6s9l148qr.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%2Ft2wtq64yklm6s9l148qr.png" alt="Image description"&gt;&lt;/a&gt; &lt;br&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%2Fnb15ti5lbgfd2cu1f7l7.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%2Fnb15ti5lbgfd2cu1f7l7.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
After configuring your project, it will come with a &lt;em&gt;template.yaml&lt;/em&gt; file that contains a Lambda function called &lt;em&gt;HelloWorldFunction&lt;/em&gt; and an API event. Navigate through your project and open it in a code editor.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1- Edit the Lambda Function and Create an API Gateway&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;We need to update the &lt;em&gt;template.yaml&lt;/em&gt; by adding a new API and reference it under the API events.&lt;/p&gt;

&lt;p&gt;Add this code block under your Resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      BinaryMediaTypes:
      - image/jpeg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, reference it under Events like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Events:
        HelloWorldApi:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /hello
            Method: POST
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remove the &lt;em&gt;ApplicationResourceGroup&lt;/em&gt; and &lt;em&gt;Outputs&lt;/em&gt; part. Your &lt;em&gt;template.yaml&lt;/em&gt; file should look like this at the end (Description part is same with how you specify your project's name during installation, so it may be different in your project):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: &amp;gt;
  blog-aws-sam-thumbnails

  Sample SAM Template for blog-aws-sam-thumbnails

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      BinaryMediaTypes:
      - image/jpeg
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs18.x
      Architectures:
        - x86_64
      Events:
        HelloWorldApi:
          Type: Api
          Properties:
            RestApiId: !Ref MyApi
            Path: /hello
            Method: POST
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2 - Deploying the SAM project&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Let's test if we can successfully set up our Lambda Function and API Gateway. For the purpose of this walkthrough, we'll simply log the payload of the received event to the console and return a 200 status code back to the source to indicate successful reception. In the next steps will implement the code to actually handle the call.&lt;/p&gt;

&lt;p&gt;Go to &lt;em&gt;app.mjs&lt;/em&gt; under &lt;em&gt;hello-world&lt;/em&gt; folder and add a console.log(event). Your app.mjs should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const lambdaHandler = async (event, context) =&amp;gt; {
    try {
        console.log(event)
        return {
            'statusCode': 200,
            'body': JSON.stringify({
                message: 'hello world',
            })
        }
    } catch (err) {
        console.log(err);
        return err;
    }
};

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

&lt;/div&gt;



&lt;p&gt;Then, go to root of your project (where &lt;em&gt;template.yaml&lt;/em&gt; is available) and run &lt;strong&gt;&lt;em&gt;sam build&lt;/em&gt;&lt;/strong&gt; in your terminal. After you get  Build Succeeded notification, run &lt;strong&gt;&lt;em&gt;sam deploy --guided&lt;/em&gt;&lt;/strong&gt; Say yes(Y) to deployment wizard's prompts as shown below:&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%2Fg9yerbtb3l08orji36ed.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%2Fg9yerbtb3l08orji36ed.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you get &lt;em&gt;Successfully created/updated stack ...&lt;/em&gt; navigate through your API Gateway and Stages in your AWS Console and copy the "Invoke URL" in the "Prod" stage.&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%2Fjdxz1z1xz7jplvctsghv.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%2Fjdxz1z1xz7jplvctsghv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, you can use the "Invoke URL" as a webhook URL by adding the /hello Path to the end, as defined in your template.yaml when setting up the API gateway. So, for example, your webhook URL should be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://23k6og5dc1.execute-api.us-west1.amazonaws.com/Prod/hello&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3 - Set Up Dolby.io to Test the Webhook&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To create a token on your Dashboard go to &lt;a href="https://streaming.dolby.io/#/tokens" rel="noopener noreferrer"&gt;https://streaming.dolby.io/#/tokens&lt;/a&gt;  and click &lt;code&gt;Create&lt;/code&gt; button. After you set the Basic settings, you need to enable &lt;code&gt;Thumbnail Preview&lt;/code&gt; from the Advanced settings.&lt;/li&gt;
&lt;/ul&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%2F0rbzzmj16mheyjrea4zg.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%2F0rbzzmj16mheyjrea4zg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To create a webhook on your Dashboard go to &lt;a href="https://streaming.dolby.io/#/webhooks" rel="noopener noreferrer"&gt;https://streaming.dolby.io/#/webhooks&lt;/a&gt; and click &lt;code&gt;Create&lt;/code&gt; button. You need to enable &lt;code&gt;Thumbnail hooks&lt;/code&gt; and add the Webhook URL that you got from the previous step.&lt;/li&gt;
&lt;/ul&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%2Fj531isfu8dqnao625udv.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%2Fj531isfu8dqnao625udv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4 - Handling the Event and Storing the Thumbnails in an S3 Bucket&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After successfully seeing the log of the event in the "CloudWatch", we are now ready to implement the functionality to handle the event to compare signatures and upload the thumbnail image to the created S3 bucket.&lt;br&gt;
→ First, replace the entire &lt;em&gt;app.mjs&lt;/em&gt; file in your project with the code in the &lt;a href="https://github.com/dolbyio-samples/streaming-webhook-thumbnails/blob/main/aws-sam-thumbnails/hello-world/app.mjs" rel="noopener noreferrer"&gt;app.mjs file&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;→ Then navigate through your &lt;em&gt;hello-world&lt;/em&gt; folder directory --- the directory that contains the Lambda function --- and type &lt;strong&gt;&lt;em&gt;npm install crypto util aws-sdk&lt;/em&gt;&lt;/strong&gt; to install the packages that our code uses. Save your code and go back to the root directory of your project where the &lt;em&gt;template.yaml&lt;/em&gt; exists.&lt;/p&gt;

&lt;p&gt;→ Next, we need to create an S3 bucket. From your AWS Management Console search S3, from Buckets click &lt;em&gt;'Create Bucket'&lt;/em&gt; and set a name for your bucket. &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%2Fo6n0t320b4v0to1gkj1b.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%2Fo6n0t320b4v0to1gkj1b.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
(We used the name &lt;em&gt;blog-thumbnails-bucket&lt;/em&gt; for the sample code in this repository.) &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%2Fugjwi30td77xiqzwv9ro.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%2Fugjwi30td77xiqzwv9ro.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, use services search bar for IAM, and go to &lt;em&gt;Policies&lt;/em&gt; under IAM Dashboard. Click &lt;em&gt;Create Policy&lt;/em&gt; and from Visiual editor choose a Service as &lt;em&gt;S3&lt;/em&gt;. From Actions, choose &lt;em&gt;PutObject&lt;/em&gt; to specify actions allowed in S3. And for Resources, click Add ARN(s) and enter the 'Bucket name' and select 'Any' for 'Object name' and click 'Add'. Select a name for your policy on the 'Review policy' part and click 'Create policy' button.  Lastly, from IAM Roles menu, find your Lambda function and from 'Add Permission' button, click 'Attach Policies' and find the policy you created and click 'Add Permissions'. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dolbyio-samples/streaming-webhook-thumbnails/blob/main/assets/s3.gif" rel="noopener noreferrer"&gt;Watch how to apply the previous steps to set up an S3 bucket&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;→ Now, we need to add the WEBHOOK_SECRET as an environment variable to our Lambda function. Go to the &lt;a href="https://streaming.dolby.io/#/webhooks" rel="noopener noreferrer"&gt;Dolby.io Dashboard&lt;/a&gt; and click on the webhook you created, copy the Webhook Secret. Navigate to the AWS Lambda Console Configuration menu and add an environment variable called WEBHOOK_SECRET. &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%2F3dzhlge329vciatj0cm8.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%2F3dzhlge329vciatj0cm8.png" alt="Image description"&gt;&lt;/a&gt;&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%2Ftw46jhnb98h44znhtwye.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%2Ftw46jhnb98h44znhtwye.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before you deploy the project, go to your &lt;em&gt;samconfig.toml&lt;/em&gt; and change the &lt;em&gt;'resolve_s3'&lt;/em&gt; parameters (lines 23 and 32) to &lt;em&gt;'false'&lt;/em&gt; in your project.&lt;/p&gt;

&lt;p&gt;→ Now that we've set up an S3 bucket, updated our Lambda function code accordingly, and added the WEBHOOK_SECRET as an environment variable --- we are good to run &lt;strong&gt;&lt;em&gt;sam build&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;sam deploy&lt;/em&gt;&lt;/strong&gt; in our terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;TEST TIME!!!&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Now, go back to your &lt;a href="http://Dolby.io" rel="noopener noreferrer"&gt;Dolby.io&lt;/a&gt; dashboard and start a Broadcast. Then, navigate back to your lambda function's "CloudWatch logs". You should be receiving an event log every 30 seconds, and check your S3 bucket and view the logged image objects.&lt;/p&gt;

&lt;p&gt;And, that's it! Now, you know how you can receive and store the thumbnail images of the ongoing stream.💪&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>sam</category>
      <category>dolbyio</category>
    </item>
  </channel>
</rss>
