| AWSTemplateFormatVersion: '2010-09-09' |
| Description: Deploy a service into an ECS cluster behind a public load balancer. |
| |
| Parameters: |
| InternetGatewayIdProp: |
| Type: String |
| Default: "" |
| Description: Internet Gateway id. If empty Internet Gateway will be created |
| VPCIdProp: |
| Type: String |
| Default: "" |
| Description: VPC id. If empty VPC will be created |
| SubnetIdProp: |
| Type: String |
| Default: "" |
| Description: Subnet id. If empty Network Stack will be created |
| Mappings: |
| # Hard values for the subnet masks. These masks define |
| # the range of internal IP addresses that can be assigned. |
| # The VPC can have all IP's from 10.0.0.0 to 10.0.255.255 |
| # There is the subnet which cover the ranges: |
| # |
| # 10.0.0.0 - 10.0.0.255 |
| SubnetConfig: |
| VPC: |
| CIDR: '10.0.0.0/16' |
| PublicOne: |
| CIDR: '10.0.0.0/24' |
| |
| Conditions: |
| CreateVPC: !Equals [!Ref VPCIdProp, ""] |
| CreateNetworkStack: !Equals [!Ref SubnetIdProp, ""] |
| CreateInternetGateway: !And |
| - !Equals [!Ref InternetGatewayIdProp, ""] |
| - !Condition CreateNetworkStack |
| Resources: |
| VPC: |
| Condition: CreateVPC |
| Type: AWS::EC2::VPC |
| Properties: |
| EnableDnsSupport: true |
| EnableDnsHostnames: true |
| CidrBlock: !FindInMap ['SubnetConfig', 'VPC', 'CIDR'] |
| |
| # Public subnets, where containers can have public IP addresses |
| PublicSubnetOne: |
| Condition: CreateNetworkStack |
| Type: AWS::EC2::Subnet |
| Properties: |
| AvailabilityZone: |
| Fn::Select: |
| - 0 |
| - Fn::GetAZs: {Ref: 'AWS::Region'} |
| VpcId: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ] |
| CidrBlock: !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR'] |
| MapPublicIpOnLaunch: true |
| |
| # Setup networking resources for the public subnets. Containers |
| # in the public subnets have public IP addresses and the routing table |
| # sends network traffic via the internet gateway. |
| InternetGateway: |
| Condition: CreateInternetGateway |
| Type: AWS::EC2::InternetGateway |
| GatewayAttachement: |
| Condition: CreateNetworkStack |
| Type: AWS::EC2::VPCGatewayAttachment |
| Properties: |
| VpcId: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ] |
| InternetGatewayId: !If [CreateInternetGateway, !Ref 'InternetGateway', !Ref 'InternetGatewayIdProp' ] |
| PublicRouteTable: |
| Condition: CreateNetworkStack |
| Type: AWS::EC2::RouteTable |
| Properties: |
| VpcId: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ] |
| PublicRoute: |
| Condition: CreateNetworkStack |
| Type: AWS::EC2::Route |
| DependsOn: GatewayAttachement |
| Properties: |
| RouteTableId: !Ref 'PublicRouteTable' |
| DestinationCidrBlock: '0.0.0.0/0' |
| GatewayId: !If [CreateInternetGateway, !Ref 'InternetGateway', !Ref 'InternetGatewayIdProp' ] |
| PublicSubnetOneRouteTableAssociation: |
| Condition: CreateNetworkStack |
| Type: AWS::EC2::SubnetRouteTableAssociation |
| Properties: |
| SubnetId: !Ref PublicSubnetOne |
| RouteTableId: !Ref PublicRouteTable |
| Outputs: |
| VPCRef: |
| Value: !If [CreateVPC, !Ref 'VPC', !Ref 'VPCIdProp' ] |
| PublicSubnetOneRef: |
| Value: !If [CreateNetworkStack, !Ref 'PublicSubnetOne', !Ref 'SubnetIdProp' ] |
| PublicOneCIDR: |
| Value: !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR'] |