Advanced Demo - Fully serverless app

[Prompt O’matic]

Building a simple serverless application using S3, API Gateway, Lambda, Step Functions, SNS & SES

Stages to Prompt O’Matic

Stage 1 - Configure Simple Email Service

The Prompt O’Matic application is going to send reminder messages via SMS and Email. It will use the simple email service or SES. In production, it could be configured to allow sending from the application email, to any app users.

SES starts off in sandbox mode, which means I’m only able to notify verified addresses (to avoid mass spamming). I first verified the sender address and the receiver address. At this point, I have whitelisted 2 email addresses for use with SES.

  • the Prompt O’Matic sending address

  • the Prompt O’Matic customer address

Also having configured an SMS number within SNS. For certain countries such as the USA, it would be necessary to arrange an origination number in Amazon Pinpoint.

Stage 2 - Email Lambda Function > SES

  • Here, I created an IAM role with the email_reminder_lambda function to interact with the rest of the stack. I used CloudFormation to automate the provision of SES, SNS & Logging permissions.

import boto3, os, json
FROM_EMAIL_ADDRESS = 'marcoverse@gmail.com'  ses = boto3.client('ses') def lambda_handler(event, context):     # Print event data to logs ..      print("Received event: " + json.dumps(event))     # Publish message directly to email, provided by EmailOnly or EmailPar TASK     ses.send_email( Source=FROM_EMAIL_ADDRESS,         Destination={ 'ToAddresses': [ event['Input']['email'] ] },          Message={ 'Subject': {'Data': 'Start studying you absolute deviants!'},             'Body': {'Text': {'Data': event['Input']['message']}}         }     )    return 'Success!'.

This stage configures the lambda function which will be used eventually to send emails on behalf of the serverless application.

Stage 3 - Configure State Machine Compute

The state machine will control the flow through the serverless application. The state machine starts ... and then waits for a certain time period based on the Timer state.
This is controlled by the web front end you.
Then the ChoiceState is used, and this is a branching part of the state machine. Depending on the option picked in the UI, it either moves to:

  • EmailOnly : Which sends an email reminder

  • SMSOnly : Which sends only an SMS reminder

  • EmailandSMS : which is a parallel state which runs both ParallelEmail and ParallelSMS which does both.

Stage 4 - Create Lambda function proxy integration with APIGW

  • Creating a front-end API for the serverless app

    The front-end loads from S3, runs in the browser and communicates with this API. Using APIGW for the API endpoint, using Lambda to provide the back-end compute.

Function - api_lambda > Python 3.8 > Default execution role > Lambda Role

API name - Promptomatic / Endpoint type > Regional. CORS > Enabled

Stage 5 - Configure State Machine Compute

Create an S3 bucket and static website hosting which will host the application front end.
Downloading the source files for the front end, configure them to connect to API gateway and then upload them to S3. Then running validation checks.

Loads HTML & JS From S3 & Static hosting

  • Communicates via javascript to API Gateway

  • uses api_lambda as backing resource

  • runs a statemachine passing in parameters

  • state machine sends email, SMS or both

  • state machine terminates

EXPLANATION:

Previous
Previous

💾NAS - Home Private Cloud

Next
Next

🦁AWS Academy