Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Cloudformation YAML, use a Ref in a multiline string (? use Fn:Sub)

Imagine you have a aws resource such as

  Resources:
    IdentityPool:
      Type: "AWS::Cognito::IdentityPool"
      Properties:
        IdentityPoolName: ${self:custom.appName}_${self:provider.stage}_identity
        CognitoIdentityProviders:
          - ClientId:
              Ref: UserPoolClient

The Ref for "AWS::Cognito::IdentityPool" returns the id of this resource. Now lets say I want to reference that id in a multiline string. I've tried

Outputs:  
  AmplifyConfig:
    Description: key/values to be passed to Amplify.configure(config);
    Value: |
      {
        'aws_cognito_identity_pool_id': ${Ref: IdentityPool}, ##<------ Error
        'aws_sign_in_enabled': 'enable',
        'aws_user_pools_mfa_type': 'OFF',
      }

I've also tried to use Fn:Sub but without luck.

   AmplifyConfig:
      Description: key/values to be passed to Amplify.configure(config);
      Value: 
        Fn::Sub 
          - |
            {
              'aws_cognito_identity_pool_id': '${Var1Name}',
              'aws_sign_in_enabled': 'enable',
            }
          - Var1Name:
              Ref: IdentityPool

Any way to do this?

like image 399
honkskillet Avatar asked Apr 27 '18 20:04

honkskillet


People also ask

How do you ref in sub CloudFormation?

Write variables as ${MyVarName}. Variables can be template parameter names, resource logical IDs, resource attributes, or a variable in a key-value map. If you specify only template parameter names, resource logical IDs, and resource attributes, don't specify a key-value map. (Emphasis added.)

What does FN :: sub do?

Fn::Sub. The intrinsic function Fn::Sub substitutes variables in an input string with values that you specify. In your templates, you can use this function to construct commands or outputs that include values that aren't available until you create or update a stack.

What does REF mean in Yaml?

YAML. Ref: logicalName. Ref function is used to enter the logical name of another resource. Ref function returns the value that is predefined for each resource. For example, your EC2 instance has a logical name of EC2.

What is difference between ref and GetAtt in CloudFormation?

GetAtt is essentially the same as the 2nd function of Ref above, it also returns an attribute of the resource that you created within your resource, but while ref returns only a default attribute, GetAtt allows you to choose from different attributes to return.


1 Answers

Using a pipe symbol | in YAML turns all of the following indented lines into a multi-line string.

A pipe, combined with !Sub will let you use:

  • your resources Ref return value easily like ${YourResource}
  • their Fn::GetAtt return values with just a period ${YourResource.TheAttribute}
  • any Pseudo Parameter just as is like ${AWS:region}

As easy as !Sub |, jumping to the next line and adding proper indentation. Example:

Resources:
  YourUserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      UserPoolName: blabla

Outputs:
  AmplifyConfig:
    Description: key/values to be passed to Amplify.configure(config);
    Value: !Sub |
      {
        'aws_cognito_identity_pool_id': '${YourUserPool}',
        'aws_sign_in_enabled': 'enable',
        'aws_user_pools_mfa_type': 'OFF',
      }
  AdvancedUsage:
    Description: use Pseudo Parameters and/or resources attributes
    Value: !Sub |
      {
        'aws_region': '${AWS::Region}',
        'user_pool_arn': '${YourUserPool.Arn}',
      }
like image 168
Clorichel Avatar answered Oct 02 '22 08:10

Clorichel