Referencing parameters
When building a cloud architecture with multiple resources, those resources often need to communicate with each other. To do so, they require identifiers such as a database endpoint, an API Gateway URL, or an ARN. These identifiers are assigned dynamically and cannot be known before the stack is deployed.
Stacktape allows you to dynamically reference these resources using the $ResourceParam()
and $CfResourceParam()
directives.
Parameters of Stacktape resources
You can reference the most commonly needed parameters of Stacktape-managed resources, such as database endpoints, database connection strings, and URLs of API Gateways and Load Balancers.
These parameters can be referenced using the $ResourceParam()
directive, which takes two arguments:
- stacktape resource name: The name of the Stacktape resource as defined in your
stacktape.yml
file. - parameter name: The parameter to reference. You can find a list of all referenceable parameters in the documentation for each resource:
resources:myDatabase:type: relational-databaseproperties: ...myFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda.tsenvironment:- name: DB_CONNECTION_STRINGvalue: $ResourceParam('myDatabase', 'connectionString')
Parameters of Cloudformation resources
To reference parameters of native Cloudformation resources or parameters that are not supported by the $ResourceParam()
directive, you can use the $CfResourceParam()
directive. It takes two arguments:
- cloudformation logical name: The logical name of the Cloudformation resource. If you are referencing a resource defined in the
cloudformationResources
section, use its name. To reference a child resource of a Stacktape resource, you can get a list of child resources using thestacktape stack-info
command. - parameter name: The parameter of the Cloudformation resource to reference. To see a list of all referenceable Cloudformation parameters for the resources in your stack, see Listing child resources.
Listing child resources
Suppose you want to get a parameter of an AWS::Lambda::Function
resource that is a child of the myFunction
Stacktape resource.
resources:myFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda.ts
You can use the stacktape stack-info --detailed
command to get a detailed list of all resources and their children.
stacktape stack-info --detailed --stage my-stage --region eu-west-1
The (truncated) output will look like this, with the logical name of the AWS::Lambda::Function
resource highlighted:
resources:myFunction:resourceType: functioncloudformationChildResources:MyFunctionFunction:cloudformationResourceType: AWS::Lambda::Functionstatus: DEPLOYEDreferenceableParams:- Name- ArnLogGroup:cloudformationResourceType: AWS::Logs::LogGroupstatus: DEPLOYEDreferenceableParams:- Name- ArnlogicalName: DeletePostLambdaLogGroup
Now you can use the Lambda function's logical name to get its parameter.
cloudformationResources:MySnsTopic:type: AWS::SNS::Topicresources:myBucket:type: bucketprocessData:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda.tsdestinations:onFailure: $CfResourceParam('MySnsTopic', 'Arn')environment:- name: BUCKET_NAMEvalue: $CfResourceParam('MyBucketBucket', 'Name')