<?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 Ali Usmani on Medium]]></title>
        <description><![CDATA[Stories by Ali Usmani on Medium]]></description>
        <link>https://medium.com/@aliusmani2010?source=rss-3a9a92f9d194------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-OoSpNMmBxohnE260WGUzQ.jpeg</url>
            <title>Stories by Ali Usmani on Medium</title>
            <link>https://medium.com/@aliusmani2010?source=rss-3a9a92f9d194------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 12:40:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aliusmani2010/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[Securing ECS Fargate Workloads with API Gateway VPC Links]]></title>
            <link>https://medium.com/@aliusmani2010/securing-ecs-fargate-workloads-with-api-gateway-vpc-links-64ab7aa1b193?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/64ab7aa1b193</guid>
            <category><![CDATA[mcp-server]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws-fargate]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Wed, 16 Sep 2026 05:38:11 GMT</pubDate>
            <atom:updated>2026-09-16T05:39:00.162Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Does The Pattern Work?</h3><p>This pattern allows integration of a public API gateway endpoint to a private HTTP endpoint. In this case, the private endpoint is an Application Load Balancer (ALB) with an ECS Fargate cluster behind it. API Gateway integration between the public API endpoint and the private ALB is enabled using VPC Link. It allows building a secure pattern without exposing the private subnet resources, and it can be accessed only via a VPC Link.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bssaZT9Y3omTmpB_JCVSVw.png" /></figure><h3>LocalStack — Your local AWS cloud</h3><p>So, why did I mention LocalStack here?</p><p>I used it for my initial configuration and testing of the Terraform sample project. We would need the LocalStack Pro version for this project, as it uses certain services that are not available in the community version.</p><p>LocalStack allows you to simulate the AWS cloud environment locally without provisioning anything on AWS.</p><p>There are a lot of services available in the community version, which is free! The ‘Pro’ version will cost you some money, but it might still be worth it if you use AWS services for development regularly and need services that are not available in the community version.</p><p>Follow along the below steps if you want to run the Terraform project.</p><ul><li><a href="https://learn.hashicorp.com/tutorials/terraform/install-cli"><strong>Install Terraform</strong></a><strong>.</strong></li><li><a href="https://docs.localstack.cloud/get-started/#get-localstack-up-and-running"><strong>Install and configure LocalStack</strong></a><strong>.</strong> [Pick whatever installation option works best for you.]</li></ul><p>Configure the “aws” provider to use fake credentials and a custom URL pointing to the LocalStack environment running locally. The file below is pretty much the same as in <a href="https://github.com/aws-samples/serverless-patterns/blob/main/apigw-vpclink-pvt-alb-terraform/main.tf"><strong>https://github.com/aws-samples/serverless-patterns/blob/main/apigw-vpclink-pvt-alb-terraform/main.tf</strong></a><strong> </strong>except for the provider configuration in line #14. I used the configuration below to include multiple services as a reference, but you can configure only the ones you need.</p><pre># Required providers configuration<br>terraform {<br>  required_providers {<br>    aws = {<br>      source  = &quot;hashicorp/aws&quot;<br>      version = &quot;~&gt; 4.0.0&quot;<br>    }<br>  }<br><br>  required_version = &quot;&gt;= 1.0.11&quot;<br>}<br><br># AWS provider configuration<br>provider &quot;aws&quot; {<br>  #profile = &quot;default&quot;<br>  #region  = &quot;us-east-1&quot;<br>  region                      = &quot;us-east-1&quot;<br>  access_key                  = &quot;mock_access_key&quot;<br>  secret_key                  = &quot;mock_secret_key&quot;<br>  s3_use_path_style           = true<br>  skip_credentials_validation = true<br>  skip_metadata_api_check     = true<br>  skip_requesting_account_id  = true<br><br>  endpoints {<br>    apigatewayv2   = &quot;http://localhost:4566&quot;<br>    apigateway     = &quot;http://localhost:4566&quot;<br>    cloudformation = &quot;http://localhost:4566&quot;<br>    cloudwatch     = &quot;http://localhost:4566&quot;<br>    dynamodb       = &quot;http://localhost:4566&quot;<br>    es             = &quot;http://localhost:4566&quot;<br>    firehose       = &quot;http://localhost:4566&quot;<br>    iam            = &quot;http://localhost:4566&quot;<br>    kinesis        = &quot;http://localhost:4566&quot;<br>    lambda         = &quot;http://localhost:4566&quot;<br>    route53        = &quot;http://localhost:4566&quot;<br>    redshift       = &quot;http://localhost:4566&quot;<br>    s3             = &quot;http://localhost:4566&quot;<br>    secretsmanager = &quot;http://localhost:4566&quot;<br>    ses            = &quot;http://localhost:4566&quot;<br>    sns            = &quot;http://localhost:4566&quot;<br>    sqs            = &quot;http://localhost:4566&quot;<br>    ssm            = &quot;http://localhost:4566&quot;<br>    stepfunctions  = &quot;http://localhost:4566&quot;<br>    sts            = &quot;http://localhost:4566&quot;<br>    ec2            = &quot;http://localhost:4566&quot;<br>    ecs            = &quot;http://localhost:4566&quot;<br>    elbv2          = &quot;http://localhost:4566&quot;<br>    elb            = &quot;http://localhost:4566&quot;<br>  }<br>}<br><br># Load balancer security group. CIDR and port ingress can be changed as required.<br>resource &quot;aws_security_group&quot; &quot;lb_security_group&quot; {<br>  description = &quot;LoadBalancer Security Group&quot;<br>  vpc_id = var.vpc_id<br>  ingress {<br>    description      = &quot;Allow from anyone on port 80&quot;<br>    from_port        = 80<br>    to_port          = 80<br>    protocol         = &quot;tcp&quot;<br>    cidr_blocks      = [&quot;0.0.0.0/0&quot;]<br>  }<br>}<br>resource &quot;aws_security_group_rule&quot; &quot;sg_ingress_rule_all_to_lb&quot; {<br>  type  = &quot;ingress&quot;<br>  description = &quot;Allow from anyone on port 80&quot;<br>  from_port         = 80<br>  to_port           = 80<br>  protocol          = &quot;tcp&quot;<br>  cidr_blocks       = [&quot;0.0.0.0/0&quot;]<br>  ipv6_cidr_blocks  = [&quot;::/0&quot;]<br>  security_group_id = aws_security_group.lb_security_group.id<br>}<br><br># Load balancer security group egress rule to ECS cluster security group.<br>resource &quot;aws_security_group_rule&quot; &quot;sg_egress_rule_lb_to_ecs_cluster&quot; {<br>  type  = &quot;egress&quot;<br>  description = &quot;Target group egress&quot;<br>  from_port         = 80<br>  to_port           = 80<br>  protocol          = &quot;tcp&quot;<br>  security_group_id = aws_security_group.lb_security_group.id<br>  source_security_group_id = aws_security_group.ecs_security_group.id<br>}<br><br># ECS cluster security group.<br>resource &quot;aws_security_group&quot; &quot;ecs_security_group&quot; {<br>  description = &quot;ECS Security Group&quot;<br>  vpc_id = var.vpc_id<br>  egress {<br>    description      = &quot;Allow all outbound traffic by default&quot;<br>    from_port        = 0<br>    to_port          = 0<br>    protocol         = &quot;-1&quot;<br>    cidr_blocks      = [&quot;0.0.0.0/0&quot;]<br>  }<br>}<br><br># ECS cluster security group ingress from the load balancer.<br>resource &quot;aws_security_group_rule&quot; &quot;sg_ingress_rule_ecs_cluster_from_lb&quot; {<br>  type  = &quot;ingress&quot;<br>  description = &quot;Ingress from Load Balancer&quot;<br>  from_port         = 80<br>  to_port           = 80<br>  protocol          = &quot;tcp&quot;<br>  security_group_id = aws_security_group.ecs_security_group.id<br>  source_security_group_id = aws_security_group.lb_security_group.id<br>}<br><br># Create the internal application load balancer (ALB) in the private subnets.<br>resource &quot;aws_lb&quot; &quot;ecs_alb&quot; {<br>  load_balancer_type = &quot;application&quot;<br>  internal = true<br>  subnets = var.private_subnets<br>  security_groups = [aws_security_group.lb_security_group.id]<br>}<br><br># Create the ALB target group for ECS.<br>resource &quot;aws_lb_target_group&quot; &quot;alb_ecs_tg&quot; {<br>  port        = 80<br>  protocol    = &quot;HTTP&quot;<br>  target_type = &quot;ip&quot;<br>  vpc_id      = var.vpc_id<br>}<br><br># Create the ALB listener with the target group.<br>resource &quot;aws_lb_listener&quot; &quot;ecs_alb_listener&quot; {<br>  load_balancer_arn = aws_lb.ecs_alb.arn<br>  port              = &quot;80&quot;<br>  protocol          = &quot;HTTP&quot;<br>  default_action {<br>    type             = &quot;forward&quot;<br>    target_group_arn = aws_lb_target_group.alb_ecs_tg.arn<br>  }<br>}<br><br># Create the ECS Cluster and Fargate launch type service in the private subnets<br>resource &quot;aws_ecs_cluster&quot; &quot;ecs_cluster&quot; {<br>  name  = &quot;demo-ecs-cluster&quot;<br>}<br><br>resource &quot;aws_ecs_service&quot; &quot;demo-ecs-service&quot; {<br>  name            = &quot;demo-ecs-svc&quot;<br>  cluster         = aws_ecs_cluster.ecs_cluster.id<br>  task_definition = aws_ecs_task_definition.ecs_taskdef.arn<br>  desired_count   = 2<br>  deployment_maximum_percent = 200<br>  deployment_minimum_healthy_percent = 50<br>  enable_ecs_managed_tags = false<br>  health_check_grace_period_seconds = 60<br>  launch_type = &quot;FARGATE&quot;<br>  depends_on      = [aws_lb_target_group.alb_ecs_tg, aws_lb_listener.ecs_alb_listener]<br><br>  load_balancer {<br>    target_group_arn = aws_lb_target_group.alb_ecs_tg.arn<br>    container_name   = &quot;web&quot;<br>    container_port   = 80<br>  }<br><br>  network_configuration {<br>    security_groups = [aws_security_group.ecs_security_group.id]<br>    subnets = var.private_subnets<br>  }<br>}<br><br># Create the ECS Service task definition. <br># &#39;nginx&#39; image is being used in the container definition.<br># This image is pulled from the docker hub which is the default image repository.<br># ECS task execution role and the task role is used which can be attached with additional IAM policies to configure the required permissions.<br>resource &quot;aws_ecs_task_definition&quot; &quot;ecs_taskdef&quot; {<br>  family = &quot;service&quot;<br>  container_definitions = jsonencode([<br>    {<br>      name      = &quot;web&quot;<br>      image     = &quot;nginx&quot;<br>      essential = true<br>      portMappings = [<br>        {<br>          containerPort = 80<br>          protocol      = &quot;tcp&quot;<br>        }<br>      ]<br>    }<br>  ])<br>  cpu       = 512<br>  memory    = 1024<br>  execution_role_arn = aws_iam_role.ecs_task_exec_role.arn<br>  task_role_arn = aws_iam_role.ecs_task_role.arn<br>  requires_compatibilities = [&quot;FARGATE&quot;]<br>  network_mode             = &quot;awsvpc&quot;<br>}<br>resource &quot;aws_iam_role&quot; &quot;ecs_task_exec_role&quot; {<br>  name = &quot;ecs_task_exec_role&quot;<br>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action = &quot;sts:AssumeRole&quot;<br>        Effect = &quot;Allow&quot;<br>        Principal = {<br>          Service = &quot;ecs-tasks.amazonaws.com&quot;<br>        }<br>      },<br>    ]<br>  })<br>}<br>resource &quot;aws_iam_role&quot; &quot;ecs_task_role&quot; {<br>  name = &quot;ecs_task_role&quot;<br>  assume_role_policy = jsonencode({<br>    Version = &quot;2012-10-17&quot;<br>    Statement = [<br>      {<br>        Action = &quot;sts:AssumeRole&quot;<br>        Effect = &quot;Allow&quot;<br>        Principal = {<br>          Service = &quot;ecs-tasks.amazonaws.com&quot;<br>        }<br>      },<br>    ]<br>  })<br>}<br><br># Create the VPC Link configured with the private subnets. Security groups are kept empty here, but can be configured as required.<br>resource &quot;aws_apigatewayv2_vpc_link&quot; &quot;vpclink_apigw_to_alb&quot; {<br>  name        = &quot;vpclink_apigw_to_alb&quot;<br>  security_group_ids = []<br>  subnet_ids = var.private_subnets<br>}<br><br># Create the API Gateway HTTP endpoint<br>resource &quot;aws_apigatewayv2_api&quot; &quot;apigw_http_endpoint&quot; {<br>  name          = &quot;serverlessland-pvt-endpoint&quot;<br>  protocol_type = &quot;HTTP&quot;<br>}<br><br># Create the API Gateway HTTP_PROXY integration between the created API and the private load balancer via the VPC Link.<br># Ensure that the &#39;DependsOn&#39; attribute has the VPC Link dependency.<br># This is to ensure that the VPC Link is created successfully before the integration and the API GW routes are created.<br>resource &quot;aws_apigatewayv2_integration&quot; &quot;apigw_integration&quot; {<br>  api_id           = aws_apigatewayv2_api.apigw_http_endpoint.id<br>  integration_type = &quot;HTTP_PROXY&quot;<br>  integration_uri  = aws_lb_listener.ecs_alb_listener.arn<br><br>  integration_method = &quot;ANY&quot;<br>  connection_type    = &quot;VPC_LINK&quot;<br>  connection_id      = aws_apigatewayv2_vpc_link.vpclink_apigw_to_alb.id<br>  payload_format_version = &quot;1.0&quot;<br>  depends_on      = [aws_apigatewayv2_vpc_link.vpclink_apigw_to_alb, <br>                    aws_apigatewayv2_api.apigw_http_endpoint, <br>                    aws_lb_listener.ecs_alb_listener]<br>}<br><br># API GW route with ANY method<br>resource &quot;aws_apigatewayv2_route&quot; &quot;apigw_route&quot; {<br>  api_id    = aws_apigatewayv2_api.apigw_http_endpoint.id<br>  route_key = &quot;ANY /{proxy+}&quot;<br>  target = &quot;integrations/${aws_apigatewayv2_integration.apigw_integration.id}&quot;<br>  depends_on  = [aws_apigatewayv2_integration.apigw_integration]<br>}<br><br># Set a default stage<br>resource &quot;aws_apigatewayv2_stage&quot; &quot;apigw_stage&quot; {<br>  api_id = aws_apigatewayv2_api.apigw_http_endpoint.id<br>  name   = &quot;$default&quot;<br>  auto_deploy = true<br>  depends_on  = [aws_apigatewayv2_api.apigw_http_endpoint]<br>}<br><br># Generated API GW endpoint URL that can be used to access the application running on a private ECS Fargate cluster.<br>output &quot;apigw_endpoint&quot; {<br>  value = aws_apigatewayv2_api.apigw_http_endpoint.api_endpoint<br>    description = &quot;API Gateway Endpoint&quot;<br>}</pre><p>Go ahead and give it a whirl.</p><p>Happy Building!</p><p><strong><em>Note:</em></strong><em> The security group rules and some of the configurations related to the resources created in this sample project may not be production-grade and should be used for reference purposes only.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64ab7aa1b193" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decoding AWS SNS: Your First Step into Serverless Messaging]]></title>
            <link>https://aws.plainenglish.io/decoding-aws-sns-your-first-step-into-serverless-messaging-8c7cc8992be4?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c7cc8992be4</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws-sns]]></category>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Sat, 12 Sep 2026 07:24:05 GMT</pubDate>
            <atom:updated>2026-09-12T07:33:12.796Z</atom:updated>
            <content:encoded><![CDATA[<h3>Amazon Simple Notification Service (SNS)</h3><h3>What is SNS?</h3><p>Amazon SNS is a serverless pub/sub service. It allows you to create topics. Topics have producers and consumers. Producers can publish messages to a topic, and consumers can subscribe to a topic to receive messages. When a message is published to a topic, every consumer will eventually receive it; this is called a fan-out pattern.</p><p>This pattern is powerful because it decouples producers and consumers. Producers do not need to know who will consume their messages, and consumers do not need to know who will produce them. A single action can trigger multiple consumers.</p><p>SNS also enables you to create filters on topics, so that consumers can subscribe to a subset of messages. This allows you to create targeted notifications and to decouple your application even more. For example, a RequestDelivery Lambda function could only be interested in messages related to delivery requests, and not in messages related to payment requests.</p><h3>Let’s build a simple app to demonstrate SNS!</h3><p>Today, we are going to build a very simple app:</p><ul><li>A OrderItem Lambda is triggered by a POST request on a REST API. The user can specify whether he wants to receive a notification, as well as whether he wants to request a delivery for his order.</li><li>The OrderItem Lambda will publish a message to an SNS topic with the order details.</li><li>Downstream, three Lambda functions will be triggered by the SNS topic:</li><li>A ExecuteOrder Lambda, which will simulate a payment.</li><li>A RequestDelivery Lambda, which will simulate a delivery, only if the user requested it.</li><li>A Notification Lambda, which will send a notification to the user, only if he requested it.</li></ul><p>The architecture of our app will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G2ZSi3DgFmVGA5lUwcbs2g.png" /></figure><p>Every action will be simulated by a simple console.logbut with the combined knowledge of this whole series, you can easily replace them with real actions on DynamoDB, S3, SES, or any other AWS service!</p><h3>Create an SNS topic</h3><p>To build this app, we will use the AWS CDK. If you are not familiar with it, I suggest you read the <a href="https://dev.to/kumo/dont-miss-on-the-cloud-revolution-learn-serverless-on-aws-the-right-way-1kac">start of my series</a> where I talk about it in detail. We will create a new CDK project, add the @aws-cdk/aws-sns package to it, and create a new SNS topic in the stack, as well as an API Gateway to trigger our OrderItem Lambda.</p><pre>import * as cdk from &#39;aws-cdk-lib&#39;;<br><br>import path from &#39;path&#39;;<br><br>export class ArticleSNS extends cdk.Stack {<br>  constructor(scope: Construct, id: string) {<br>    super(scope, id);<br><br>    const topic = new cdk.aws_sns.Topic(this, &#39;topic&#39;);<br><br>    const api = new cdk.aws_apigateway.RestApi(this, &#39;api&#39;, {});<br><br>    const orderItem = new cdk.aws_lambda_nodejs.NodejsFunction(this, &#39;OrderItem&#39;, {<br>      entry: path.join(__dirname, &#39;orderItem&#39;, &#39;handler.ts&#39;),<br>      handler: &#39;handler&#39;,<br>      environment: {<br>        TOPIC_ARN: topic.topicArn,<br>      },<br>    });<br>    topic.grantPublish(orderItem);<br>    api.root.addResource(&#39;orderItem&#39;).addMethod(&#39;POST&#39;, new cdk.aws_apigateway.LambdaIntegration(orderItem));<br>  }<br>}</pre><p>Notice that we grant the publish permission to the OrderItem Lambda, so that it can publish messages to the topic. We also pass the topic ARN to the Lambda as an environment variable, so that it can use it to publish messages.</p><h3>Subscribe Lambdas to the SNS topic</h3><p>Now, let’s create the 3 downstream Lambda functions and subscribe them to the topic. We will implement filters on the topic, so that each lambda function only receives the messages it is interested in.</p><pre>// ... previous code<br><br>const executeOrder = new cdk.aws_lambda_nodejs.NodejsFunction(this, &#39;ExecuteOrder&#39;, {<br>  entry: path.join(__dirname, &#39;executeOrder&#39;, &#39;handler.ts&#39;),<br>  handler: &#39;handler&#39;,<br>});<br>topic.addSubscription(new cdk.aws_sns_subscriptions.LambdaSubscription(executeOrder));<br><br>const requestDelivery = new cdk.aws_lambda_nodejs.NodejsFunction(this, &#39;RequestDelivery&#39;, {<br>  entry: path.join(__dirname, &#39;requestDelivery&#39;, &#39;handler.ts&#39;),<br>  handler: &#39;handler&#39;,<br>});<br>topic.addSubscription(<br>  new cdk.aws_sns_subscriptions.LambdaSubscription(requestDelivery, {<br>    filterPolicy: {<br>      // Only triggers when the &quot;requestDelivery&quot; attribute is set to &quot;true&quot;<br>      requestDelivery: cdk.aws_sns.SubscriptionFilter.stringFilter({ allowlist: [&#39;true&#39;] }),<br>    },<br>  }),<br>);<br><br>const sendNotification = new cdk.aws_lambda_nodejs.NodejsFunction(this, &#39;SendNotification&#39;, {<br>  entry: path.join(__dirname, &#39;sendNotification&#39;, &#39;handler.ts&#39;),<br>  handler: &#39;handler&#39;,<br>});<br>topic.addSubscription(<br>  new cdk.aws_sns_subscriptions.LambdaSubscription(sendNotification, {<br>    filterPolicy: {<br>      // Only triggers when the &quot;sendNotification&quot; attribute is set to &quot;true&quot;<br>      sendNotification: cdk.aws_sns.SubscriptionFilter.stringFilter({ allowlist: [&#39;true&#39;] }),<br>    },<br>  }),<br>);</pre><p>See, nothing too complicated. Using the FilterPolicy parameter, we can specify which messages should trigger the lambda function. In our case, we want to trigger the RequestDelivery Lambda only when the requestDelivery attribute is set to true, and the SendNotification Lambda only when the sendNotification attribute is set to true.</p><h3>Publish messages to the SNS topic</h3><p>Finally, the interesting part: time to write the code of the Lambdas! Let’s start with the OrderItem Lambda, which will publish the message to the topic.</p><pre>// orderItem/handler.ts<br>import { PublishCommand, SNSClient } from &#39;@aws-sdk/client-sns&#39;;<br><br>const client = new SNSClient({});<br><br>export const handler = async (event: { body: string }): Promise&lt;{ statusCode: number; body: string }&gt; =&gt; {<br>  const topicArn = process.env.TOPIC_ARN;<br><br>  if (topicArn === undefined) {<br>    throw new Error(&#39;TOPIC_ARN is undefined&#39;);<br>  }<br><br>  const { requestDelivery, sendNotification, item, quantity } = JSON.parse(event.body) as {<br>    requestDelivery?: boolean;<br>    sendNotification?: boolean;<br>    item?: string;<br>    quantity?: number;<br>  };<br><br>  if (requestDelivery === undefined || sendNotification === undefined || item === undefined || quantity === undefined) {<br>    return {<br>      statusCode: 400,<br>      body: &#39;Bad request&#39;,<br>    };<br>  }<br><br>  await client.send(<br>    new PublishCommand({<br>      Message: JSON.stringify({ item, quantity }),<br>      TopicArn: topicArn,<br>      MessageAttributes: {<br>        sendNotification: {<br>          DataType: &#39;String&#39;,<br>          StringValue: sendNotification.toString(),<br>        },<br>        requestDelivery: {<br>          DataType: &#39;String&#39;,<br>          StringValue: requestDelivery.toString(),<br>        },<br>      },<br>    }),<br>  );<br><br>  return {<br>    statusCode: 200,<br>    body: &#39;Item ordered&#39;,<br>  };<br>};</pre><p>This code does three things:</p><ul><li>It gets the topic ARN from the environment variables.</li><li>It parses the body of the request and extracts the requestDelivery, sendNotification, item and quantity attributes.</li><li>It publishes a message to the topic:</li><li>The message body contains the business data, item and quantity.</li><li>The attributes requestDelivery and sendNotification are set to true or false, depending on the request. These attributes are the ones that will be used by the filters we defined earlier.</li></ul><p>For the three downstream Lambdas, we will use console.log to be able to see in the AWS console if they are triggered or not.</p><pre>// executeOrder/handler.ts<br>export const handler = async (event: {<br>  Records: {<br>    Sns: {<br>      Message: string;<br>    };<br>  }[];<br>}): Promise&lt;void&gt; =&gt; {<br>  event.Records.forEach(({ Sns: { Message } }) =&gt; {<br>    const { item, quantity } = JSON.parse(Message) as { item: string; quantity: number };<br><br>    console.log(`ORDER EXECUTED - Item: ${item}, Quantity: ${quantity}`);<br>  });<br>};</pre><p>In this Lambda, I log the content of the messages received from the topic. The interesting part is the type of the event parameter: it is an array of records. This is because SNS can send multiple messages at once, so we need to handle this case.</p><pre>// requestDelivery/handler.ts<br>export const handler = (): Promise&lt;void&gt; =&gt; {<br>  console.log(&#39;DELIVERY REQUESTED&#39;);<br>};</pre><pre>// sendNotification/handler.ts<br>export const handler = (): Promise&lt;void&gt; =&gt; {<br>  console.log(&#39;NOTIFICATION SENT&#39;);<br>};</pre><p>As I already said, if you followed this series from the start, you should be able to replace these console.log statements with real actions, such as updating a DynamoDB or SQL database, sending an email with SES, or anything else!</p><h3>Time to test it!</h3><p>First, let’s deploy the app thanks to the AWS CDK:</p><pre>npm run cdk deploy</pre><p>Then, let’s test it by sending API calls with Postman:</p><p>In the first call, I set sendNotification and requestDelivery to false. Only the ExecuteOrder Lambda is triggered, as expected. We can see the logs in CloudWatch, with the content of the message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kATf37PHooXM-IRMaTGdGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a9OVgApCwdLs-NrsyV-P_Q.png" /></figure><p>In the second call, I set sendNotification to true. The ExecuteOrder and SendNotification Lambdas are triggered. We can see the logs of SendNotification in CloudWatch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kmdPJ4WRAT2Api3ZqyI8tw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nnRO4l-fPm9sYIAaGY5xPQ.png" /></figure><p>In the third call, I set requestDelivery to true. The ExecuteOrder and RequestDelivery Lambdas are triggered. We can see the logs of RequestDelivery in CloudWatch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hqrw2m9Z7X_dZuUcNDzvFA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x7bZ382l47IS22a7SrHVZg.png" /></figure><h3>Conclusion</h3><p>This was a very shallow introduction to SNS. I demonstrated how to create a topic, subscribe Lambdas to it, and publish messages to it. I didn’t code any real side effects, but you should be able to do it by yourself, using the knowledge you acquired in the previous articles of this series.</p><p>SNS also allows you to send emails, SMS, or push notifications to mobile apps, but I didn’t cover this in this article. I will probably write another article about it in the future!</p><p>I plan to continue this series of articles on a bi-monthly basis. I already covered the creation of simple lambda functions and REST APIs, as well as interacting with DynamoDB databases and S3 buckets. I will cover new topics like front-end deployment, type safety, more advanced patterns, and more… If you have any suggestions, do not hesitate to contact me!</p><p>I would really appreciate it if you could react and share this article with your friends and colleagues. It will help me a lot to grow my audience. Also, don’t forget to subscribe to be updated when the next article comes out!</p><p>I often post or repost interesting stuff about AWS and serverless; feel free to follow me!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c7cc8992be4" width="1" height="1" alt=""><hr><p><a href="https://aws.plainenglish.io/decoding-aws-sns-your-first-step-into-serverless-messaging-8c7cc8992be4">Decoding AWS SNS: Your First Step into Serverless Messaging</a> was originally published in <a href="https://aws.plainenglish.io/">AWS in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling Failed Lambda Events Using Async Invocation and Destinations]]></title>
            <link>https://medium.com/@aliusmani2010/handling-failed-lambda-events-using-async-invocation-and-destinations-f32398e4fb4d?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/f32398e4fb4d</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[events]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Sun, 05 Oct 2025 07:14:07 GMT</pubDate>
            <atom:updated>2026-03-19T08:03:25.753Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*QygEXfU9LzpVBgKV4vFdIg.png" /><figcaption>AWS Asynchronous Function Execution Result</figcaption></figure><p>Let’s discuss one of the most effective aspects of AWS Lambda: asynchronous invocation. Lambda functions that are invoked asynchronously receive an immediate 202 status code as Lambda confirms that the request has been accepted; the processing will then be completed as Lambda functions are processed in the background. This arrangement is certainly convenient. However, as will be discussed in more detail in the next section, there are some potential issues.</p><p>Just because Lambda accepted your request doesn’t mean everything will go as planned. You need a means to be aware if the processing succeeded or ran into a snag.</p><p>Let’s say you’re working on an address book app, and a user adds a new contact, then triggers a Lambda function asynchronously to save the new contact information. Lambda returns a success message. But what if something goes wrong in the save operation, like the database being down, or a validation error? You would want to know if the operation fails as well</p><p>Lambda destinations are useful for this purpose. AWS introduced this feature in November 2019, aiming to provide developers with a more graceful way to manage invocation results. You can now flexibly direct the output of your function to other services like another Lambda function, SQS, SNS, or even EventBridge.</p><p>And even more good news! In November 2024, AWS added S3 as a supported destination for failed events. Now, all your failed events can be automatically routed to an S3 bucket. This will greatly help in troubleshooting, as you can review the contents of the bucket to see all the events that failed.</p><p>In this article, I will show you how to configure S3 as your destination for failed events in the case of asynchronous Lambda invocations. Let’s do this. Let’s look at the architecture diagram!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MnxJsLYYTbCbMK6gC854Hw.png" /><figcaption><strong>Review Lambda Function — Hello from Lambda!</strong></figcaption></figure><p><strong>What We’re Going to Build</strong></p><p>We will concentrate on a single objective: demonstrating how Lambda destinations operate, specifically in the case of archiving failed events to an S3 bucket. For the sake of simplicity, we will not construct anything elaborate. We will instead construct a basic Lambda function that performs a single action, which is to print ‘Hello from Lambda!’ when invoked. This will serve as a testing environment for us to examine the destination functionality.</p><p><strong>Getting Started with the AWS Console</strong></p><p>I’ll demonstrate this using the AWS Management Console since it provides the easiest way to visualise the process at each step. Once the concept is clear, you may opt to automate this later using Infrastructure as Code.</p><p>To get started, go to Lambda in your AWS Management Console. You should see a button labelled “Create Function.” Please click this button.</p><p>Next, we will name our function. For this example, I have chosen “HelloWorldGB” (as you can see in the screenshot provided below). You can keep this name or choose a name that is meaningful to you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UEQ5pVpibYtj22E2PadfoA.png" /><figcaption>Create a function named HelloWorldGB.</figcaption></figure><p>For this function, I am using Python Runtime 3.13.</p><p>Example code is shown in the screenshot below.</p><p>This view is from the updated Lambda code editor. If you’re unfamiliar with this console, check out my video that explains how to build a Lambda function using the new editor and SAM templates. The link to the video is in the description.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Th21AUSbh7wq4dsSN8IKBw.png" /><figcaption>Lambda Function</figcaption></figure><p>From the Lambda console, add a destination. This destination will be configured to store events on failure. Note that destinations can be configured for both On Success and On Failure events.<br>For this use case, I am only configuring it for failure scenarios.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPeOeFQX3cBYBsFAxf2NIA.png" /><figcaption><strong>Add a destination—S3 bucket</strong></figcaption></figure><p>In the dropdown menu, you will see various destination options, including:</p><ul><li>SNS</li><li>SQS</li><li>Another Lambda function</li><li>EventBridge</li><li>S3 Bucket</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bel_JRCf102CbpfuhHJEmg.png" /><figcaption>Destination Configuration</figcaption></figure><p><strong>Choosing S3 as Our Destination</strong></p><p>For this demonstration, we will configure an S3 bucket as our chosen destination. It is simple and effective in storing failed events.</p><p>One easily overlooked consideration is that your Lambda function must have the proper permissions to write to that S3 bucket. Otherwise, you will see errors during Lambda execution when saving failed events. Trust me, this is one of the permissions that is worth double-checking, you will save time during troubleshooting.</p><p><strong>Making Our Function Fail (On Purpose)</strong></p><p>The thing about failure destinations is they only kick in when something goes wrong with your lambda function. As our current ‘Hello from Lambda!’ code is running as expected, we will need to make it fail to satisfy our requirement</p><p>We’re going to modify our code so that it throws an error. I know that it sounds odd to intentionally break your function, but this is exactly what we need to trigger the destination mechanism. By forcing an exception, we will see the destination feature in action and observe how it captures and stores those failed events in our S3 bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*IHx6Zy2dlN-m4YVcE832fQ.png" /><figcaption>Lambda Function</figcaption></figure><p>Invoke the Lambda function after making this change. Due to the forced exception, you should see an error message, as shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QIhj60O8Nw2GetRGqvOPew.png" /><figcaption>Execution Function Failed (Checking Logs)</figcaption></figure><p><strong>Review CloudWatch Log</strong></p><p>Next, review the CloudWatch logs. Since the Lambda function has been invoked, a CloudWatch log group will be created. The logs will contain details about the invocation, including the error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v6jlI8AifB-Ubsdxgz1WKA.png" /><figcaption>CloudWatch Logs</figcaption></figure><p><strong>Review Destination S3 Bucket</strong></p><p>Navigate to the S3 bucket that you configured as the destination and check whether the failed event has been posted there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_LKLgRSFo3nZKe2ucyyCYA.png" /></figure><p><strong>Why Isn’t It Working Yet?</strong></p><p>The Lambda function we activated failed, as anticipated, yet there was nothing in our S3 bucket, not even a record of a failed event. What could that be?</p><p>The key point we missed is that Lambda destinations operate exclusively with asynchronous function calls. When testing a function in the console, the function is invoked synchronously, which is why we are not capturing anything in the destination. Many people miss this point.</p><p><strong>Testing with the AWS CLI</strong></p><p>In order to execute testing for this functionality, our Lambda function needs to be invoked asynchronously. There are various real-world scenarios where this can be the case; for example, via EventBridge, S3 events, or other AWS services. However, for our testing needs, the AWS CLI will be the most efficient option.</p><p>Here’s the command I’m using to invoke the function asynchronously:</p><pre>aws lambda invoke \<br>  --function-name arn:aws:lambda:us-east-1:24xxxxxxxx7:function:HelloWorldGB \<br>  --invocation-type Event \<br>  --cli-binary-format raw-in-base64-out \<br>  --payload &#39;{ &quot;name&quot;: &quot;GB&quot; }&#39; \<br>  --region us-east-1 \<br>  response.json</pre><p>Notice the --invocation-type Event parameter? That&#39;s the key part that tells Lambda to process this asynchronously.</p><p><strong>Result:</strong></p><pre>{<br>  &quot;StatusCode&quot;: 202<br>}</pre><p><strong>Let’s Check That S3 Bucket Again</strong></p><p>Excellent! We received a 202 status code, which indicates that Lambda accepted our request for asynchronous processing. Now, let’s go check our S3 bucket and see what happened.</p><p>There it is! Checking the bucket now, I see the failed event is logged. The destination feature has worked flawlessly. This saved event captures what went wrong in great detail, providing us with the comprehensive documentation we need to analyse the failure and address it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gH5V8lqn1wXg40Yl561iYg.png" /><figcaption>- Result-</figcaption></figure><p><strong>Cleaning Up Your AWS Resources</strong></p><p>With the successful testing of the destination feature behind us, it is time to perform some housekeeping. Even if the costs are minor, unused resources can accumulate over time.</p><p>The following resources should be deleted:</p><ul><li>Your created Lambda function</li><li>Your S3 bucket (remember to empty it first, if it has objects inside)</li><li>The CloudWatch log group associated with the function</li><li>Any IAM roles created solely for this demonstration</li></ul><p>Cleaning up your resources now will help avoid unexpected fees in the future</p><p><strong>Wrapping Up</strong></p><p>Definitely! In the previous explanation, we considered the entire procedure <strong>for configuring S3 to capture failed events within Lambda. We used a basic function, deployed the destination configuration, encountered the asynchronous vs synchronous invocation ‘gotcha’, and finally,</strong> validated the configuration via the AWS Command Line Interface.</p><p>Lambda destinations allow you to manage failures more effectively. Instead of lost events vanishing, S3 provides a repository for failed events for examination, automated recovery processes, alerting, and reporting.</p><p>Thank you for your patience during the tutorial. I hope the concepts were clear, and I hope to see this in your future projects. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f32398e4fb4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Beans to Bytes: Creating a Serverless CRUD App with AWS Lambda, API Gateway, and DynamoDB]]></title>
            <link>https://medium.com/@aliusmani2010/from-beans-to-bytes-creating-a-serverless-crud-app-with-aws-lambda-api-gateway-and-dynamodb-2a9119358548?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a9119358548</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[dynamodb]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Mon, 18 Aug 2025 04:02:13 GMT</pubDate>
            <atom:updated>2026-03-19T07:57:34.248Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uCeg5FhB1cQxiu_S3_7gew.png" /><figcaption>AWS ARCHITECTURE DIAGRAM</figcaption></figure><p>This guide will walk you through building a serverless web application for a coffee shop. The owner can sign in securely with AWS Cognito, then manage the inventory on a React frontend through HTTP endpoints — adding, viewing, updating, or removing coffee items. The entire solution is based on <strong>fully managed AWS services, comprising DynamoDB, Lambda, API Gateway, Cognito, S3, and CloudFront</strong>, providing varying degrees of scalability, cost-effectiveness, and security. To use a custom domain, you can integrate CloudFlare and AWS Certificate Manager with the setup for additional features.</p><h3>Architecture at a Glance</h3><p>The app architecture flows as follows:</p><ul><li>The system keeps records of coffee items in DynamoDB containing attributes of ID, name, price, and availability.</li><li>CRUD operations are executed through Lambda Functions, implementing business logic using shared code contained in Lambda Layers when accessing DynamoDB.</li><li>RESTful endpoints that trigger DynamoDB Lambdas are available through the API Gateway.</li><li>The API is secured using JWT authentication.</li><li>The React frontend is integrated with Cognito and is stored in S3, served through CloudFront CDN, which also manages API traffic</li></ul><h4>1. DynamoDB Table Setup:</h4><p>Create a DynamoDB table namedCoffeeShop, using coffeeId as the primary key. Populate some initial entries like:</p><pre>{<br>  &quot;coffeeId&quot;: &quot;c123&quot;,<br>  &quot;name&quot;: &quot;new cold coffee&quot;,<br>  &quot;price&quot;: 456,<br>  &quot;available&quot;: true<br>}</pre><p>This lets you validate connectivity early on.</p><h4>2. IAM Role for Lambda Functions:</h4><p>Create an IAM role called CoffeeShopRole, which grants the required permissions:</p><p><strong>DynamoDB:</strong> Put Item, Delete Item, Get Item, Scan, Update Item</p><p><strong>CloudWatch Logs:</strong> Create Log Group, Create Log Stream, Put Log Events</p><p>This facilitates the enforcement of the principle of least privilege while preserving tracking capabilities.</p><p><strong>Trust policy (</strong><strong>trust-policy.json):</strong></p><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;Service&quot;: &quot;lambda.amazonaws.com&quot; },<br>      &quot;Action&quot;: &quot;sts:AssumeRole&quot;<br>    }<br>  ]<br>}</pre><p><strong>Create the role:</strong></p><pre>aws iam create-role \<br>  --role-name CoffeeShopRole \<br>  --assume-role-policy-document file://trust-policy.json</pre><p><strong>Attach permissions:</strong></p><pre>aws iam put-role-policy \<br>  --role-name CoffeeShopRole \<br>  --policy-name CoffeeShopPolicy \<br>  --policy-document &#39;{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>      { &quot;Effect&quot;: &quot;Allow&quot;, &quot;Action&quot;: [<br>          &quot;dynamodb:PutItem&quot;,<br>          &quot;dynamodb:DeleteItem&quot;,<br>          &quot;dynamodb:GetItem&quot;,<br>          &quot;dynamodb:Scan&quot;,<br>          &quot;dynamodb:UpdateItem&quot;<br>        ], &quot;Resource&quot;: &quot;*&quot; },<br>      { &quot;Effect&quot;: &quot;Allow&quot;, &quot;Action&quot;: [<br>          &quot;logs:CreateLogGroup&quot;,<br>          &quot;logs:CreateLogStream&quot;,<br>          &quot;logs:PutLogEvents&quot;<br>        ], &quot;Resource&quot;: &quot;*&quot; }<br>    ]<br>  }&#39;</pre><h4>3. Lambda Layer and CRUD Function Deployment:</h4><p>Pack the SDK along with the utility code and prepare a Lambda Layer for the DynamoDB SDK. After completing this, prepare four Lambda functions as follows:</p><p>• <strong>getCoffee:</strong> retrieves all items or a specific item, depending on an ID path parameter.</p><p>• <strong>createCoffee:</strong> adds a new item, ensures validation for required fields, and manages duplicates.</p><p>• <strong>updateCoffee:</strong> changes specific attributes (name, price, availability), applies changes only for the provided fields, and addresses missing items tokens.</p><p>• <strong>deleteCoffee:</strong> removes an item by ID and returns an error for a missing item.</p><p><strong>Create Lambda Layer (</strong><strong>utils.mjs):</strong></p><pre>import { DynamoDBClient } from &quot;@aws-sdk/client-dynamodb&quot;;<br>import {<br>  DynamoDBDocumentClient,<br>  ScanCommand,<br>  GetCommand,<br>  PutCommand,<br>  UpdateCommand,<br>  DeleteCommand<br>} from &quot;@aws-sdk/lib-dynamodb&quot;;<br><br>const client = new DynamoDBClient({});<br>const docClient = DynamoDBDocumentClient.from(client);<br><br>const createResponse = (statusCode, body) =&gt; ({<br>  statusCode,<br>  headers: { &quot;Content-Type&quot;: &quot;application/json&quot; },<br>  body: JSON.stringify(body),<br>});<br><br>export {<br>  docClient,<br>  createResponse,<br>  ScanCommand,<br>  GetCommand,<br>  PutCommand,<br>  UpdateCommand,<br>  DeleteCommand,<br>};</pre><p><strong>Example CRUD Lambda:</strong></p><pre>import { docClient, GetCommand, ScanCommand, createResponse } from &#39;/opt/nodejs/utils.mjs&#39;;<br><br>export const getCoffee = async (event) =&gt; {<br>  const id = event.pathParameters?.id;<br>  try {<br>    const command = id<br>      ? new GetCommand({ TableName: &quot;CoffeeShop&quot;, Key: { coffeeId: id } })<br>      : new ScanCommand({ TableName: &quot;CoffeeShop&quot; });<br>    const result = await docClient.send(command);<br>    return createResponse(200, result);<br>  } catch (err) {<br>    return createResponse(500, { error: err.message });<br>  }<br>};</pre><p>Each Lambda imports shared logic from the Layer, ensuring consistency and maintainability.</p><h4>4. API Gateway Configuration:</h4><p>Set up an HTTP API exposing these routes and connecting to the respective Lambda functions:</p><p><strong>Create HTTP API:</strong></p><pre>aws apigatewayv2 create-api \<br>  --name CoffeeShopAPI \<br>  --protocol-type HTTP</pre><p><strong>Add routes and integrate with Lambdas:</strong></p><pre>aws apigatewayv2 create-route \<br>  --api-id &lt;API_ID&gt; \<br>  --route-key &quot;GET /coffee&quot; \<br>  --target integrations/&lt;LAMBDA_INTEGRATION_ID&gt;</pre><ul><li>GET /coffee → fetch all items</li><li>GET /coffee/{id} → fetch specific item</li><li>POST /coffee → create new item</li><li>PUT /coffee/{id} → update item</li><li>DELETE /coffee/{id} → remove item</li></ul><pre>curl https://&lt;API_ID&gt;.execute-api.&lt;region&gt;.amazonaws.com/coffee</pre><p>Test the endpoints using tools like Postman or Thunder Client.</p><h4>5. Cognito &amp; API Authorization:</h4><p><strong>Create a User Pool:</strong></p><pre>aws cognito-idp create-user-pool --pool-name CoffeeShopUsers</pre><p><strong>Set up an App Client:</strong></p><pre>aws cognito-idp create-user-pool-client \<br>  --user-pool-id &lt;POOL_ID&gt; \<br>  --client-name CoffeeShopClient \<br>  --no-generate-secret</pre><p><strong>In API Gateway:</strong></p><p>Create a Cognito User Pool and configure API Gateway to use it as a JWT authorizer. This ensures only authenticated users can access the inventory management endpoints.</p><h4>6. React Frontend Setup</h4><p><strong>Install dependencies:</strong></p><pre>npx create-react-app coffee-frontend<br>cd coffee-frontend<br>npm install amazon-cognito-identity-js axios</pre><p><strong>Example Cognito login:</strong></p><pre>import { CognitoUserPool, CognitoUser, AuthenticationDetails } from &#39;amazon-cognito-identity-js&#39;;<br><br>const poolData = {<br>  UserPoolId: &#39;&lt;USER_POOL_ID&gt;&#39;,<br>  ClientId: &#39;&lt;CLIENT_ID&gt;&#39;,<br>};<br><br>const userPool = new CognitoUserPool(poolData);<br><br>export function login(username, password) {<br>  const authDetails = new AuthenticationDetails({ Username: username, Password: password });<br>  const user = new CognitoUser({ Username: username, Pool: userPool });<br>  user.authenticateUser(authDetails, {<br>    onSuccess: result =&gt; console.log(&#39;JWT Token:&#39;, result.getIdToken().getJwtToken()),<br>    onFailure: err =&gt; console.error(err),<br>  });<br>}</pre><p><strong>Build &amp; upload to S3:</strong></p><pre>npm run build<br>aws s3 sync build/ s3://&lt;your-bucket-name&gt;</pre><p>Build a React application integrated with Cognito for login/auth flows (you can use the GitHub reference provided by the author). Bundle the app and deploy the build output — typically a dist/ folder—to an S3 bucket.</p><h4>7. CloudFront Distribution:</h4><p><strong>Create a distribution with two origins:</strong></p><ul><li><strong>Origin 1:</strong> S3 bucket (React app)</li><li><strong>Origin 2:</strong> API Gateway domain</li></ul><p><strong>Behavior example (for API calls):</strong></p><pre>{<br>  &quot;PathPattern&quot;: &quot;/coffee*&quot;,<br>  &quot;TargetOriginId&quot;: &quot;APIGatewayOrigin&quot;,<br>  &quot;ViewerProtocolPolicy&quot;: &quot;redirect-to-https&quot;<br>}</pre><p>Optionally, use <strong>AWS Certificate Manager</strong> and <strong>Cloudflare DNS</strong> to set up a custom domain.</p><h4>8. Resource Cleanup:</h4><p><strong>Delete resources in reverse order:</strong></p><pre>aws cloudfront delete-distribution --id &lt;DIST_ID&gt;<br>aws s3 rb s3://&lt;bucket-name&gt; --force<br>aws apigatewayv2 delete-api --api-id &lt;API_ID&gt;<br>aws lambda delete-function --function-name &lt;func-name&gt;<br>aws lambda delete-layer-version --layer-name &lt;layer-name&gt; --version-number &lt;ver&gt;<br>aws iam delete-role --role-name CoffeeShopRole<br>aws cognito-idp delete-user-pool --user-pool-id &lt;POOL_ID&gt;</pre><h3>Wrapping Up</h3><p>Using AWS Lambda, API Gateway, DynamoDB, Cognito, and CloudFront, we built a comprehensive serverless CRUD application with no server management, heavy lifting, or maintenance — purely cloud magic.</p><p>The configuration is secure, economical, and easy to scale. You can concentrate on implementing features to solve practical problems when AWS manages scaling and uptime.</p><p>Consider it a solid foundational beginning for any cloud endeavour, whether you are in the prototyping stage or in full production. Also, the best part about it is that you are charged only on a pay-as-you-go basis.</p><p>Now, it is time for you to utilise this archetype and construct something extraordinary.</p><blockquote>From idea to deployment — serverless makes it happen faster than you can say “coffee refill.”<em> ☕</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a9119358548" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Serverless Services]]></title>
            <link>https://blog.devgenius.io/aws-serverless-service-7a2feb903ab7?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a2feb903ab7</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Mon, 16 Sep 2024 10:36:47 GMT</pubDate>
            <atom:updated>2024-09-24T04:05:33.656Z</atom:updated>
            <content:encoded><![CDATA[<p>AWS Serverless services allow you to build and run applications and components on your behalf while you do not provision, control, or manage any servers. You do not have to spend valuable time fixing servers; <strong>AWS</strong> lets you focus on the code alone, given that they care for the infrastructure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4_L-hz4E9Pg4XXPscWgbbA.png" /><figcaption><strong>AWS Serveless Service</strong></figcaption></figure><p><strong>Why is Serverless so popular in modern times?</strong></p><p>The popularity of Serverless in the present age is quite understandable. Serverless has been embraced over time due to its benefits, especially in AWS. Key benefits of using AWS Serverless services include:</p><ul><li><strong>No server management:</strong> There is no need for static provisioning of servers which can be time-consuming and may force one into a large expense bill in server’s management. AWS takes care of all this infrastructure; one needs to write code and deploy it onto the system.</li><li><strong>Cost efficiency:</strong> You only pay for what you consume in terms of computing, storage, and data transfer, not those additional costs incurred in conventional over-the-top server pricing models where you are charged for unused resources, too.</li></ul><p>Serverless services are good for applications that are triggered by events for instance <strong>HTTP requests, changes in the database, or uploads</strong> amongst others. One key is that whenever an event happens, AWS brings into play additional resources and then releases them when the event is complete. This dynamic scaling prevents you from getting the tragedy of the commodity, which was quite typical for traditional infrastructure.</p><p>This will assist us in running the applications without worrying because AWS will have to scale up or down the applications depending on the traffic or workload encountered. This means that our application can handle many users and requests in an efficient manner possibly at the time of heavy traffic.</p><p>For serverless architecture to be implemented AWS has a line of fully managed services that support the creation of serverless applications. These include; <strong>AWS Lambda</strong> for computing, <strong>Amazon S3 for storage</strong>, <strong>Amazon DynamoDB for databases</strong>, and <strong>Amazon API Gateway</strong> to create and develop APIs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*Baaj0DP5WQfEi16NAu96Nw.png" /><figcaption><strong>AWS Lambda</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZpL-yKEINvy1h982uIQgfQ.png" /><figcaption><strong>AWS Lambda</strong></figcaption></figure><p><strong>AWS Lambda</strong> is the most common service for creating applications using serverless computing on AWS.</p><p>To create a serverless application based on Lambda, one can write code in one of the specified languages such as <strong>Java, Go, PowerShell, and Node.</strong> Most widespread programming languages include <strong>JavaScript, C#, Python, and Ruby.</strong> For instance, if you select Python, you will set the code that outlines the function that should run to process the events that happen.</p><p>Once your code is ready, upload it to AWS Lambda through the <strong>AWS Management Console, AWS Command Line Interface, or AWS SDKs.</strong></p><p>Next, you must configure the triggers or events that will allow for the invocation of your Lambda function. These triggers can be invoked from several <strong>AWS services including S3, DynamoDB, API Gateway, SNS, SQS, and others.</strong></p><p>Whenever a trigger event happens, <strong>AWS Lambda</strong> runs your code in a stateless environment and can scale on its own by accommodating the precise number of requests needed. After the execution phase, it retracts to zero, that is how it works.</p><p><strong>Lambda automatically</strong> scales your application, running your code in response to events, from just a few requests a day to thousands per second — no intervention required.</p><p>For Lambda, it works similarly to the <strong>serverless environment</strong> where you are charged for the number of milliseconds your code is in execution. There are <strong>no initial one-time charges or monthly minimums</strong>, and it is not billed when your application is not in active use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a2feb903ab7" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/aws-serverless-service-7a2feb903ab7">AWS Serverless Services</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Creating your first Lambda function using the command line]]]></title>
            <link>https://blog.devgenius.io/active-record-select-vs-pluck-1247a2193242?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/1247a2193242</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[software]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Fri, 15 Sep 2023 06:11:29 GMT</pubDate>
            <atom:updated>2024-12-31T17:47:19.862Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*RtLHsiEKna5IwIFuVMzxTQ.png" /><figcaption>- AWS Lambda -</figcaption></figure><p>The concept of APIs has become very common, especially when it comes to systems and integration. As a developer, you will be required to design APIs to enable you to interact with systems and integration. In the past, this meant much work regarding establishing the environment and deploying the code to an on-premise or even <strong>Amazon EC2</strong>.</p><p>But this method is very slow and is equally very expensive.</p><p>Therefore, the notion known as serverless computing has appeared rather recently.</p><p>An example of a serverless service is <a href="https://aws.amazon.com/lambda/"><strong>AWS Lambda</strong></a> or in simpler terms a function as a service. Lambda resembles an API end-point in the sense that you invoke a single function, assuming you can call that function.</p><p>This service runs code only when required, and scales the requests per day depending on need, automatically. It is useful where it is paired with API gateways to build the best solution.</p><p><a href="https://aws.amazon.com/api-gateway/"><strong>AWS API Gateway</strong></a> itself is a service that enables developers to create, deploy, monitor, and update your APIs at scale.</p><p>When you used it together with a Lambda function, you now had what is called a <strong>FaaS</strong> which stands for <strong>Function as a Service</strong>. To this end, it allows developers of applications to remove the current time-consuming and expensive back-end development technique and adopt the serverless option.</p><p><strong>Benefits of Using Lambda:</strong></p><p>Let us look at some of the merits of using serverless technologies.</p><p>Integration testing and dealing with the systematic complexities is also facilitated at the development level by <strong>Lambda</strong>. In other words, Lambda allows engineers to code with total abstraction of the infrastructure because it takes care of <strong>scaling</strong>, <strong>logging</strong>, <strong>error identification</strong>, and <strong>authentication</strong>.</p><p>Some of the most essential benefits of Lambda are as follows:</p><p><strong>Built-in logging:</strong></p><p>Have you ever built a certain piece of infrastructure where you looked at and thought you have to spend a reasonable amount of time implementing logging and error tracking?</p><p>Well, Lambda does all that for you. There is a lot of flexibility available regarding viewing the log file, and all your logs can be found in the <strong>AWS Management Console.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*G_FZEDXOmbgzWEfoNuPj_Q.png" /><figcaption>- Logging for test function -</figcaption></figure><p><strong>Scalability:</strong></p><p>Also worthy of note is the fact that <strong>AWS Lambda auto-scales your application.</strong></p><p>When responding to each connection, your code executes, and when it does, your application processes each connection differently, side by side with the running code. This way the Inserts are scaled exactly with the size of the total workload of the data-flow processing.</p><p><strong>Introduction to building your First AWS Lambda Function with API Gateway:</strong></p><p>Having listed all the advantages, let me explain how to create an <strong>AWS Lambda function.</strong></p><p>Let us begin with this function h which I assume you have encountered earlier. This is the simple functional style in the learning programming language <strong>“Hello World”</strong>. You will also realize that we are using <strong>Python</strong> — especially when it comes to adjusting your parameters in the future.</p><p>We would like to move this basic function onto <strong>AWS Lambda</strong> as well. Unlike most beginner’s programs that may use a simple ‘Hello World Function’ that requires only a single argument which will be a reference to an ‘event’. This will contain the data we will be passing in our <strong>HTTP request Data.</strong></p><p>This data will then be parsed and will pull out any parameters in that body request and set them as whatever we need. Here, it is possible to find only one variable — the <strong>“name”</strong>.</p><p>Which we will use to send back <strong>“Hello”</strong> + name and a <strong>status code of 200.</strong> This will represent when the function is ending in success.</p><h3>Step 1: Python code</h3><pre>import json<br><br>def lambda_test_function(event, context):<br>    body={&#39;message&#39;:&#39;OK&#39;}<br>    params = event[&#39;eventStringParameters&#39;]<br><br>    name =str(params[&#39;name&#39;])<br>    helloString = &quot;Hello &quot; + name<br>    body[&#39;helloString&#39;] = helloString<br><br>    response={<br>        &#39;StatusCode&#39;:200,<br>        &#39;body&#39;:json.dumps(body),<br>    }<br><br>    return response</pre><h3>Step 2: Serverless.yml file</h3><p>To make it as easy as possible to push this function to Lambda using the command line, I also created a file called serverless.yml.</p><p>This file will contain the parameters including which programming language to use when the code is run, the <strong>geographical location</strong> of the user, academic achievements, the actual function, and the parameters that the function will require.</p><p>It is also helpful in configuring the <strong>Lambda function</strong> in a way that it can be later easily pushed to the cloud with the help of the command line.</p><p>The file below is the one that will be used for all diagrams:</p><pre>service: lambda<br>provider:<br>  name: aws<br>  stage: dev<br>  runtime: python3.6<br>  region: us-east-2<br>  credentials:<br>    accessKeyId: YOUR KEY HERE <br>    secretAccessKey: YOUR SECRET HERE<br><br>functions:<br>  test_function:<br>    handler: handler.lambda_test_function<br>    memorySize: 512<br>    timeout: 60<br>    events:<br>      - http:<br>          path: get-hello<br>          method: get<br>          request:<br>            parameters:<br>              queryStrings:<br>                name: true<br><br><br>plugins:<br>  - serverless-python-requirements<br><br>custom:<br>  pythonRequirements:<br>    dockerizePip: true<br>    slim: true</pre><h3>Step 3: Install serverless</h3><p>The next step in deploying this function to Lambda is you use <a href="https://www.npmjs.com/package/serverless-lambda?activeTab=readme"><strong>npm install serverless</strong></a>, since having these only gives access to the <strong>sls</strong> command.</p><p>The commands below will enable you to install this service.</p><pre>npm install -g serverless<br>npm i -D serverless-dotenv-plugin<br>sls plugin install -n serverless-python-requirements</pre><p>In case you have confirmed that only the files which you want to deploy are in a folder, in the terminal you can type the following command.</p><pre>sudo sls deploy</pre><p>After this, sls will deploy your code to AWS again. After that, it should be available directly to your <strong>AWS management console</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3nwcoM_2oWRIa92Ni2XjpA.png" /><figcaption>- Congrats! You have created your first serverless function -</figcaption></figure><h3>Step 5: Testing your function</h3><p>In this case, we shall test this function using a <strong>JSON file</strong> as the parameters of the function. This will represent a fundamental HTTP request that clients need to make in order to regain control from rogue APIs.</p><p>Here is a file needed for the task, we previously named it <strong>event.json</strong>. Again, you can see that it is defined as a function with only a “name” argument, but in the future, you might complicate, your serverless functions, and so have more arguments.</p><p>After you have this JSON file setup, then you can use the command below to test the function in the serverless function.</p><pre>sudo sls invoke --- function predict-price --- path event.json</pre><p>When you run this function, you will produce an output something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*MoIHGFJp66KARjlD3-ZiHg.png" /></figure><p>This is the response you would get back when deploying this serverless function in infrastructure.</p><p>That is it, you have successfully deployed and run your initial Lambda function by the command line interface.</p><p><strong>CONCLUSION:</strong></p><p><strong>AWS Lambda</strong> is an <strong>elastic</strong>, <strong>event-based</strong>, <strong>serverless computing</strong> providing a recommending function that allows developers to build functions. This can be used by software developers to build end-points and has also been used very extensively by data engineers to build data converters between AWS services like <a href="https://aws.amazon.com/s3/"><strong>S3</strong></a>, <a href="https://aws.amazon.com/kinesis/"><strong>Kinesis</strong></a>, and <a href="https://aws.amazon.com/redshift/"><strong>Redshift</strong></a>.</p><p>This tells you that as Lambda is enabling the reduction of the time it takes to build infrastructure, the usage of Lambda is becoming popular each day. Not to mention the expensive cost of <strong>EC2</strong>, being able to only pay per the call of a function makes Lambda an awesome solution.</p><p>Overall, this <strong>FaaS (Function as a Service)</strong> is good for developers and can vacuum your development cycle.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1247a2193242" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/active-record-select-vs-pluck-1247a2193242">[Creating your first Lambda function using the command line]</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PostgreSQL Master-Slave Database Replication]]></title>
            <link>https://levelup.gitconnected.com/postgresql-master-slave-database-replication-a845777901ab?source=rss-3a9a92f9d194------2</link>
            <guid isPermaLink="false">https://medium.com/p/a845777901ab</guid>
            <category><![CDATA[master-slave]]></category>
            <category><![CDATA[database-administration]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Ali Usmani]]></dc:creator>
            <pubDate>Mon, 30 Jan 2023 14:22:50 GMT</pubDate>
            <atom:updated>2025-01-07T17:30:38.405Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*oCbobAbmY71K5yYqrLvXdA.png" /><figcaption><a href="https://github.com/jhunt/pgrouter">https://github.com/jhunt/pgrouter</a></figcaption></figure><h4><strong>Why do we need master-slave relations in PostgreSQL?</strong></h4><p>The master-slave replication in PostgreSQL is used to improve the reliability and scalability of database systems. The main purposes are:</p><ol><li>High Availability: By having a slave database, it can take over in case the master database fails.</li><li>Load Balancing: The read requests can be sent to the slave database to reduce the load on the master database.</li><li>Data Backup: The slave database can be used as a backup in case the master database is lost or corrupted.</li><li>Data Analysis: The slave database can be used to run analytics or reports without affecting the performance of the master database.</li></ol><p>Overall, the master-slave replication in PostgreSQL helps to ensure that the data is available, reliable, and scalable for a variety of use cases.</p><h4><strong>Networking of Machine</strong></h4><p>To reduce latency, I connect all of my devices to the same network.</p><pre>Application Server        -  172.16.10.191<br>Master PostgreSQL Server  -  172.16.10.220<br>Slave  PostgreSQL Server  -  172.16.10.119</pre><h4><strong>PostgreSQL Installation Follow</strong></h4><p>We will install PostgreSQL on both the master and slave servers. For this, I’m using PostgreSQL 12. Follow the same installation methods as listed below for both systems (master and slave).</p><pre>sudo apt -y update<br>sudo apt -y install nano wget</pre><p>We must import the GPG key and install the PostgreSQL 12 repository on our Ubuntu machine. To accomplish this, use the commands listed below;</p><pre>wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -</pre><p>Add the repository contents to your Ubuntu <strong>18.04 or 16.04</strong> system after importing the GPG key.</p><pre>echo &quot;deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main&quot; |sudo tee  /etc/apt/sources.list.d/pgdg.list</pre><p>After successfully adding the repository, update the package list and install PostgreSQL 12 server and client packages on your Ubuntu 18.04 / Ubuntu 16.04 Linux system.</p><pre>sudo apt update<br>sudo apt -y install postgresql-12 postgresql-client-12</pre><p>It will set up the PostgreSQL server and client on the machine. Following the start of service. We’ll see if the service is up and operating. After each system reboot, the PostgreSQL service should start.</p><pre>sudo /etc/init.d/postgresql start<br>sudo /etc/init.d/postgresql status<br>sudo systemctl is-enabled postgresql<br>               OR<br>sudo service postgresql start<br>sudo service postgresql status<br>sudo systemctl is-enabled postgresql</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*dgZMHkNqa0WhtBMbRKCeZA.png" /><figcaption>We are starting the PostgreSQL service, verifying the status of the service, and enabling service startup on reboot.</figcaption></figure><h4><strong>Configuring Master Database (172.16.10.220)</strong></h4><p>We will establish a database user called “replication” who will perform the replication task.</p><pre>sudo -i -u postgres<br>      OR<br>su postgres<br>psql</pre><p>This will launch the psql software, from which you can create a new user.</p><pre>postgres=# CREATE USER replication REPLICATION LOGIN CONNECTION LIMIT 1 ENCRYPTED PASSWORD &#39;replicationpa55word&#39;;</pre><p>Please take a look at how I used to check the list of roles after creating the user.</p><pre>\du</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TMi0dnJyW8Zm0huBwuQ6HA.png" /><figcaption>Checking the List of roles using \du</figcaption></figure><p>By performing the following command on the psql client, we can change the maximum number of connections allowed to the replication user.</p><pre>postgres=# ALTER ROLE replication CONNECTION LIMIT -1;</pre><p>This is now a stand-alone database. We need to update a certain configuration to make it the master of a database cluster. Add the following lines to the file ‘<strong>/etc/postgresql/12/main/postgresql.conf</strong>’.</p><pre>listen_addresses = &#39;localhost,172.16.10.220&#39;<br>wal_level = replica<br>max_wal_senders = 10<br>wal_keep_segments = 64</pre><p>Note: Please replace &quot;<strong>172.16.10.220</strong>&quot; with the IP address of your master database.</p><p>For replication, the slave server requires authentication. Now add the following line to the file ‘<strong>/etc/postgresql/12/main/pg_hba.conf</strong>’</p><pre>host    replication     replication     172.16.10.119/0   md5</pre><p>Restart the PostgreSQL service and verify that it has started.</p><pre>sudo /etc/init.d/postgresql restart<br>sudo /etc/init.d/postgresql status<br>                 OR<br>sudo service postgresql restart<br>sudo service postgresql status</pre><p>We have completed the master server configuration, and our master server is now ready for replication.</p><h4><strong>Configuring Slave Database (172.16.10.119)</strong></h4><p>First, we must stop the PostgreSQL service on the slave server.</p><pre>sudo /etc/init.d/postgresql stop<br>sudo /etc/init.d/postgresql status<br>                 OR<br>sudo service postgresql stop<br>sudo service postgresql status</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NWzOM7-d2cBcFAGBQk8qow.png" /></figure><p>Append the following lines to the file of “<strong><em>/etc/postgresql/12/main/postgresql.conf”</em></strong></p><pre>listen_addresses = &#39;localhost,172.16.10.119&#39;<br>wal_level = replica<br>max_wal_senders = 10<br>wal_keep_segments = 64<br>hot_standby = on</pre><p>Please replace <strong>“172.16.10.119&quot;</strong> with the IP address of your slave database.</p><p>Now append the following line to the file <strong>“/etc/postgresql/12/main/pg_hba.conf”</strong></p><pre>host    replication     replication     172.16.10.220/0   md5</pre><p>We should delete all PostgreSQL data directory files and directories.</p><pre>cd /var/lib/postgresql/12/main/<br>sudo rm -rfv *<br>            OR<br>sudo rm -rfv * cd /var/lib/postgresql/12/main/</pre><p>Now, run the following command to copy all data from the master database to the slave database.</p><pre>sudo su postgres<br>pg_basebackup -h 172.16.10.220 -U replication -p 5432 -D /var/lib/postgresql/12/main/  -Fp -Xs -P -R</pre><p>While running the command, replace <strong>“172.16.10.220”</strong> with your master’s IP address.</p><p>It will prompt for a password for the <strong>“<em>replication”</em></strong> PostgreSQL user, which is in our case <strong>“replicationpa55word”</strong>.</p><p>After it finishes the fetching, start the PostgreSQL service.</p><pre>sudo /etc/init.d/postgresql start<br>               OR<br>sudo service postgresql start<br>sudo service postgresql status</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*dgZMHkNqa0WhtBMbRKCeZA.png" /></figure><p><strong>Congratulations</strong> on the successful replication of your database.</p><h4><strong>What happens next after a successful master-slave relationship?</strong></h4><ul><li>Using PgBouncer for connection pooling.</li><li>PostgreSQL failover implementation.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a845777901ab" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/postgresql-master-slave-database-replication-a845777901ab">PostgreSQL Master-Slave Database Replication</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>