Issues with !Sub and !GetAtt on EC2 Metadata

Hi, I’m having issues to obtain some RDS Instance attributes from my EC2 Metadata configSet using !Sub and !GetAtt functions combined.

So, at the resources key in my serverless.yml file I have (among other things) an EC2 instance (OASInstance) and an RDS instance (OASRDSInstance). I’m trying to build a connection string for my DB hosted on RDS, and for that, I’m using !Sub and !GetAtt functions inside a file definition on the Metadata block at my EC2 instance definition.

I’ve tried this in several ways, using short and long syntax format, but I make sure that this one works on a CloudFormation template:

- Fn::Sub:
  - "${RDSEndpointURL}:${RDSEndpointPort}:ORCL"
  - {RDSEndpointURL: !GetAtt OASRDSInstance.Endpoint.Address, RDSEndpointPort: !GetAtt OASRDSInstance.Endpoint.Port}

To give you more context, those lines are in a content block for a path like this one:

The error I’m getting when I try to deploy this template is this one:

Serverless Error ---------------------------------------   
Invalid variable reference syntax for variable RDSEndpointURL. You can only reference env vars, options, & files. You can check our docs for more info.

However, the same code block works perfectly if I use it in the same place on a CloudFormation template.

My guess is that serverless is confusing {RDSEndpointURL} and {RDSEndpointPort} with local template variables, and doesn’t realize that those are just for the !Sub context.

What should I do?


Well, I found a workaround.

I replace !Sub with !Join.

This is what I had:

- Fn::Sub:
  - "${RDSEndpointURL}:${RDSEndpointPort}:ORCL"
  - {RDSEndpointURL: !GetAtt OASRDSInstance.Endpoint.Address, RDSEndpointPort: !GetAtt OASRDSInstance.Endpoint.Port}

And this is the replacement:

- !Join 
  - ''
  - - !GetAtt OASRDSInstance.Endpoint.Address
    - ':'
    - !GetAtt OASRDSInstance.Endpoint.Port
    - ':'
    - ${self:custom.RDSDbName}

However, I would like to know if there is a better way.