---
AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Additional Region workload stack - Deploys two vMX instances in different availability zones in an exsisting VPC along with the resources required to communicate with the AWS Cloud WAN network.
  **WARNING** You will be billed for the AWS resources used if you create a stack from this template.(qs-1srtkbc43)
Metadata:
  QuickStartDocumentation:
    EntrypointName: "Launch into an existing VPC"
    Order: "2"
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Meraki configuration
        Parameters:
          - MerakiOrgID
          - MerakiAPIKey
          - NumberOfvMXs
          - vMX1Token
          - vMX2Token
          - vMX1MerakiNetworkTag
          - vMX2MerakiNetworkTag
      - Label:
          default: Network configuration
        Parameters:
          - VPCID
          - AZ1PublicSubnetID
          - AZ2PublicSubnetID
          - VPCRouteTableID
#          - MerakiEventBusArn
          - MerakiEventBusName
          - GlobalNetworkName
          - BaseRegionName
      - Label:
          default: EC2 configuration
        Parameters:
          - InstanceType
      - Label:
          default: AWS Lambda configuration
        Parameters:
          - LambdaRate
      - Label:
          default: AWS Quick Start configuration
        Parameters:
          - QSS3BucketName
          - QSS3KeyPrefix
          - QSS3BucketRegion
          - KeyPairName
    ParameterLabels:
      KeyPairName:
        default: Key pair name
      MerakiOrgID:
        default: Meraki organization ID
      MerakiAPIKey:
        default: Meraki organization API key
      InstanceType:
        default: Amazon EC2 instance type
      NumberOfvMXs:
        default: Number of vMX instances
      vMX1MerakiNetworkTag:
        default: Meraki network tag for first vMX instance
      vMX2MerakiNetworkTag:
        default: Meraki network tag for second vMX instance
      vMX1Token:
        default: Authentication token for first vMX instance
      vMX2Token:
        default: Authentication token for second vMX instance
      VPCID:
        default: VPC ID
      AvailabilityZone1CIDR:
        default: CIDR block for public subnet 1
      AvailabilityZone2CIDR:
        default: CIDR block for public subnet 2
      VPCRouteTableID:
        default: Existing VPC route table ID
      QSS3BucketName:
        default: Quick Start S3 bucket name
      QSS3BucketRegion:
        default: Quick Start S3 bucket Region
      QSS3KeyPrefix:
        default: Quick Start S3 key prefix
      LambdaRate:
        default: CloudWatch Events rule rate
      MerakiEventBusName:
        default: "Meraki Cloud WAN event bus name"
      GlobalNetworkName:
        default: "Meraki Cloud WAN global network name"
      BaseRegionName:
        default: "Value of the BaseRegionName parameter, located in the Outputs section of the base Region stack"
Parameters:
  MerakiOrgID:
    Description: Meraki organization ID.
    Type: String
  MerakiAPIKey:
    Description: Meraki organization API key.
    Type: String
    NoEcho: 'true'
  InstanceType:
    Description: Amazon EC2 instance type for the vMX instances. For recommended and supported instance types, see https://documentation.meraki.com/MX/MX_Installation_Guides/vMX_Setup_Guide_for_Amazon_Web_Services_(AWS).
    Type: String
    Default: c5.large
    AllowedValues:
      - c5.large
      - c5.xlarge
      - m4.large
    ConstraintDescription: Must be a valid EC2 instance type.
  NumberOfvMXs:
    Description: Number of vMX instances to deploy.
    Type: String
    AllowedValues:
      - '1'
      - '2'
    Default: '2'
  vMX1MerakiNetworkTag:
    Description: On the Meraki dashboard, the vMX network tag for the first vMX instance.
    Type: String
    Default: vMX1
  vMX2MerakiNetworkTag:
    Description: On the Meraki dashboard, the vMX network tag for the second vMX instance.
    Type: String
    Default: vMX2
  vMX1Token:
    Description: Onboarding token for the first vMX instance. You can obtain the token from the Meraki dashboard. See https://account.meraki.com/login/dashboard_login?go=%2F.
    Type: String
    Default: change_to_match_the_token_from_meraki_dashboard
  vMX2Token:
    Description: Onboarding token for the second vMX instance. You can obtain the token from the Meraki dashboard. See https://account.meraki.com/login/dashboard_login?go=%2F.
    Type: String
    Default: change_to_match_the_token_from_meraki_dashboard
  AZ1PublicSubnetID:
    Description: ID of the public subnet in Availability Zone 1 of the existing VPC (e.g., `subnet-z0376dab`).
    Type: "AWS::EC2::Subnet::Id"
  AZ2PublicSubnetID:
    Description: ID of the public subnet in Availability Zone 2 of the existing VPC (e.g., `subnet-a29c3d84`).
    Type: "AWS::EC2::Subnet::Id"
  VPCID:
    Description: ID of the existing VPC for deployment (e.g., `vpc-0343606e`).
    Type: AWS::EC2::VPC::Id
  VPCRouteTableID:
    Description: ID of the existing VPC main route table (e.g., `rt-0343606e`).
    Type: String
  QSS3BucketName:
    AllowedPattern: '^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$'
    ConstraintDescription:
      The Quick Start bucket name can include numbers, lowercase
      letters, uppercase letters, and hyphens (-). It cannot start or end with a
      hyphen (-).
    Default: aws-quickstart
    Description:
      Name of the S3 bucket for your copy of the Quick Start assets.
      Keep the default name unless you are customizing the template.
      Changing the name updates code references to point to a new Quick
      Start location. This name can include numbers, lowercase letters,
      uppercase letters, and hyphens, but do not start or end with a hyphen (-).
      See https://aws-quickstart.github.io/option1.html.
    Type: String
  QSS3BucketRegion:
    Default: 'us-east-1'
    Description: 'AWS Region where the Quick Start S3 bucket (QSS3BucketName) is
    hosted. Keep the default Region unless you are customizing the template.
    Changing this Region updates code references to point to a new Quick Start location.
    When using your own bucket, specify the Region.
    See https://aws-quickstart.github.io/option1.html.'
    Type: String
  QSS3KeyPrefix:
    AllowedPattern: '^[0-9a-zA-Z-/]*$'
    ConstraintDescription:
      The Quick Start S3 key prefix can include numbers, lowercase letters,
      uppercase letters, hyphens (-), and forward slashes (/). The prefix should
      end with a forward slash (/).
    Default: quickstart-cisco-meraki-vmx-cloudwan/
    Description:
      S3 key prefix used to simulate a directory for your copy of the
      Quick Start assets. Keep the default prefix unless you are customizing
      the template. Changing this prefix updates code references to point to
      a new Quick Start location. This prefix can include numbers, lowercase
      letters, uppercase letters, hyphens (-), and forward slashes (/). End with
      a forward slash. See https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
      and https://aws-quickstart.github.io/option1.html.
    Type: String
  KeyPairName:
    Description: EC2 key pair name.
    ConstraintDescription: Must be the name of an existing EC2 key pair. vMX instances launch with this key pair.
    Type: "AWS::EC2::KeyPair::KeyName"
  LambdaRate:
    Description: How often the CloudWatch Events rule runs. The rule invokes AWS Lambda to update the VPC and transit gateway route tables.
    Default: rate(10 minutes)
    AllowedValues:
      - rate(1 minute)
      - rate(10 minutes)
      - rate(60 minutes)
    Type: String
  MerakiEventBusName:
    Description: The Meraki event bus name.
    Type: String
  GlobalNetworkName:
    Description: The Meraki Cloud WAN global network name.
    Type: String
  BaseRegionName:
    Description: Deployment location of the base Region template. Value needed for the EventBridge API.
    Type: String


Conditions:
  UsingDefaultBucket: !Equals [!Ref QSS3BucketName, 'aws-quickstart']
  1vMXCondition: !Or
    - !Equals
      - !Ref 'NumberOfvMXs'
      - '1'
    - !Condition 2vMXCondition
  2vMXCondition: !Equals
    - !Ref 'NumberOfvMXs'
    - '2'

Resources:
  VMX1Stack:
    Condition: 1vMXCondition
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL:
        !Sub
          - https://${S3Bucket}.s3.${S3Region}.${AWS::URLSuffix}/${QSS3KeyPrefix}submodules/quickstart-cisco-meraki-sd-wan-vmx/templates/quickstart-cisco-meraki-sdwan-vmx-instance.yaml
          - S3Region: !If [UsingDefaultBucket, !Ref 'AWS::Region', !Ref QSS3BucketRegion]
            S3Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName]
      Parameters:
        InstanceType:
          Ref: InstanceType
        KeyPairName:
          Ref: KeyPairName
        SubnetID:
          Ref: AZ1PublicSubnetID
        vMXToken:
          Ref: vMX1Token
        vMXMerakiNetworkTag:
          Ref: vMX1MerakiNetworkTag

  VMX2Stack:
    Condition: 2vMXCondition
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL:
        !Sub
          - https://${S3Bucket}.s3.${S3Region}.${AWS::URLSuffix}/${QSS3KeyPrefix}submodules/quickstart-cisco-meraki-sd-wan-vmx/templates/quickstart-cisco-meraki-sdwan-vmx-instance.yaml
          - S3Region: !If [UsingDefaultBucket, !Ref 'AWS::Region', !Ref QSS3BucketRegion]
            S3Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName]
      Parameters:
        InstanceType:
          Ref: InstanceType
        KeyPairName:
          Ref: KeyPairName
        SubnetID:
          Ref: AZ2PublicSubnetID
        vMXToken:
          Ref: vMX2Token
        vMXMerakiNetworkTag:
          Ref: vMX2MerakiNetworkTag

  CustomResourceStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL:
        !Sub
          - https://${S3Bucket}.s3.${S3Region}.${AWS::URLSuffix}/${QSS3KeyPrefix}templates/quickstart-cisco-meraki-sdwan-vmx-cw-vpc-attachment-custom-resource.template.yaml
          - S3Region: !If [UsingDefaultBucket, !Ref 'AWS::Region', !Ref QSS3BucketRegion]
            S3Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName]
      Parameters:
          AvailabilityZone1SubnetID:
            Ref: AZ1PublicSubnetID
          AvailabilityZone2SubnetID:
            Ref: AZ2PublicSubnetID
          VPCID:
            Ref: VPCID
          BaseRegionName:
            Ref: BaseRegionName
          MerakiEventBusName:
            Ref: MerakiEventBusName
          GlobalNetworkName:
            Ref: GlobalNetworkName

  MerakiLambdaStack:
    DependsOn: CustomResourceStack
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL:
        !Sub
          - https://${S3Bucket}.s3.${S3Region}.${AWS::URLSuffix}/${QSS3KeyPrefix}templates/quickstart-cisco-meraki-sdwan-vmx-cw-lambda.template.yaml
          - S3Region: !If [UsingDefaultBucket, !Ref 'AWS::Region', !Ref QSS3BucketRegion]
            S3Bucket: !If [UsingDefaultBucket, !Sub '${QSS3BucketName}-${AWS::Region}', !Ref QSS3BucketName]
      Parameters:
          AvailabilityZone1SubnetID:
            Ref: AZ1PublicSubnetID
          AvailabilityZone2SubnetID:
            Ref: AZ2PublicSubnetID
          VPCID:
            Ref: VPCID
          QSS3BucketName:
            Ref: QSS3BucketName
          QSS3KeyPrefix:
            Ref: QSS3KeyPrefix
          QSS3BucketRegion:
            Ref: QSS3BucketRegion
          SDWANVPCRTID:
            Ref: VPCRouteTableID
          MerakiAPIKey:
            Ref: MerakiAPIKey
          MerakiOrgID:
            Ref: MerakiOrgID
          vMX1MerakiNetworkTag:
            Ref: vMX1MerakiNetworkTag
          vMX2MerakiNetworkTag:
            Ref: vMX2MerakiNetworkTag
          LambdaRate:
            Ref: LambdaRate
          MerakiEventBusName:
            Ref: MerakiEventBusName
          BaseRegionName:
            Ref: BaseRegionName
          GlobalNetworkName:
            Ref: GlobalNetworkName

Outputs:
  VPCID:
    Description: The ID of the deployed SD-WAN VPC.
    Value: !Ref VPCID
  vMX1InstanceID:
    Description: First vMX instance ID.
    Value:
      Fn::GetAtt:
      - VMX1Stack
      - Outputs.vMXInstanceID
  vMX2InstanceID:
    Description: Second vMX instance ID.
    Value:
      Fn::GetAtt:
      - VMX2Stack
      - Outputs.vMXInstanceID
  Postdeployment:
    Description: See the deployment guide for postdeployment steps.
    Value: https://aws.amazon.chttps://aws-quickstart.github.io/quickstart-cisco-meraki-vmx-cloudwan/