{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "The AWS CloudFormation template for this Serverless application",
  "Resources": {
    "ServerlessDeploymentBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketEncryption": {
          "ServerSideEncryptionConfiguration": [
            {
              "ServerSideEncryptionByDefault": {
                "SSEAlgorithm": "AES256"
              }
            }
          ]
        }
      }
    },
    "ServerlessDeploymentBucketPolicy": {
      "Type": "AWS::S3::BucketPolicy",
      "Properties": {
        "Bucket": {
          "Ref": "ServerlessDeploymentBucket"
        },
        "PolicyDocument": {
          "Statement": [
            {
              "Action": "s3:*",
              "Effect": "Deny",
              "Principal": "*",
              "Resource": [
                {
                  "Fn::Join": [
                    "",
                    [
                      "arn:",
                      {
                        "Ref": "AWS::Partition"
                      },
                      ":s3:::",
                      {
                        "Ref": "ServerlessDeploymentBucket"
                      },
                      "/*"
                    ]
                  ]
                },
                {
                  "Fn::Join": [
                    "",
                    [
                      "arn:",
                      {
                        "Ref": "AWS::Partition"
                      },
                      ":s3:::",
                      {
                        "Ref": "ServerlessDeploymentBucket"
                      }
                    ]
                  ]
                }
              ],
              "Condition": {
                "Bool": {
                  "aws:SecureTransport": false
                }
              }
            }
          ]
        }
      }
    },
    "AlbEventLogGroup": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": "/aws/lambda/serverless-test-project-alb-dev-albEvent"
      }
    },
    "IamRoleLambdaExecution": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Policies": [
          {
            "PolicyName": {
              "Fn::Join": [
                "-",
                [
                  "serverless-test-project-alb",
                  "dev",
                  "lambda"
                ]
              ]
            },
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "logs:CreateLogStream",
                    "logs:CreateLogGroup"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/serverless-test-project-alb-dev*:*"
                    }
                  ]
                },
                {
                  "Effect": "Allow",
                  "Action": [
                    "logs:PutLogEvents"
                  ],
                  "Resource": [
                    {
                      "Fn::Sub": "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/serverless-test-project-alb-dev*:*:*"
                    }
                  ]
                }
              ]
            }
          }
        ],
        "Path": "/",
        "RoleName": {
          "Fn::Join": [
            "-",
            [
              "serverless-test-project-alb",
              "dev",
              {
                "Ref": "AWS::Region"
              },
              "lambdaRole"
            ]
          ]
        }
      }
    },
    "AlbEventLambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "S3Bucket": {
            "Ref": "ServerlessDeploymentBucket"
          },
          "S3Key": "serverless/serverless-test-project-alb/dev/1664186319340-2022-09-26T09:58:39.340Z/serverless-test-project-alb.zip"
        },
        "Handler": "alb-handler.handleALB",
        "Runtime": "nodejs24.x",
        "FunctionName": "serverless-test-project-alb-dev-albEvent",
        "MemorySize": 1024,
        "Timeout": 6,
        "Role": {
          "Fn::GetAtt": [
            "IamRoleLambdaExecution",
            "Arn"
          ]
        }
      },
      "DependsOn": [
        "AlbEventLogGroup"
      ]
    },
    "AlbEventLambdaVersionWRrrxcUYvPJyfakOMlSIkRmwir9Tdqe6ehWwNlLFde8": {
      "Type": "AWS::Lambda::Version",
      "DeletionPolicy": "Retain",
      "Properties": {
        "FunctionName": {
          "Ref": "AlbEventLambdaFunction"
        },
        "CodeSha256": "00o/styhAYYVW99EROYyclIdflCXdVnDtBI+GmzoUus="
      }
    },
    "AlbEventAlbTargetGrouphttpListener": {
      "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties": {
        "TargetType": "lambda",
        "Targets": [
          {
            "Id": {
              "Fn::GetAtt": [
                "AlbEventLambdaFunction",
                "Arn"
              ]
            }
          }
        ],
        "Name": "1d5fdfd5099ec257209ef7b7c5ee8cb4",
        "Tags": [
          {
            "Key": "Name",
            "Value": "serverless-test-project-alb-albEvent-httpListener-dev"
          }
        ],
        "TargetGroupAttributes": [
          {
            "Key": "lambda.multi_value_headers.enabled",
            "Value": false
          }
        ],
        "HealthCheckEnabled": false
      },
      "DependsOn": [
        "AlbEventLambdaPermissionRegisterTarget"
      ]
    },
    "AlbEventAlbListenerRule1": {
      "Type": "AWS::ElasticLoadBalancingV2::ListenerRule",
      "Properties": {
        "Actions": [
          {
            "Type": "forward",
            "TargetGroupArn": {
              "Ref": "AlbEventAlbTargetGrouphttpListener"
            }
          }
        ],
        "Conditions": [
          {
            "Field": "path-pattern",
            "Values": [
              "/handleALB"
            ]
          },
          {
            "Field": "http-request-method",
            "HttpRequestMethodConfig": {
              "Values": [
                "POST"
              ]
            }
          }
        ],
        "ListenerArn": {
          "Ref": "httpListener"
        },
        "Priority": 1
      }
    },
    "AlbEventLambdaPermissionAlb": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": [
            "AlbEventLambdaFunction",
            "Arn"
          ]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "elasticloadbalancing.amazonaws.com",
        "SourceArn": {
          "Ref": "AlbEventAlbTargetGrouphttpListener"
        }
      }
    },
    "AlbEventLambdaPermissionRegisterTarget": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": [
            "AlbEventLambdaFunction",
            "Arn"
          ]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "elasticloadbalancing.amazonaws.com"
      }
    },
    "bucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "awesome-savage-bucket1"
      }
    },
    "vpcALB": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/20",
        "EnableDnsSupport": true,
        "EnableDnsHostnames": true,
        "InstanceTenancy": "default",
        "Tags": [
          {
            "Key": "ProjectName",
            "Value": "serverless-test-project-alb"
          },
          {
            "Key": "Stage",
            "Value": "dev"
          }
        ]
      }
    },
    "internetGateway": {
      "Type": "AWS::EC2::InternetGateway",
      "Properties": {
        "Tags": [
          {
            "Key": "ProjectName",
            "Value": "serverless-test-project-alb"
          },
          {
            "Key": "Stage",
            "Value": "dev"
          }
        ]
      }
    },
    "vpcGatewayAttachment": {
      "Type": "AWS::EC2::VPCGatewayAttachment",
      "Properties": {
        "VpcId": {
          "Ref": "vpcALB"
        },
        "InternetGatewayId": {
          "Ref": "internetGateway"
        }
      }
    },
    "subnetA": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": "eu-west-1a",
        "CidrBlock": "10.0.5.0/24",
        "VpcId": {
          "Ref": "vpcALB"
        },
        "Tags": [
          {
            "Key": "ProjectName",
            "Value": "serverless-test-project-alb"
          },
          {
            "Key": "Stage",
            "Value": "dev"
          }
        ]
      }
    },
    "subnetB": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": "eu-west-1b",
        "CidrBlock": "10.0.6.0/24",
        "VpcId": {
          "Ref": "vpcALB"
        },
        "Tags": [
          {
            "Key": "ProjectName",
            "Value": "serverless-test-project-alb"
          },
          {
            "Key": "Stage",
            "Value": "dev"
          }
        ]
      }
    },
    "routeTableA": {
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {
          "Ref": "vpcALB"
        },
        "Tags": [
          {
            "Key": "ProjectName",
            "Value": "serverless-test-project-alb"
          },
          {
            "Key": "Stage",
            "Value": "dev"
          }
        ]
      }
    },
    "routeTableB": {
      "Type": "AWS::EC2::RouteTable",
      "Properties": {
        "VpcId": {
          "Ref": "vpcALB"
        },
        "Tags": [
          {
            "Key": "ProjectName",
            "Value": "serverless-test-project-alb"
          },
          {
            "Key": "Stage",
            "Value": "dev"
          }
        ]
      }
    },
    "routeTableAssociationSubnetA": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "SubnetId": {
          "Ref": "subnetA"
        },
        "RouteTableId": {
          "Ref": "routeTableA"
        }
      }
    },
    "routeTableAssociationSubnetB": {
      "Type": "AWS::EC2::SubnetRouteTableAssociation",
      "Properties": {
        "SubnetId": {
          "Ref": "subnetB"
        },
        "RouteTableId": {
          "Ref": "routeTableB"
        }
      }
    },
    "routeTableAInternetRoute": {
      "Type": "AWS::EC2::Route",
      "DependsOn": [
        "vpcGatewayAttachment"
      ],
      "Properties": {
        "RouteTableId": {
          "Ref": "routeTableA"
        },
        "DestinationCidrBlock": "0.0.0.0/0",
        "GatewayId": {
          "Ref": "internetGateway"
        }
      }
    },
    "routeTableBInternetRoute": {
      "Type": "AWS::EC2::Route",
      "DependsOn": [
        "vpcGatewayAttachment"
      ],
      "Properties": {
        "RouteTableId": {
          "Ref": "routeTableB"
        },
        "DestinationCidrBlock": "0.0.0.0/0",
        "GatewayId": {
          "Ref": "internetGateway"
        }
      }
    },
    "alb": {
      "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
      "Properties": {
        "Name": "awesome-loadBalancer",
        "Type": "application",
        "Subnets": [
          {
            "Ref": "subnetA"
          },
          {
            "Ref": "subnetB"
          }
        ],
        "SecurityGroups": [
          {
            "Ref": "albSecurityGroup"
          }
        ]
      }
    },
    "httpListener": {
      "Type": "AWS::ElasticLoadBalancingV2::Listener",
      "Properties": {
        "LoadBalancerArn": {
          "Ref": "alb"
        },
        "Port": 80,
        "Protocol": "HTTP",
        "DefaultActions": [
          {
            "Type": "redirect",
            "RedirectConfig": {
              "Protocol": "HTTP",
              "Port": 400,
              "Host": "#{host}",
              "Path": "/#{path}",
              "Query": "#{query}",
              "StatusCode": "HTTP_301"
            }
          }
        ]
      }
    },
    "albSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Allow http to client host",
        "VpcId": {
          "Ref": "vpcALB"
        },
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": 80,
            "ToPort": 80,
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": 443,
            "ToPort": 443,
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    }
  },
  "Outputs": {
    "ServerlessDeploymentBucketName": {
      "Value": {
        "Ref": "ServerlessDeploymentBucket"
      },
      "Export": {
        "Name": "sls-serverless-test-project-alb-dev-ServerlessDeploymentBucketName"
      }
    },
    "AlbEventLambdaFunctionQualifiedArn": {
      "Description": "Current Lambda function version",
      "Value": {
        "Ref": "AlbEventLambdaVersionWRrrxcUYvPJyfakOMlSIkRmwir9Tdqe6ehWwNlLFde8"
      },
      "Export": {
        "Name": "sls-serverless-test-project-alb-dev-AlbEventLambdaFunctionQualifiedArn"
      }
    }
  }
}