AWSTemplateFormatVersion: "2010-09-09"
Mappings:
  RegionMap:
    us-east-1:
      AMI: "ami-7f418316"
      TestAz: "us-east-1a"
    us-west-1:
      AMI: "ami-951945d0"
      TestAz: "us-west-1a"
    us-west-2:
      AMI: "ami-16fd7026"
      TestAz: "us-west-2a"
    eu-west-1:
      AMI: "ami-24506250"
      TestAz: "eu-west-1a"
    sa-east-1:
      AMI: "ami-3e3be423"
      TestAz: "sa-east-1a"
    ap-southeast-1:
      AMI: "ami-74dda626"
      TestAz: "ap-southeast-1a"
    ap-southeast-2:
      AMI: "ami-b3990e89"
      TestAz: "ap-southeast-2a"
    ap-northeast-1:
      AMI: "ami-dcfa4edd"
      TestAz: "ap-northeast-1a"
Parameters:
  EnvType:
    Description: Environment type.
    Default: test
    Type: String
    AllowedValues:
      - prod
      - test
    ConstraintDescription: must specify prod or test.
  EnableGeoBlocking:
    Type: String
Conditions:
  CreateProdResources: !Equals [!Ref EnvType, prod]
  BadCondition: String
  UnusedCondition: !Equals [!Ref EnvType, prod]
  TooManyConditions:
    Fn::Equals: [!Ref EnvType, prod]
    Fn::Not: !Equals [!Ref EnvType, prod]
  EnableGeoBlocking: !Equals [!Ref EnableGeoBlocking, "true"]
  HasParam:
    "Fn::Of":
      - !Not [!Equals [!Ref EnvType, ""]]
      - !Not [!Equals [!Ref EnableGeoBlocking, ""]]
  NullCondition: null
Resources:
  EC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
      Tags:
        - Key: TestKey
          Value: TestValue
        - Fn::If:
            - isProd
            - Key: Environment1
              Value: Prod
            - Fn::If:
                - isDev
                - BadKey: Environment2
                  BadValue: Dev
                - !Ref AWS::NoValue
  MountPoint:
    Type: "AWS::EC2::VolumeAttachment"
    Condition: CreateProdResources
    Properties:
      InstanceId: !Ref EC2Instance
      VolumeId: !Ref NewVolume
      Device: /dev/sdh
  NewVolume:
    Type: "AWS::EC2::Volume"
    Condition: CreateProdResources
    Properties:
      Size: 100
      AvailabilityZone: !GetAtt EC2Instance.AvailabilityZone
  CloudFrontDistribution:
    Type: "AWS::CloudFront::Distribution"
    Condition: False
    Properties:
      DistributionConfig:
        Enabled: true
        Restrictions:
          GeoRestriction: !If
            - EnableGeoBlocking
            - RestrictionType: !If
                - EnableGeoBlocking
                - - whitelist
                - whitelist
              BadLocations:
                - BE
                - LU
                - NL
            - RestrictionType: none
Outputs:
  VolumeId:
    Condition: CreateProdResources
    Value: !Ref NewVolume
