AWSTemplateFormatVersion: "2010-09-09"
Description: A sample template
Parameters:
  WebServerPort:
    Type: String
    Default: 80
    Description: Web Server Ports
  Package:
    Type: String
    Default: httpd
    Description: Package to install
  Package1:
    Type: String
    Default: httpd
    Description: Package to install
  pIops:
    Type: Number
    Default: 60
  pEnvironment:
    Type: String
    Default: Prod
  pProdVolumeSize:
    Type: Number
    Default: 50
  pDevVolumeSize:
    Type: Number
    Default: 25
Conditions:
  ProdVolumeSize: !Equals [!Ref pEnvironment, "Prod"]
Resources:
  MyModule:
    Type: "My::Organization::Custom::MODULE"
  RootRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
              Service:
                - "ec2.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Path: "/"
      Policies:
        - PolicyName: "root"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action: "*"
                Resource: "*"
  RolePolicies:
    Type: "AWS::IAM::Policy"
    Properties:
      PolicyName: "root"
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Action: "*"
            Resource: "*"
      Roles:
        - Ref: "RootRole"
  RootInstanceProfile:
    Type: "AWS::IAM::InstanceProfile"
    Properties:
      Path: "/"
      Roles:
        - Ref: "RootRole"
  MyEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-2f726546"
      InstanceType: t3.micro
      KeyName: testkey
      IamInstanceProfile: !Ref RootInstanceProfile
      BlockDeviceMappings:
        - DeviceName: /dev/sdm
          Ebs:
            VolumeType: io1
            Iops: !Ref pIops
            DeleteOnTermination: false
            VolumeSize: 20
      NetworkInterfaces:
        - DeviceIndex: "1"
      UserData: !Sub |
        yum install ${ Package}
  mySnsTopic:
    Type: AWS::SNS::Topic
  MyEC2Instance1:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-2f726546"
      InstanceType: t3.micro
      KeyName: testkey
      BlockDeviceMappings:
        - DeviceName: /dev/sdm
          Ebs:
            VolumeType: io1
            Iops: !Ref pIops
            DeleteOnTermination: false
            VolumeSize:
              !If [ProdVolumeSize, !Ref pProdVolumeSize, !Ref pDevVolumeSize]
      NetworkInterfaces:
        - DeviceIndex: "1"
          PrivateIpAddresses:
            - PrivateIpAddress: 1.1.1.1
              Primary: true
            - PrivateIpAddress: 1.1.1.2
              Primary: false
      UserData:
        Fn::Sub:
          - "yum install ${myPackage}"
          - myPackage: !Ref Package1
  ElasticIP:
    Type: "AWS::EC2::EIP"
    Properties:
      Domain: "vpc"
  ElasticLoadBalancer:
    Type: AWS::ElasticLoadBalancing::LoadBalancer
    Properties:
      AvailabilityZones:
        Fn::GetAZs: ""
      Instances:
        - Ref: MyEC2Instance
      Listeners:
        - LoadBalancerPort: "80"
          InstancePort:
            Ref: WebServerPort
          Protocol: HTTP
      HealthCheck:
        Target:
          Fn::Sub: "HTTP:${WebServerPort}/"
        HealthyThreshold: "3"
        UnhealthyThreshold: "5"
        Interval: "30"
        Timeout: "5"
  IamPipeline:
    DeletionPolicy: Delete
    UpdateReplacePolicy: Delete
    Type: "AWS::CloudFormation::Stack"
    Properties:
      TemplateURL: !Sub "https://s3.${ AWS::Region}.amazonaws.com/ss-vsts-codepipeline-${AWS::Region}/vsts/${AWS::AccountId}/templates/vsts-pipeline/pipeline.yaml"
      Parameters:
        DeploymentName: iam-pipeline
        Deploy: "auto"
  CustomResource:
    Type: Custom::Function
    Version: "1.0"
    Properties:
      ServiceToken: anArn
  WaitCondition:
    Type: AWS::CloudFormation::WaitCondition
    CreationPolicy:
      ResourceSignal:
        Timeout: PT15M
        Count: 1
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          exports.handler = function(event, context) {
            console.log('Hello World');
            context.done(null, 'Hello World');
          };
      Environment:
        Variables:
          NODE_ENV: !Ref pEnvironment
      Handler: index.handler
      Role: "arn:aws:iam::123456789012:role/lambda_basic_execution"
      Runtime: nodejs22.x
Outputs:
  ElasticIP:
    Value: !Sub "${ElasticIP}/32"
    Export:
      Name: "elastic-ip-cidr"
