CyberKeeda In Social Media

Windows Server : One Liner Command to get List of Installed Software

 


Open Windows Command Prompt and type wmic and hit enter.

C:\Users\Administrator>wmic

wmic:root\cli>


Now hit the below command to extract a report in text file named list.txt



C:\Users\Administrator>wmic

wmic:root\cli> /output:C:\list.txt product get name, version

Above command will extract a list of installed programs, saved within C directory. 

Read more ...

YAML Cheat Sheet

 


YAML or YML has become a must know data serialization language for System Administrators, it is widely used in many of the configuration management, containerization platforms , Orchestration applications, Infra as a Code, and many more, and because of it's human readable formats, it's widely accepted and used.

YAML usage in some widely used sysadmin tools.
  • Ansible
  • Docker
  • Kubernetes
  • AWS CloudFormation.
  • TerraForm
Here in this blog post, i have kept some of the YAML cheat sheets, which can help you to craft your own or to read some yaml files.

Key Value

A variety of data is structured in the form of key value pairs.

Key - Items written on left side of data ( Fruit, Drink, Vegetable )
Value Items written on left side of data ( Orange, Juice, Spinach)
Important: Space after :


YAML Key Value Pair
Fruit: Orange Drink: Juice Vegetable: Spinach


List or Array

A representation of List Smartphone type and Brand type in YAML format.


YAML List/Array
Smartphones:
- IOS - Android Brands: - Apple - Nokia - Micromax
- Vivo


Dictionary

A representation of Employee named 26 year old male named as Jignesh working as Web developer data in dictionary type in YAML format.


YAML Dictionary/Map
Employee: Name: Jignesh Sex: Male Age: 26 Title: Web Developer


Dictionary inside List

A representation of multiple list of Fruits (Mango, Guava, Banana) and their individual specification mapped in dictionary value ( Calories, Fat, Carbs )


YAML Dictionary inside List
Fruits: - Mango: Calories: 95 Fat: 0.3 Carbs: 25 - Guava: Calories: 105 Fat: 0.4 Carbs: 27 - Banana: Calories: 45 Fat: 0.1 Carbs: 11 Vegetables: - Onion: Calories: 25 Fat: 0.1 Carbs: 6 - Potato: Calories: 22 Fat: 0.2 Carbs: 4.8 - Ginger: Calories: 8 Fat: 0.1 Carbs: 1.9


List inside Dictionary

A representation of Employee named 26 year old male named as Jignesh working as Web developer data in dictionary type.

Employee Shail has been assigned two Projects ( Login and Logout Form ), a representation of List data.

YAML List inside Dictionary
Employee: Name: Shail Sex: Male Age: 28 Title: Web Developer
  Projects:
- Login Form - Logout Form

For sure there are many more to update in the YAML cheat list, but will begin with these..

Read more ...

Linux - Count no of files in a folder by day.

 


One Liner to count the number of files in a directory by date.

We often got this requirement where we have to deal with file counts, we have a huge list of files within a folder and we want to count the number of files created by date.

One Liner Linux CLI command

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

 
Output for above command will look something like below.

1 2019-07-03 1 2019-08-08 6 2019-08-13 1 2019-08-15 1 2019-09-10 2 2019-09-11 1 2019-09-23 1 2019-10-22 1 2019-10-25 1 2019-10-29 1 2019-12-05 1 2020-03-04 2 2020-03-30 1 2020-04-07 11 2020-04-08 2 2020-04-09 1 2020-04-21 1 2020-04-26 2 2020-04-30 430 2020-05-06 1 2020-05-20 4 2020-05-26 951 2020-07-01 434 2020-07-02 1 2020-07-05 2 2020-07-06 100 2020-07-15 1 2020-07-28 6 2020-07-29 1 2020-08-01 2 2020-09-03


Let's break out the command and understand one by one highlighted in pale Yellow.

find . -type f

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

Find . -type f 

Find command will fetch only the files ( -type f ) within present directory ( . )


-printf '%TY-%Tm-%Td\n'

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

-printf '%TY-%Tm-%Td\n' will prints the modification time of files in e.g. 2020-04-26 format

sort | uniq -c
# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

sort : It will sorts the output 
uniq -c : It will count the sorted output by date.


Feel free to use it,  thank me later ;)

Read more ...

AWS CloudFormation Interface Metadata



AWS CloudFormation Metadata
Metadata section define the details about the cloudformation template. 

Syntax Template

Metadata:
  Instances:
    Description"Instances details within dev environment"
  Applications:
    Description"Application details for dev environment"

There are three types of AWS Cloudformation specific Metadata keys.
  •   AWS::CloudFormation::Designer
It's auto generated during drag and drop of canvas within Cloudformation designer section.
  •   AWS::CloudFormation::Interface
It's used for parameter grouping and labeling as per requirement
  •   AWS::CloudFormation::Init
One of the most important section from Automation prospective, it's used for Application installation and configuration on our AWS EC2 instances.

So within this blog post, we will cover 
  • What is Interface Metadata.
  • Why and How it's used
  • How to customize orders of defined parameters.
  • How to group up parameters and mark them a identifying label.
  • How to define labels for the parameters.

AWS::CloudFormation::Interface

So during our stack creation, you might have noticed our defined parameters appears into an alphabetical order by their Logical ids, it has nothing to do with the way or order we define in our CFN template.

Interface Metadata Syntax
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - ParameterGroup
    ParameterLabels:
      - ParameterLabel

For this post, in order to understand more about it we will use the below CFN template as an example.

CFN Template

AWSTemplateFormatVersion: 2010-09-09
Description|
  CFN Script to demonstrate Cloudformation Interface Metadata Usage
  Interface Metadata : AWS::CloudFormation::Interface
  Interface Metadata can be used to
           Group Parameters and it's order.
           Labels for Parameter for user friendly description input.

Parameters:
  EnvironmentName:
    DescriptionSelect the environment.
    TypeString
    Defaultdev
    AllowedValues:
      - dev
      - prd
  EC2KeyName:
    DescriptionSelect your Key name from List.
    TypeAWS::EC2::KeyPair::KeyName
  EC2SecurityGroup:
    DescriptionSelect your security group from List.
    TypeAWS::EC2::SecurityGroup::Id
  EC2Subnet:
    DescriptionSelect your Subnet from List.
    TypeAWS::EC2::Subnet::Id
  EC2InstanceType:
    TypeString
    Defaultt2.micro
    AllowedValues:
      - t2.micro
      - t2.small
  ApplicationType:
    TypeString
    Defaultweb
    AllowedValues:
      - web
      - app
  MonitoringStatus:
    TypeString
    Defaultenabled
    AllowedValues:
      - enabled
      - disabled

When we will import the above template to create or update stack the console will appear something like below.



From above we can witness that irrespective of the order of allignment parameters within CFN script, it re-orders it within console alphabetically by Parameter's logical ids.

What if we want to group parameters and label them as a separate identifier, Interface metadata can help us to accomplish the goal

What are we going to do with above template ?
    Well we have two sections one relates to the  EC2 Configuration data and the other one is more specific to Application configuration, so we will define our parameter groups and label according to that only.

CFN Interface Metadata with Custom Parameter Group

AWSTemplateFormatVersion: 2010-09-09
Description|
  CFN Script to demonstrate Cloudformation Interface Metadata Usage
  Interface Metadata : AWS::CloudFormation::Interface
  Interface Metadata can be used to
           Group Parameters and it's order.
           Labels for Parameter for user friendly description input.
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default"EC2 Configuration"
        Parameters:
          - EC2InstanceType
          - EC2KeyName
          - EC2SecurityGroup
          - EC2Subnet
      - Label:
          default"Application Configuration"
        Parameters:
          - EnvironmentName
          - ApplicationType
          - MonitoringStatus

Once above file is imported, here is how the console screen looks likes.
We have two Parameter Groups and our parameters are ordered by the way we defined under these groups.
  • EC2 Configuration
  • Application Configuration.

Now, we will add some custom Labels to our Parameters to make it more user friendly and informative for user to avoid confusion.

Within our base template, we will intuit below Parameter Logical Ids with our own Label.
  • EnvironmentName
    • Be carefull while choosing your deployment environmnet
  • EC2KeyName
    • Ensure you have the keys before selecting
  • MonitoringStatus
    • It enables cloudwatch logs for application data

CFN Template to demonstrate Custom Labels for Parameters

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default"EC2 Configuration"
        Parameters:
          - EC2InstanceType
          - EC2KeyName
          - EC2SecurityGroup
          - EC2Subnet
      - Label:
          default"Application Configuration"
        Parameters:
          - EnvironmentName
          - ApplicationType
          - MonitoringStatus
    ParameterLabels:
      EnvironmentName
        default"Be carefull while choosing your deployment environmnet"
      EC2KeyName:
        default"Ensure you have the keys before selecting"
      MonitoringStatus:
        default"It enables cloudwatch logs for application data"

Once imported, this is the output in cloudformation console.



From above, we can state few things to note while using a parameter label configuration.

  • ParameterLabels will substitute your logical Id for your parameter with the one we have declared as default within our ParameterLabels section.
  • Only Logical Id gets substituted, description mentioned under individual parameter section persists.

AWS CFN best practice, we must use Interface metadata for large stack creation this makes things quite helpful for end user.

Feel free to comment.  

Read more ...

AWS CloudFormation - EC2 UserData



AWS CloudFormation EC2 UserData.


EC2 UserData is a way to define multiple actions within ec2 instnace once the system gets first time started, if you are from a Linux background you might be familiar with rc.local file where we used to write all the commands or script action that needs to be executed whenever a system gets booted up.
It's somehow similar to that but not the same at all.

Something more about EC2 UserData
  • We can use UserData in CFN template for EC2. 
  • We need to use an intrinsic function Fn::Base64 with UserData in CFN template, this function return the Base64 representation of string, it passes encoded data to EC2 instance.
  • Multiple Line values can be used under Base64 followed by YAML Pipe ( | )
  • UserData scripts/commands runs during the boot cycle when we first launch the instance.
  • To Update and reflect the changes under the value of UserData, reboot is required.
  • Can be used the same way normal parameters are used via !Ref as an argument.
Usage Example.

Below CFN template along with UserData section is used to install to perform three steps.
  • Update YUM
  • Download Logstash using Wget
  • Install downloaded RPM
AWSTemplateFormatVersion: 2010-09-09
Description: A Cloudformation Script to demonstrate EC2 UserData

Resources:
CreateEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
ImageId: ami-01460aa81365561fe
InstanceType: t2.micro
KeyName: kunal_eks_test
SubnetId: subnet-0cdcc621
IamInstanceProfile: "IamInstanceProfile"
SecurityGroups:
- launch-wizard-1
UserData:
Fn::Base64: |
#!/bin/bash
sudo yum update -y
sudo wget https://artifacts.elastic.co/downloads/logstash/logstash-7.1.0.rpm
sudo rpm -Uvh logstash/logstash-7.1.0.rpm

Feel free to comment !

Read more ...

AWS CloudFormation Psuedo Parameters




AWS Cloud Formation Pseudo Parameters.


Parameters section within CloudFormation template is way to gather inputs from user, which can be used within other sections of entire cloudformation script.

Pseudo Parameter
  • Type of parameters that are predefined by AWS Cloudformation.
  • We don't need to declare it within our template section.
  • Can be used the same way normal parameters are used via !Ref as an argument.
Usage Examples.

"AWS::Region" is one of the widely used Pseudo parameters, so below is the way we can use it under our CFN template.


CFN Output Section
Outputs:
  MyStacksRegion:
    Value: !Ref "AWS::Region"


CFN Mappings Section
Resources:
  CreateEC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: !FindInMap
      - ImageIdMap
      - !Ref !Ref "AWS::Region"
      - defaultAMI

So in the same there are few more pseudo parameters enlisted below.

AWS::AccountId
  • Returns the account id from where the stack is being created.
AWS::NotificationARNs
  • Returns the list of notification Amazon Resource Names (ARNs) for the current stack
AWS::NoValue
  • Removes the corresponding resource property when specified as a return value in the Fn::If intrinsic function.
AWS::Partition
  • Returns the partition that the resource is in. For standard AWS regions, the partition is aws. For resources in other partitions, the partition is aws-partitionname
AWS::Region
  • Return where the stack is being created, example ap-south-1
AWS::StackId
  • Returns the ID of the stack as specified with the aws cloudformation create-stack command, such as arn:aws:cloudformation:ap-south-1:123456789987:stack/cyberkeedastack/90af3dc0-d9a7-01e4-972e-1234567se123.
AWS::StackName
  • Returns the name of the stack as specified with the aws cloudformation create-stack command, such as cyberkeedastack.
AWS::URLSuffix
  • Returns the suffix for a domain. The suffix is typically amazonaws.com, but might differ by region.

Read more ...

AWS CloudFormation Mappings : How and where to use.



AWS Cloud Formation Mappings.


Mappings is one of the section under AWS Cloudformation templates, which matches a key to it's corresponding set of named values

Basic mapping template.
Mappings:
MyFirstMapping:
Key1:
Name: Value1
Key2:
Name: Value2

MySecondMapping:
dev:
Name: Server1
prod:
Name: Server2

So from above example few things to note:

  • There can be multiple mappings under mapping section.
  • First/Top level key name must be different, from above template ( Key1, Key2, dev, prod)
  • Second level key must be same, from above template ( Name )
Where can we use mappings.
  • For example, if we want to set a instance type based upon environment or instance AMI id based on environment, we can use it to mappings.
So our Key will be environment name and Instance type and AMI id as values for specific second level key.
  • We can use Fn::FindInMap intrinsic function to retrieve values in map.
So we will use the same example within our Cloud Formation script and understand it's section.

AWSTemplateFormatVersion: 2010-09-09
Description: |
CFN Script to demonstrate Cloudformation Mappings section.

Parameters:
EnvironmentName:
Description: Select the environment.
Type: String
Default: dev
AllowedValues:
- dev
- prd
EC2KeyName:
Description: Select your Key name from List.
Type: AWS::EC2::KeyPair::KeyName
EC2SecurityGroup:
Description: Select your security group from List.
Type: AWS::EC2::SecurityGroup::Id
EC2Subnet:
Description: Select your Subnet from List.
Type: AWS::EC2::Subnet::Id

Mappings:
InstanceTypeMap:
dev:
defaultInstanceType: t2.micro
prd:
defaultInstanceType: t2.small
ImageIdMap:
dev:
defaultAMI: ami-0732b62d310b80e97
prd:
defaultAMI: ami-0732b62d310b80e97

Resources:
CreateEC2Instance:
Type: "AWS::EC2::Instance"
Properties:
ImageId: !FindInMap
- ImageIdMap
- !Ref EnvironmentName
- defaultAMI
InstanceType: !FindInMap
- InstanceTypeMap
- !Ref EnvironmentName
- defaultInstanceType
KeyName: !Ref EC2KeyName
SubnetId: !Ref EC2Subnet
SecurityGroupIds:
- !Ref EC2SecurityGroup

So from above template, 
  • We can launch a basic EC2 instance and here mappings is used to decide which AMI ID and Instance Type based upon environment dev or prod.
  • Parameters section has the EnvironmentName parameter, from which an user can select his/her environment name which can be prod or dev.
Mappings section : We have created two maps as
  • InstanceTypeMap : 
    • First/Top level keys : dev and prod
    • Second Level key : defaultInstanceType.
    • Second level values : t2.micro and t2.small
  • ImageIdMap:
    • First/Top level keys : dev and prod
    • Second Level key : defaultAMI
    • Second level values : ami-0732b62d3 andami-0732b6223

How to Use/Call mapping values within our resource section using intrinsic function FindInMap.


Our above template has two mappings and we will know how it has been referenced under resource section.

EC2 Instance creation resource has a requirement of ImageId through which we can declare our AMI Id.

So we can use the below template as base template.
ImageId: !FindInMap
- MappingLogicalId
- !Ref ParameterLogicalId
- SecondLevelKey
With above base template can be replaced with our values as.
  • Mapping Logical Id : ImageIdMap
  • Top/First Level Key : We have used Parameter to grab it as user input.
  • Second Level Key : defaultAMI
ImageId: !FindInMap
- ImageIdMap
- !Ref EnvironmentName
- defaultAMI

Thus Same way, we have called mapping values under InstanceType's value.
InstanceType: !FindInMap
- InstanceTypeMap
- !Ref EnvironmentName
- defaultInstanceType

Within next post i will cover mappings with Mappings with Pseudo parameters.





Read more ...
Designed By Jackuna