Search code examples
amazon-web-servicesaws-cloudformation

CloudFormation functions in YAML


I want service role in my CF template as:

ServiceRole: arn:aws:iam::1234556:role/service-role/awsBatchServiceRole(parameter)

I am using below code but getting error:

ServiceRole:!Join ["/", [!Sub arn:aws:iam::${AWS::AccountId}:role/service-role, !Ref BatchServiceRole]]

Error:

Template validation error: Template format error: YAML not well-formed.

How can I fix this?


Solution

  • I've encountered similar issues. Adding spaces after opening brackets and before closing brackets seemed to fix these issues.

    All examples found in the AWS CloudFormation User Guide are including extra spaces before and after they've used curly braces or square brackets while executing special functions.

    Your line of code will then become:

    ServiceRole: !Join [ "/", [ !Sub 'arn:aws:iam::${AWS::AccountId}:role/service-role', !Ref BatchServiceRole ] ]
    

    EDIT: OP added missing quotes which are necessary for inline use of the !Sub function