Description: >
  This template illustrates how to use AWS CodeBuild and AWS CodePipeline to
  achieve continuous deployment of source code from a GitHub repository via a
  Docker container to an Amazon EC2 Container Service cluster.

Parameters:
  RepositoryBranch:
    Description: The name of the branch for the CodeCommit repo
    Type: String
    Default: master
    AllowedPattern: "[\\x20-\\x7E]*"
    ConstraintDescription: Can contain only ASCII characters.
  EmailAddress:
    Description: Email Address for sending SNS notifications for CodeCommit
    Type: String

Resources:
  Cluster:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://s3.amazonaws.com/www.devopsessentialsaws.com/samples/ecs/templates/ecs-cluster.yml
      Parameters:
        SourceSecurityGroup: !GetAtt LoadBalancer.Outputs.SecurityGroup
        Subnets: !GetAtt VPC.Outputs.Subnets
        VpcId: !GetAtt VPC.Outputs.VpcId

  DeploymentPipeline:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://s3.amazonaws.com/www.devopsessentialsaws.com/samples/ecs/templates/deployment-pipeline.yml
      Parameters:
        Cluster: !GetAtt Cluster.Outputs.ClusterName
        RepositoryBranch: !Ref RepositoryBranch
        EmailAddress: !Ref EmailAddress
        TargetGroup: !GetAtt LoadBalancer.Outputs.TargetGroup

  LoadBalancer:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://s3.amazonaws.com/www.devopsessentialsaws.com/samples/ecs/templates/load-balancer.yml
      Parameters:
        Subnets: !GetAtt VPC.Outputs.Subnets
        VpcId: !GetAtt VPC.Outputs.VpcId

  VPC:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://s3.amazonaws.com/www.devopsessentialsaws.com/samples/ecs/templates/vpc.yml
      Parameters:
        Name: !Ref AWS::StackName
        VpcCIDR: 10.215.0.0/16
        Subnet1CIDR: 10.215.10.0/24
        Subnet2CIDR: 10.215.20.0/24


Outputs:
  SiteUrl:
    Description: The sample service that is being continuously deployed.
    Value: !GetAtt LoadBalancer.Outputs.ServiceUrl

  PipelineUrl:
    Description: The continuous deployment pipeline in the AWS Management Console.
    Value: !GetAtt DeploymentPipeline.Outputs.PipelineUrl

  GitCloneCommand:
    Value:
      Fn::Join:
      - ''
      - - "git clone https://git-codecommit."
        - Ref: AWS::Region
        - ".amazonaws.com/v1/repos/"
        - Ref: AWS::StackName
        - "/"
    Description: Git Repository URL