Referencing parameters
Introduction
- When using a cloud architecture composed of multiple infrastructure resources, these resources usually need to communicate with each other.
- To do that, they usually need some kind of identifier or address - for example a database endpoint, API Gateway URL or an ARN (Amazon Resource Name). These identifiers are assigned dynamically, and they can't be known ahead of time (before the stack is deployed).
- Stacktape allows you to easily create dynamic references to these resources using $ResourceParam() and $CfResourceParam() directives.
Parameters of Stacktape resources
Stacktape allows you to reference most commonly needed parameters of the resources it creates.
These include database endpoints, database connection strings, URLs of API Gateways and Load Balancers, etc.
These parameters can be referenced using a $ResourceParam() directive.
$ResourceParam()
directive returns the specified parameter of the specified Stacktape resource.Arguments:
- 1. stacktape resource name - name of the referenced Stacktape resource as specified in the configuration.
- 2. parameter name - parameter to reference. You can find a list of all referenceable parameters for every resource in its docs:
Copy
resources:myDatabase:type: relational-databaseproperties: ...database properties...myFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda-requiring-db.tsenvironment:- name: DB_CONNECTION_STRINGvalue: $ResourceParam('myDatabase', 'connectionString')
Example usage of $ResourceParam() directive.
Parameters of Cloudformation resources
- To reference parameters of native Cloudformation resources or parameters that aren't supported by
$ResourceParam()
directive, you can use the $CfResourceParam() directive. - Arguments:
- 1. cloudformation logical name - name of the AWS Cloudformation resource.
- If you want to reference a resource defined in
cloudformationResources
section, just use its name. - If you want to reference a child resources of a Stacktape resource, you can get a list of child resource using the stack-info command
- If you want to reference a resource defined in
- 2. parameter name - parameter of the AWS Cloudformation resource to reference. To see a list of all referenceable cloudformation parameters for resources in your stack, refer to Listing child resources.
- 1. cloudformation logical name - name of the AWS Cloudformation resource.
Listing child resources
Let's say we want to get parameter of the AWS Lambda resource (cloudformation type AWS::Lambda::Function
) that is a
child resource of myFunction
stacktape resource.
Copy
resources:myFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda.ts
We can use stack-info command to get a detailed list of all resources and their child resources.
Copy
stacktape stack-info --detailed --stage my-stage --region eu-west-1
The (truncated) output will look like this. The logical name of the AWS::Lambda::Function
resource is highlighted.
Copy
resources:myFunction:resourceType: functioncloudformationChildResources:MyFunctionFunction:cloudformationResourceType: AWS::Lambda::Functionstatus: DEPLOYEDreferenceableParams:- Name- ArnLogGroup:cloudformationResourceType: AWS::Logs::LogGroupstatus: DEPLOYEDreferenceableParams:- Name- ArnlogicalName: DeletePostLambdaLogGroup
Now we can use the lambda function's logical name to get its parameter.
Copy
cloudformationResources:MySnsTopic:type: AWS::SNS::Topicresources:myBucket:type: bucketprocessData:type: functionproperties:packageConfig:filePath: path/to/my/lambda.tsdestinations:onFailure: $CfResourceParam('MySnsTopic', 'Arn')environment:name: BUCKET_NAMEvalue: $CfResourceParam('MyBucketBucket', 'Name')