How can I write nested IF in serverless.yml using yaml format file while using it for cloud formation?

I’m trying to access secrets created in secrets manager(AWS Secrets Manager | Rotate, Manage, Retrieve Secrets | Amazon Web Services (AWS)) via SSM (Systems Manager- AWS Systems Manager Parameter Store - AWS Systems Manager ) i.e. AWS Parameter store, and store it in a custom YAML variable in serverless.yml file?
I am trying to implement cloud formation through serverless framework(, and I am trying to implement a nested if statement in cloud formation for implementing the above using the code below.

 stage: &stage 'dev' #Hardcoded for now
  - !Equals [*stage,"prod"]
  - ${ssm:/aws/reference/secretsmanager/cred-prod~true:rdsMasterPassword}
  - !If 
      - !Equals [*stage,"staging"]
      - ${ssm:/aws/reference/secretsmanager/cred-staging~true:rdsMasterPassword}
      - ${ssm:/aws/reference/secretsmanager/cred-dev~true:rdsMasterPassword}

I have tried Cloud formation instrinsic functions Fn::If for this but facing this errror :
Fn::If requires a list argument with the first element being a condition?

This may be a simpler solution in serverless.yml:

custom.rdsMasterPassword: ${ssm:/aws/reference/secretsmanager/cred-${self:provider.stage}~true:rdsMasterPassword}

Hope it helps.