|
| 1 | +AWSTemplateFormatVersion: '2010-09-09' |
| 2 | +Transform: AWS::Serverless-2016-10-31 |
| 3 | +Description: Multi-source API Lambda function with ALB and API Gateway V2 |
| 4 | + |
| 5 | +Resources: |
| 6 | + MultiSourceAPIFunction: |
| 7 | + Type: AWS::Serverless::Function |
| 8 | + Properties: |
| 9 | + CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MultiSourceAPI/MultiSourceAPI.zip |
| 10 | + Handler: provided |
| 11 | + Runtime: provided.al2 |
| 12 | + Architectures: |
| 13 | + - arm64 |
| 14 | + MemorySize: 256 |
| 15 | + Timeout: 30 |
| 16 | + Environment: |
| 17 | + Variables: |
| 18 | + LOG_LEVEL: trace |
| 19 | + Events: |
| 20 | + ApiGatewayEvent: |
| 21 | + Type: HttpApi |
| 22 | + Properties: |
| 23 | + Path: /{proxy+} |
| 24 | + Method: ANY |
| 25 | + |
| 26 | + # VPC for ALB |
| 27 | + VPC: |
| 28 | + Type: AWS::EC2::VPC |
| 29 | + Properties: |
| 30 | + CidrBlock: 10.0.0.0/16 |
| 31 | + EnableDnsHostnames: true |
| 32 | + EnableDnsSupport: true |
| 33 | + |
| 34 | + PublicSubnet1: |
| 35 | + Type: AWS::EC2::Subnet |
| 36 | + Properties: |
| 37 | + VpcId: !Ref VPC |
| 38 | + CidrBlock: 10.0.1.0/24 |
| 39 | + AvailabilityZone: !Select [0, !GetAZs ''] |
| 40 | + MapPublicIpOnLaunch: true |
| 41 | + |
| 42 | + PublicSubnet2: |
| 43 | + Type: AWS::EC2::Subnet |
| 44 | + Properties: |
| 45 | + VpcId: !Ref VPC |
| 46 | + CidrBlock: 10.0.2.0/24 |
| 47 | + AvailabilityZone: !Select [1, !GetAZs ''] |
| 48 | + MapPublicIpOnLaunch: true |
| 49 | + |
| 50 | + InternetGateway: |
| 51 | + Type: AWS::EC2::InternetGateway |
| 52 | + |
| 53 | + AttachGateway: |
| 54 | + Type: AWS::EC2::VPCGatewayAttachment |
| 55 | + Properties: |
| 56 | + VpcId: !Ref VPC |
| 57 | + InternetGatewayId: !Ref InternetGateway |
| 58 | + |
| 59 | + RouteTable: |
| 60 | + Type: AWS::EC2::RouteTable |
| 61 | + Properties: |
| 62 | + VpcId: !Ref VPC |
| 63 | + |
| 64 | + Route: |
| 65 | + Type: AWS::EC2::Route |
| 66 | + DependsOn: AttachGateway |
| 67 | + Properties: |
| 68 | + RouteTableId: !Ref RouteTable |
| 69 | + DestinationCidrBlock: 0.0.0.0/0 |
| 70 | + GatewayId: !Ref InternetGateway |
| 71 | + |
| 72 | + SubnetRouteTableAssociation1: |
| 73 | + Type: AWS::EC2::SubnetRouteTableAssociation |
| 74 | + Properties: |
| 75 | + SubnetId: !Ref PublicSubnet1 |
| 76 | + RouteTableId: !Ref RouteTable |
| 77 | + |
| 78 | + SubnetRouteTableAssociation2: |
| 79 | + Type: AWS::EC2::SubnetRouteTableAssociation |
| 80 | + Properties: |
| 81 | + SubnetId: !Ref PublicSubnet2 |
| 82 | + RouteTableId: !Ref RouteTable |
| 83 | + |
| 84 | + # Application Load Balancer |
| 85 | + ALBSecurityGroup: |
| 86 | + Type: AWS::EC2::SecurityGroup |
| 87 | + Properties: |
| 88 | + GroupDescription: Security group for ALB |
| 89 | + VpcId: !Ref VPC |
| 90 | + SecurityGroupIngress: |
| 91 | + - IpProtocol: tcp |
| 92 | + FromPort: 80 |
| 93 | + ToPort: 80 |
| 94 | + CidrIp: 0.0.0.0/0 |
| 95 | + |
| 96 | + ApplicationLoadBalancer: |
| 97 | + Type: AWS::ElasticLoadBalancingV2::LoadBalancer |
| 98 | + Properties: |
| 99 | + Scheme: internet-facing |
| 100 | + Subnets: |
| 101 | + - !Ref PublicSubnet1 |
| 102 | + - !Ref PublicSubnet2 |
| 103 | + SecurityGroups: |
| 104 | + - !Ref ALBSecurityGroup |
| 105 | + |
| 106 | + ALBTargetGroup: |
| 107 | + Type: AWS::ElasticLoadBalancingV2::TargetGroup |
| 108 | + DependsOn: ALBLambdaInvokePermission |
| 109 | + Properties: |
| 110 | + TargetType: lambda |
| 111 | + Targets: |
| 112 | + - Id: !GetAtt MultiSourceAPIFunction.Arn |
| 113 | + |
| 114 | + ALBListener: |
| 115 | + Type: AWS::ElasticLoadBalancingV2::Listener |
| 116 | + Properties: |
| 117 | + LoadBalancerArn: !Ref ApplicationLoadBalancer |
| 118 | + Port: 80 |
| 119 | + Protocol: HTTP |
| 120 | + DefaultActions: |
| 121 | + - Type: forward |
| 122 | + TargetGroupArn: !Ref ALBTargetGroup |
| 123 | + |
| 124 | + ALBLambdaInvokePermission: |
| 125 | + Type: AWS::Lambda::Permission |
| 126 | + Properties: |
| 127 | + FunctionName: !GetAtt MultiSourceAPIFunction.Arn |
| 128 | + Action: lambda:InvokeFunction |
| 129 | + Principal: elasticloadbalancing.amazonaws.com |
| 130 | + |
| 131 | +Outputs: |
| 132 | + ApiGatewayUrl: |
| 133 | + Description: API Gateway endpoint URL |
| 134 | + Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com" |
| 135 | + |
| 136 | + ALBUrl: |
| 137 | + Description: Application Load Balancer URL |
| 138 | + Value: !Sub "http://${ApplicationLoadBalancer.DNSName}" |
0 commit comments