Using cloudformation, I want to give default security group and SSH security group as the security group of the instance

Solution 1:

You just need to add the security group's ID in the list of attached SGs on the EC2's definition, you can do this by exporting the ID of the security group from the security.yaml and importing it from the application.yaml

Network.yaml

  SampleVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: sample-vpc
Outputs:
  DefaultNetworkSG:
    Value:
     !GetAtt SampleVPC.DefaultSecurityGroup
  

Security.yaml

Outputs:
  BastionSecurityGroupID:
    Value:
      Ref: BastionSecurityGroup

Application.yaml

  BastionEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      SecurityGroupIds:
      - !ImportValue: BastionSecurityGroupID
      - !ImportValue: DefaultNetworkSG