<?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 Okoruwa Precious on Medium]]></title>
        <description><![CDATA[Stories by Okoruwa Precious on Medium]]></description>
        <link>https://medium.com/@okoruwap1?source=rss-e036ed1092dd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-Fm6pqYSih-kegzD</url>
            <title>Stories by Okoruwa Precious on Medium</title>
            <link>https://medium.com/@okoruwap1?source=rss-e036ed1092dd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 18:32:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@okoruwap1/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[AWS Serverless Architecture: Creating a Scalable Event Announcement System (Step by Step Guide)]]></title>
            <link>https://medium.com/@okoruwap1/aws-serverless-architecture-creating-a-scalable-event-announcement-system-step-by-step-guide-66aa032fa623?source=rss-e036ed1092dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/66aa032fa623</guid>
            <dc:creator><![CDATA[Okoruwa Precious]]></dc:creator>
            <pubDate>Mon, 26 May 2025 00:02:06 GMT</pubDate>
            <atom:updated>2025-05-26T00:02:06.170Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9368AqfGsLNaYgVJcbb8fw.png" /></figure><h3>1.1 Project Overview</h3><p>This project involves developing an event announcement website that allows users to subscribe for email notifications, view a list of upcoming events, and create new events using a simple form.</p><p>The frontend of the website — consisting of HTML, CSS, and a JSON file (events.json)—will be hosted using Amazon S3’s static website hosting feature. Once uploaded and configured, the site will be accessible through a public URL.</p><p>For backend functionality, API Gateway will be set up to expose two endpoints:</p><ul><li>One for subscribing users to notifications (/subscribe)</li><li>One for submitting new events (/create-event)</li></ul><p>A Lambda function will handle email subscriptions by adding submitted email addresses to an SNS topic. Another Lambda function will process event submissions, update the events.json file in the S3 bucket with the new event data, and trigger the SNS topic to notify all subscribers.</p><p>This project uses the following AWS services:</p><ul><li>Amazon S3 to host the frontend and store event data.</li><li>Amazon SNS to manage email subscriptions and send out event notifications.</li><li>AWS Lambda to handle the backend logic for event creation and subscription management.</li><li>API Gateway to expose RESTful endpoints that the frontend can interact with.</li><li>IAM roles and policies to control secure access to the S3 bucket and SNS topic.</li></ul><h3>1.2 Setting Up Frontend Hosting with Amazon S3</h3><p>To get the frontend live, follow these steps:</p><ol><li>Start by creating the necessary frontend files: index.html, styles.css, and events.json. These files make up the user interface and store the list of events.<br>You can download the pre-built files (index.html, styles.css, and events.json) from the provided GitHub repository: <a href="https://github.com/Okoruwap/event-announcement-system">https://github.com/Okoruwap/event-announcement-system</a></li><li>On the repository page, click on the <strong>Code</strong> button (green button)<br>Copy the repository URL (<strong>HTTPS</strong> is recommended).<br>- Open your terminal or command prompt and run: <br>git clone <a href="https://github.com/Okoruwap/event-announcement-system">https://github.com/Okoruwap/event-announcement-system</a> <br>- Navigate to the cloned directory: cd event-announcement-system <br>-<a href="https://github.com/Okoruwap/event-announcement-system.git"> <strong>Or</strong></a>, if you are not familiar with git, click on the Code button<br>- Select Download Zip to download the repository as a Zip file.<br>- Extract the Zip file to your local machine.</li><li>Next, create an S3 bucket in the AWS Management Console that will be used to host the static website.</li><li>Upload the three frontend files into the newly created bucket.</li><li>Enable static website hosting in the bucket’s settings and specify index.html as the default index document.</li><li>Configure the bucket policy to allow public access so that anyone can view the website.</li><li>Finally, open the provided S3 website endpoint URL in your browser to verify that the site is live and functioning.</li></ol><h3>#2 Create an S3 Bucket</h3><ul><li>Open the AWS Management Console and navigate to the <strong>S3</strong> service.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gsfasWuBV6ugju0xr1DFTQ.png" /></figure><ul><li>Click <strong>“Create bucket”</strong> and configure:</li><li><strong>Bucket Name</strong>: Use a unique name (e.g., my-event-announcement-website).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GoRVgZQto_YnyvO3nBbhkw.png" /></figure><ul><li><strong>Region</strong>: Select the same region where your backend services will be hosted (e.g., ap-south-1).</li><li><strong>Block Public Access</strong>: Disable “Block all public access” so the bucket can be used for static website hosting.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_-D5EepUao0Xfl9JHwcLgQ.png" /></figure><ul><li>Click <strong>“Create bucket”</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pw0MrH5YdMZbd9-Mfy8heg.png" /></figure><h3>#3 Upload Website Files to S3</h3><ul><li>Open the S3 bucket.</li><li>Click <strong>“Upload”</strong>, select the files (index.html, styles.css, and events.json), and upload them</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PtioOWdmLgB6eleli3r5hA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*yOf2k45X1jb8vUFZw6Ayig.png" /></figure><h3>#4 Enable Static Website Hosting</h3><ul><li>In the S3 bucket, go to the <strong>“Properties”</strong> tab.</li><li>Scroll to <strong>Static website hosting</strong> and enable it.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wX1lts_EHvzLyxhbV53M9A.png" /></figure><ul><li>Select <strong>“Host a static website”</strong>.</li><li>Enter index.html as the <strong>Index Document</strong> and leave the Error Document blank.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*guQm7rvxsdHqUlHFtflQuw.png" /></figure><ul><li>Save the configuration.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0tv07yFymduHxIT_9F7Kxg.png" /></figure><h3>#5 Set Permissions for Public Access</h3><ul><li>Navigate to the “Permissions” tab and navigate to the Bucket Policy. Click on Edit.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oSXcn5zAaDXNKyj-1_FxTQ.png" /></figure><ul><li>Add the policy as given below and click on save changes:</li></ul><pre>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Principal&quot;: &quot;*&quot;,<br>      &quot;Action&quot;: &quot;s3:GetObject&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:s3:::&lt;YOUR-UNIQUE-BUCKET-NAME&gt;/*&quot;<br>    }<br>  ]<br>}</pre><ul><li>Note: Replace &lt;YOUR-UNIQUE-BUCKET-NAME&gt; with your bucket name.</li></ul><h3>#6 Verify the Website</h3><ul><li>Copy the Bucket Website Endpoint from the Static website hosting settings.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iJfWiDJ_2ru2fF_1W3-ovw.png" /></figure><ul><li>Open the URL in a browser to confirm that the website loads successfully.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPJ1_6Ezx4fAz6H2ZyKdaQ.png" /></figure><h3>1.3 Integrate SNS Notifications and Lambda Functions</h3><h3>Steps to be Performed</h3><ol><li>Create an SNS topic</li><li>Create and test the Subscription Lambda function</li><li>Create and test the Event creation Lambda function</li></ol><h3>#1 Create an SNS Topic</h3><p>SNS (Simple Notification Service) will be used to notify subscribers when a new event is created. Here’s how to set it up:</p><p><strong>Go to the SNS Console</strong></p><ul><li>Navigate to the <strong>AWS SNS Console</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*7HFy2dyylI7UZAMpy3xtyA.png" /></figure><ul><li>Go to <strong>Create Topic</strong>. Give the Topic Name and Click on Next Step.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4hU-mE9pORdy2pzuenpKZg.png" /></figure><ul><li>Select <strong>Standard</strong> as the topic type.</li><li>Provide a name for your topic, such as EventAnnouncements.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*KYJROikVDbumklHEqU1VpA.png" /></figure><p><strong>Create the Topic</strong></p><ul><li>Leave the rest as defaults. Click <strong>Create topic</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kCxirfUkmaF7h247xLLcvg.png" /></figure><ul><li>You’ll be directed to the topic details page where you can see the <strong>Topic ARN</strong> (Amazon Resource Name). This will be used in your Lambda functions to send messages to the topic.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pmx8rAo8_EAZMVRgxvZ6kw.png" /></figure><p><strong>Note: </strong>Keep a copy of the <strong>Topic ARN</strong> for later use.</p><h3>#2 Create and Test the Subscription Lambda Function</h3><p>In this step, we will create the <strong>Subscription Lambda function</strong> that subscribes users to the SNS topic using their email address.</p><p><strong>1. Create IAM Role for Lambda Function</strong></p><p>Before we create the Lambda function, we need to set up an IAM role that gives the Lambda function permissions to interact with AWS SNS (for subscribing users) and log to CloudWatch (for logging Lambda execution).</p><p><strong>Steps to Create the IAM Role:</strong></p><p><strong>Navigate to IAM Console:</strong></p><ul><li>Go to the <strong>IAM Console</strong> in AWS.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*FzcAGbLd3d95OlglWnY9VA.png" /></figure><ul><li>On the left sidebar, select <strong>Roles</strong>, then click <strong>Create role</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNhmMGiNtXUxanANqEIVdQ.png" /></figure><p><strong>Choose a Trusted Entity:</strong></p><ul><li>Select <strong>AWS service</strong>.</li><li>For the use case, choose <strong>Lambda</strong> (this grants Lambda permissions to assume this role).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*6o_hYj2KvhOQ0TnbjaNLsA.png" /></figure><p><strong>Attach Permissions Policies:</strong></p><ul><li>Search for and select the following policies:</li><li><strong>AmazonSNSFullAccess</strong> — This grants the Lambda function permission to interact with SNS (subscribe users).</li><li>Use the predefined AWSLambdaBasicExecutionRole to grant CloudWatch logging permissions.</li></ul><p><strong>Review and Create the Role:</strong></p><ul><li>Give the role a name, such as LambdaSubscribeRole.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wWNGeaSL-O4NuPWUO4BH-A.png" /></figure><ul><li>Click Create role to complete the process.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*89kOCsdnAwNXm_Z-" /></figure><p><strong>2. Create the Subscription Lambda Function</strong></p><p>Now that we have the necessary IAM role, let’s create the Lambda function.</p><p><strong>Navigate to the Lambda Console:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*nooqT_69uip5u-li0vfqoQ.png" /></figure><ul><li>Go to the <strong>AWS Lambda Console</strong> and click <strong>Create function</strong>.</li></ul><p><strong>Configure the Lambda Function:</strong></p><ul><li><strong>Function Name:</strong> Enter SubscribeToSNSFunction.</li><li><strong>Runtime:</strong> Choose <strong>Python 3.12</strong></li><li>Click on ‘Create Function’.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*E3oV8R7pypVOiHVi" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*fYGAnMg2A4AB1xU3o4ToJA.png" /></figure><p><strong>Function Code:</strong></p><ul><li>In the code editor, replace the default code with the following code that handles email subscription to SNS:</li></ul><pre>import json<br>import boto3<br><br>def lambda_handler(event, context):<br>    # Log the entire event to CloudWatch<br>    print(&quot;Event received:&quot;, json.dumps(event))<br><br>    if &#39;body&#39; in event:<br>        body = event[&#39;body&#39;]<br>        email = body.get(&#39;email&#39;, None)<br><br>        if email:<br>            sns_client = boto3.client(&#39;sns&#39;)<br><br>            try:<br>                # Subscribe the user to the SNS topic (email subscription)<br>                response = sns_client.subscribe(<br>                    TopicArn=&#39;enter-sns-topic-ARN&#39;,  # Replace with your SNS Topic ARN<br>                    Protocol=&#39;email&#39;,<br>                    Endpoint=email<br>                )<br><br>                return {<br>                    &#39;statusCode&#39;: 200,<br>                    &#39;body&#39;: json.dumps({&#39;message&#39;: &#39;Subscription successful! Please check your email to confirm.&#39;})<br>                }<br><br>            except Exception as e:<br>                print(f&quot;Error subscribing user: {str(e)}&quot;)<br>                return {<br>                    &#39;statusCode&#39;: 500,<br>                    &#39;body&#39;: json.dumps({&#39;error&#39;: f&#39;Failed to subscribe: {str(e)}&#39;}).encode(&#39;utf-8&#39;)<br>                }<br><br>        else:<br>            return {<br>                &#39;statusCode&#39;: 400,<br>                &#39;body&#39;: json.dumps({&#39;error&#39;: &#39;Email not provided.&#39;})<br>            }<br><br>    return {<br>        &#39;statusCode&#39;: 400,<br>        &#39;body&#39;: json.dumps({&#39;error&#39;: &#39;Invalid request format.&#39;})<br>    }</pre><p><strong>Explanation of Code:</strong></p><ul><li><strong>sns_client = boto3.client(&#39;sns&#39;)</strong>: This creates an SNS client to interact with SNS.</li><li><strong>sns_client.subscribe</strong>: This subscribes the provided email address to the SNS topic.</li><li>The function checks if the email is present in the request body, and if so, subscribes it to the SNS topic using the provided ARN.</li></ul><p>📌 NOTE: Under code edit the above code replace the ‘enter-sns-topic-ARN’ with your SNS Topic ARN that we copied in the earlier step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/0*UfXTfjOUmjKnCWq_" /></figure><p><strong>3. Test the Lambda Function</strong></p><p>Now we’ll test the Lambda function to ensure that it works correctly. We’ll simulate an event where a user provides their email for subscription.</p><p><strong>Steps to Test the Lambda Function:</strong></p><p><strong>Configure a Test Event:</strong></p><ul><li>In the Lambda function console, click on <strong>Test</strong>.</li><li>Create a new test event by selecting <strong>Create new test event</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CRyHogPQy7ObqlzyT0MB0A.png" /></figure><ul><li>Give it a name like TestSubscribeEvent.</li><li>Use the following JSON as the test event:</li></ul><pre>{<br>  &quot;body&quot;: {<br>    &quot;email&quot;: &quot;user@example.com&quot;<br>  }<br>}</pre><p>This simulates a request body containing the email &quot;<a href="/service/mailto:user@example.com">user@example.com</a>&quot;</p><p>Note: Replace &quot;<a href="/service/mailto:user@example.com">user@example.com</a>&quot; with an email you have access to receive the email notification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*4dUIetEUf9DWdgm8_xtkZw.png" /></figure><ul><li>Click on Save.</li></ul><p><strong>Run the Test:</strong></p><ul><li>After configuring the test event, click on <strong>Test</strong>.</li><li>The Lambda function should run, and you should see the log output in the <strong>Execution result</strong> section.</li><li>The response should look like:</li></ul><pre>{<br>  &quot;statusCode&quot;: 200,<br>  &quot;body&quot;: &quot;{\&quot;message\&quot;: \&quot;Subscription successful! Please check your email to confirm.\&quot;}&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/1*hVDVmOoZNjH_wn7VvxP7JA.png" /></figure><p><strong>Verify the Subscription:</strong></p><ul><li>Check the email inbox (in this case, the email you provided)for a confirmation email.</li><li>The email should have a confirmation link from AWS SNS.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pAa98NdyKrCMiveT8nmucQ.png" /></figure><ul><li>Once the user confirms the subscription, the email will be successfully subscribed to the SNS topic.</li></ul><h3>#3 Create and Test Event Creation Lambda Function</h3><p>In this step, we’ll set up the <strong>Event Creation Lambda Function</strong> to update the events.json file in your S3 bucket and send notifications using your SNS topic.</p><p><strong>1. Create an IAM Role for the Lambda Function:</strong></p><p>This role grants the necessary permissions for the Lambda function to:</p><ol><li>Read and write the events.json file in the S3 bucket.</li><li>Publish messages to the SNS topic.</li></ol><p><strong>Navigate to IAM console &amp; Create a New Role:</strong></p><ul><li>Click <strong>Create Role</strong>.</li><li>Select <strong>AWS Service</strong> and choose <strong>Lambda</strong>.</li><li>Click <strong>Next</strong> to proceed.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mFRRnNtgvAQOOKUX" /></figure><p><strong>Attach Policies:</strong></p><ul><li>Select <strong>AWS managed policies</strong>:</li><li>AmazonS3FullAccess (or create a custom policy with restricted S3 access to your bucket).</li><li>AmazonSNSFullAccess (or create a custom policy with access to your SNS topic).</li></ul><p><strong>Review and Create:</strong></p><ul><li>Name the role, e.g., EventCreationLambdaRole.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eMm5sstOil_d-tmD" /></figure><ul><li>Click Create Role.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hqeLZvfZWCz5jgOz" /></figure><p><strong>2. Create the Event Creation Lambda Function</strong></p><p>This function will:</p><ol><li>Append new events to the events.json file stored in S3.</li><li>Notify subscribers via SNS about the new event.</li></ol><p><strong>Navigate to AWS Lambda console &amp; Create the Function:</strong></p><ul><li>Click <strong>Create Function</strong> and choose <strong>Author from scratch</strong>.</li><li>Fill in the details:</li><li>Function name: createEventFunction.</li><li>Runtime: <strong>Python 3.12</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FYRfaV2vZ6077-_X" /></figure><ul><li>Select the IAM role created earlier (EventCreationLambdaRole). Click on Create Function.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G6leKWhGrJAv4fLb" /></figure><p><strong>Upload the Code:</strong></p><ul><li>Copy the following code into the inline editor:</li></ul><pre>import json<br>import boto3<br>from botocore.exceptions import ClientError<br><br>s3 = boto3.client(&#39;s3&#39;)<br>sns = boto3.client(&#39;sns&#39;)<br><br># S3 and SNS configuration<br>bucket_name = &#39;your-bucket-name&#39;<br>events_file_key = &#39;events.json&#39;<br>sns_topic_arn = &#39;your-sns-topic-arn&#39;<br><br>def lambda_handler(event, context):<br>    try:<br>        new_event = json.loads(event[&#39;body&#39;])  # Parse event body<br><br>        # Fetch existing events<br>        response = s3.get_object(Bucket=bucket_name, Key=events_file_key)<br>        events_data = json.loads(response[&#39;Body&#39;].read().decode(&#39;utf-8&#39;))<br><br>        # Add the new event<br>        events_data.append(new_event)<br><br>        # Update events.json in S3<br>        s3.put_object(<br>            Bucket=bucket_name,<br>            Key=events_file_key,<br>            Body=json.dumps(events_data, indent=2),<br>            ContentType=&#39;application/json&#39;<br>        )<br><br>        # Send SNS notification<br>        message = f&quot;New Event: {new_event[&#39;title&#39;]} on {new_event[&#39;date&#39;]}\n{new_event[&#39;description&#39;]}&quot;<br>        sns.publish(TopicArn=sns_topic_arn, Message=message, Subject=&quot;New Event Announcement&quot;)<br><br>        return {<br>            &#39;statusCode&#39;: 200,<br>            &#39;headers&#39;: {<br>                &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;,<br>                &quot;Access-Control-Allow-Methods&quot;: &quot;OPTIONS, POST&quot;,<br>                &quot;Access-Control-Allow-Headers&quot;: &quot;Content-Type&quot;<br>            },<br>            &#39;body&#39;: json.dumps({&#39;message&#39;: &#39;Event created successfully!&#39;})<br>        }<br>    <br>    except ClientError as e:<br>        print(f&quot;Error: {e}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 500,<br>            &#39;headers&#39;: {<br>                &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;,<br>                &quot;Access-Control-Allow-Methods&quot;: &quot;OPTIONS, POST&quot;,<br>                &quot;Access-Control-Allow-Headers&quot;: &quot;Content-Type&quot;<br>            },<br>            &#39;body&#39;: json.dumps({&#39;message&#39;: &#39;Error processing the event&#39;})<br>        }<br>    <br>    except Exception as e:<br>        print(f&quot;Unexpected Error: {e}&quot;)<br>        return {<br>            &#39;statusCode&#39;: 500,<br>            &#39;headers&#39;: {<br>                &quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;,<br>                &quot;Access-Control-Allow-Methods&quot;: &quot;OPTIONS, POST&quot;,<br>                &quot;Access-Control-Allow-Headers&quot;: &quot;Content-Type&quot;<br>            },<br>            &#39;body&#39;: json.dumps({&#39;message&#39;: &#39;Unexpected error occurred&#39;})<br>        }</pre><p><strong>Update the Configuration:</strong></p><ul><li>Replace your-bucket-name with your S3 bucket name.</li><li>Replace your-sns-topic-arn with your SNS topic ARN.</li></ul><p><strong>Deploy the function.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*AHVo5S9cygrecArLpnYn4g.png" /></figure><p><strong>3. Test the Event Creation Lambda Function</strong></p><p>Verify that the Lambda function can update the events.json file in S3 and send notifications to SNS.</p><p><strong>Prepare a Test Event:</strong></p><ul><li>Use the following JSON as the test event:</li></ul><pre>{<br> &quot;httpMethod&quot;: &quot;POST&quot;,<br>  &quot;body&quot;: &quot;{\&quot;title\&quot;:\&quot;Tech Meetup\&quot;,\&quot;date\&quot;:\&quot;2024-12-01\&quot;,\&quot;description\&quot;:\&quot;A gathering of tech enthusiasts!\&quot;}&quot;<br>}</pre><p><strong>Run the Test:</strong></p><ul><li>Click <strong>Test</strong> and paste the JSON above.</li><li>Name the test, e.g., TestEventCreation.</li><li>Click on Save and test the new test event.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*alEPjF3BHZFCzhRJZ2bf0g.png" /></figure><p><strong>Verify the Output:</strong></p><ul><li>Expected response:</li></ul><pre>{<br>  &quot;statusCode&quot;: 200,<br>  &quot;body&quot;: &quot;{\&quot;message\&quot;: \&quot;Event created successfully!\&quot;}&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*OmtBQhmIcEGCMYA4orJh3g.png" /></figure><p>Confirm that:</p><ul><li>The events.json file in S3 was updated with the new event. (You can download the events.json and check if you are curious or check the last modified time for events.json to make sure there was an update)</li><li>Subscribers received an email notification about the new event.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m7USlO2yOLWCXPTdExzMTA.png" /></figure><h3>1.4 Setup, Test and Deploy the API Gateway</h3><h3>Steps to be Performed 👩‍💻</h3><ol><li>Create a REST API in API Gateway</li><li>Create and test the /subscribe Endpoint</li><li>Create and test the /create-event Endpoint</li><li>Deploy the API</li></ol><h3>#1 Create a REST API in API Gateway</h3><p>We need to set up a REST API to manage requests for subscribing users and creating events. To do this —</p><p><strong>Go to the API Gateway Console</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*2RDtHfUEZ0NhbZ9bB9HddQ.png" /></figure><p><strong>Create a New REST API:</strong></p><ul><li>Select <strong>Create API</strong> &gt; <strong>REST API</strong> &gt; <strong>Build</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*mFkUFb55WMDxumJK7VBI-w.png" /></figure><ul><li>Choose:</li><li><strong>API Details</strong>: New API</li><li><strong>API name</strong>: EventManagementAPI.</li><li><strong>Endpoint Type</strong>: Regional.</li><li>Click <strong>Create API</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*WktRvFiwJghWOF-K7n6ldg.png" /></figure><h3>#2 Create and Test the /subscribe Endpoint</h3><p>We need to connect the subscribeToSNSFunction Lambda function to a /subscribe endpoint for email subscriptions. To do this—</p><p><strong>Add a Resource:</strong></p><ul><li>Click on <strong>Create Resource</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*zY6jVg57oh8wm8dsS7HREA.png" /></figure><ul><li>Enter:</li><li><strong>Resource Name</strong>: subscribe.</li><li><strong>Resource Path</strong>: /.</li><li>Enable the CORS checkbox.</li><li>Click <strong>Create Resource</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*6FdPrBzOHNh-kPWPRX3b3Q.png" /></figure><p><strong>Create a POST Method:</strong></p><ul><li>Select the /subscribe resource.</li><li>Click on <strong>Create Method</strong> &gt; <strong>POST</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tGwR8EjIwMqenjCS" /></figure><ul><li>Click on Create Method.</li></ul><p><strong>Enable CORS:</strong></p><ul><li>Select the /subscribe POST method.</li><li>Click on <strong>Enable CORS</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*kp0unczmoVWYoV2csThvAA.png" /></figure><ul><li><strong>Enable the checkboxes as shown below and click on Save.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3Da5B7-yR-b9l6xg" /></figure><p><strong>Add a mapping template:</strong></p><ul><li>Select the /subscribe POST method. Click <strong>Integration Request</strong>. Click Edit.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*X7eKmnt_s6p0h8j2Jc3zgw.png" /></figure><ul><li>Under the Mapping Templates section, click Add mapping template.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2lbDxIO33rtea7gq" /></figure><ul><li>Enter application/json as the content type.</li><li>In the text box that appears, add the following mapping template:</li></ul><pre>{<br>  &quot;body&quot;: $input.json(&#39;$&#39;)<br>}</pre><ul><li>This template wraps the entire request body within a body field, matching the Lambda function’s expectations.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*vt-BEC11HzMJJEkrUCv4qw.png" /></figure><p>Click on Save.</p><p><strong>Test the </strong><strong>/subscribe Endpoint:</strong></p><ul><li>In the API Gateway console, click <strong>Test</strong> on the POST method.</li><li>Provide the following request body:</li></ul><pre>{<br>  &quot;email&quot;: &quot;user@example.com&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*5jqHmnUTDuJqrNkZ3uqjzA.png" /></figure><ul><li>Click Test and verify that the Lambda function is invoked successfully.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*S_nCXb9P3TC1hwh6Je4Z6A.png" /></figure><h3>#3 Create and test the /create-event Endpoint</h3><p>Now, we need to connect the createEventFunction Lambda function to a /create-event endpoint for event creation. To do this—</p><p><strong>Add a Resource:</strong></p><ul><li>Click on <strong>Create Resource</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*XphzS_vwoehIIEzXxU8BKQ.png" /></figure><ul><li>Enter:</li><li><strong>Resource Name</strong>: create-event.</li><li><strong>Resource Path</strong>: /.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*5SNZnFcolRj9f1Kq00usVA.png" /></figure><ul><li>Click <strong>Create Resource</strong>.</li></ul><p><strong>Create a POST Method:</strong></p><ul><li>Select the /create-event resource.</li><li>Click on <strong>Create Method</strong> &gt; <strong>POST</strong>.</li><li>Set the integration type to <strong>Lambda Function</strong>.</li><li>Enable the <strong>Lambda Proxy integration</strong> toggle.</li><li>Enter the Lambda function name: createEventFunction.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*a4onJy5OHU8giQW0Ift-gw.png" /></figure><ul><li>Click Create Method.</li></ul><p><strong>Enable CORS:</strong></p><ul><li>Select the /create-event POST method.</li><li>Click on <strong>Enable CORS</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/829/1*LgfWxn0cnMDkeMGooyyrug.png" /></figure><ul><li><strong>Enable the checkboxes as given below and click on Save</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*Jq5Zun4Kvmp6KSqafVUGhQ.png" /></figure><p><strong>Test the </strong><strong>/create-event Endpoint:</strong></p><ul><li>In the API Gateway console, click <strong>Test</strong> on the POST method.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*SkhwBdiyAWu2vJSlloTUdw.png" /></figure><ul><li>Provide the following request body:</li></ul><pre>{<br>  &quot;title&quot;: &quot;Tech Meetup&quot;,<br>  &quot;date&quot;: &quot;2024-12-01&quot;,<br>  &quot;description&quot;: &quot;A gathering of tech enthusiasts!&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*EglRpQ9Ah0Q4pJoIAi0saw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*AEnLHdLUYQ1ALPBjAdWS0Q.png" /></figure><ul><li>Click <strong>Test</strong> and verify that:</li><li>The event is added to the events.json file in S3.</li><li>Subscribers receive a notification.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*01LRAuQU6QqOrp30HuyEpA.png" /></figure><h3>#4 Deploy the API</h3><p>We need to make the API publicly accessible for external requests we need to deploy the API. To do this —</p><p><strong>Create a Deployment:</strong></p><ul><li>In the API Gateway console, first click to your “/” path and then click on <strong>Deploy API</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*5Y7JzvvHAx5x-mu0pmLQGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*q3Hpnei537WrBmjR5Fi-KQ.png" /></figure><p><strong>Note the Invoke URLs:</strong></p><ul><li>After deployment, copy <strong>Invoke URL</strong>:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LTU0errpDJzX7kZK" /></figure><ul><li>The invoke URL for your endpoints would be in the form — invokeURL/endpointName. For example:</li><li>suscribe: https://&lt;api-id&gt;.execute-api.&lt;region&gt;.amazonaws.com/dev/subscribe</li><li>create-event: https://&lt;api-id&gt;.execute-api.&lt;region&gt;.amazonaws.com/dev/create-event</li></ul><p>Remember to copy these endpoints as we will be adding these endpoints in the code in the upcoming steps.</p><h3>1.5 Test and Finalize</h3><h3>Steps to be Performed 👩‍💻</h3><ol><li>Update the Frontend code</li><li>Upload the updated files in S3</li><li>Test the website</li></ol><h3>#1 Update the Frontend Code</h3><p>Integrate the API Gateway endpoints into the index.html file by replacing the placeholders with the actual URLs of the /subscribe and /create-event API Gateway endpoints.</p><p>Replace the relevant sections of the script in index.html:</p><pre>// Subscribe function to integrate with API Gateway and Lambda for SNS subscription<br>async function subscribeToEvents() {<br>    const email = prompt(&quot;Enter your email to subscribe to event notifications:&quot;);<br>    if (email) {<br>        try {<br>            const response = await fetch(&#39;&lt;YOUR_SUBSCRIBE_API_ENDPOINT&gt;&#39;, { // Replace with actual endpoint<br>                method: &#39;POST&#39;,<br>                headers: {<br>                    &#39;Content-Type&#39;: &#39;application/json&#39;<br>                },<br>                body: JSON.stringify({ email: email })<br>            });<br><br>            const data = await response.json();<br>            console.log(&quot;API Response:&quot;, data);<br><br>            if (response.ok) {<br>                alert(data.message); // Display success message from Lambda<br>            } else {<br>                alert(&quot;Subscription failed. Please try again.&quot;);<br>                console.error(data);<br>            }<br>        } catch (error) {<br>            alert(&quot;Error subscribing to events. Please try again later.&quot;);<br>            console.error(&quot;Error:&quot;, error);<br>        }<br>    } else {<br>        alert(&quot;Email is required for subscription.&quot;);<br>    }<br>}<br><br>// Function to submit a new event<br>async function submitNewEvent(event) {<br>    event.preventDefault();<br><br>    const title = document.getElementById(&quot;event-title&quot;).value;<br>    const date = document.getElementById(&quot;event-date&quot;).value;<br>    const description = document.getElementById(&quot;event-description&quot;).value;<br><br>    const newEvent = {<br>        title: title,<br>        date: date,<br>        description: description<br>    };<br><br>    try {<br>        const response = await fetch(&#39;&lt;YOUR_CREATE_EVENT_API_ENDPOINT&gt;&#39;, { // Replace with actual endpoint<br>            method: &#39;POST&#39;,<br>            headers: {<br>                &#39;Content-Type&#39;: &#39;application/json&#39;<br>            },<br>            body: JSON.stringify(newEvent)<br>        });<br><br>        const result = await response.json();<br><br>        if (response.ok) {<br>            alert(&quot;New event created successfully!&quot;);<br>            loadEvents();  // Reload events after creating a new one<br>        } else {<br>            alert(&quot;Failed to create new event.&quot;);<br>            console.error(result);<br>        }<br>    } catch (error) {<br>        alert(&quot;Error creating new event. Please try again.&quot;);<br>        console.error(&quot;Error:&quot;, error);<br>    }<br>}</pre><blockquote><em>Replace &lt;YOUR_SUBSCRIBE_API_ENDPOINT&gt; and &lt;YOUR_CREATE_EVENT_API_ENDPOINT&gt; with the actual API Gateway URLs for the /subscribe and /create-event endpoints, respectively.</em></blockquote><p>💡 Where to get these API Gateway URLs? As mentioned earlier, from your AWS Console navigate to API Gateway → &lt;YOUR_API_NAME&gt; → From the sidebar, goto Stages → Copy the Invoke URL</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5VB1-LKsTPP2Z8Ut" /></figure><p>Now replace &lt;YOUR_SUBSCRIBE_API_ENDPOINT&gt;by &lt;INVOKE_URL&gt;/subscribe and &lt;YOUR_CREATE_EVENT_API_ENDPOINT&gt; by &lt;INVOKE_URL&gt;/create-event</p><h3>#2 Upload the Updated Files to S3</h3><ol><li>Open the AWS Management Console and navigate to <strong>S3</strong>.</li><li>Open the S3 bucket where your website is hosted.</li><li>Delete the existing index.html file.</li><li>Upload the updated index.html file along with any other updated assets (if applicable).</li></ol><h3>#3 Test the Website</h3><p>Once the updates are live, thoroughly test the website to ensure all functionalities are working as expected:</p><p><strong>Access the Website:</strong></p><ul><li>Open your website in a browser using the URL (The S3 bucket URL).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L82Qi_enVPNlEPlQaHJZow.png" /></figure><p><strong>Test the Subscription Feature:</strong></p><ul><li>Click on the “Subscribe to Events” button.</li><li>Enter your email address when prompted.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*oN2uGBEajbUByd-0AZphLg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/1*tx6Z5Cf7ISiAGwV8_QcYOw.png" /></figure><ul><li>Verify that you receive a subscription confirmation email.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TkRGykVmytCxfspu" /></figure><ul><li>Confirm the subscription through the email.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/0*Mj5Stcalzv6D3myC" /></figure><p><strong>Test the Create Event Feature:</strong></p><ul><li>Click on the “Create New Event” button.</li><li>Fill in the event details (title, date, description) and submit the form.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*w4HelF4x8XaZG0-Dlv01_g.png" /></figure><p><strong>Verify that:</strong></p><ul><li>You get the successful event creation message.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*EQdSYKbzvTfUhSjvgt6EHw.png" /></figure><ul><li>Subscribers receive an email notification about the new event.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*ZNlO3HA5y-vClub7kDaOow.png" /></figure><ul><li>The new event appears on the website after refreshing the page.</li></ul><p>Congratulations! That’s the end.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66aa032fa623" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>