Reference module resources in CloudFormation templates (original) (raw)

In CloudFormation templates, you often need to set properties on one resource based on the name or property of another resource. For more information, see Referencing resources.

To reference a resource contained within a module in your CloudFormation template, you must combine two logical names:

You can combine these two logical names with or without using a period (.) between them. For example, if the module's logical name is MyModule and the resource's logical name is MyBucket, you can refer to that resource as eitherMyModule.MyBucket or MyModuleMyBucket.

To find the logical names of resources inside a module, you can consult the module's schema, which is available in the CloudFormation registry or by using the DescribeType operation. The schema lists all the resources and their logical names that are part of the module.

Once you have the full logical name, you can use CloudFormation functions likeGetAtt and Ref to access property values on module resources.

For example, you have a My::S3::SampleBucket::MODULE module that contains anAWS::S3::Bucket resource with the logical name S3Bucket. To refer to the name of this bucket using the Ref function, you combine the module's name in your template (MyBucket) with the logical name of the resource in the module (S3Bucket). The full logical name is either MyBucket.S3Bucket orMyBucketS3Bucket.

Example template

The following example template creates an S3 bucket using theMy::S3::SampleBucket::MODULE module. It also create an Amazon SQS queue and set its name to be the same as the bucket name from the module. Additionally, the template outputs the Amazon Resource Name (ARN) of the created S3 bucket.

# Template that uses My::S3::SampleBucket::MODULE
Parameters:
  BucketName:
    Description: Name for your sample bucket
    Type: String
Resources:
  MyBucket:
    Type: My::S3::SampleBucket::MODULE
    Properties:
      BucketName: !Ref BucketName
  exampleQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Ref MyBucket.S3Bucket
Outputs:
  BucketArn:
    Value: !GetAtt MyBucket.S3Bucket.Arn