logoStacktape docs




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 indentifier or address - for example a database endpoint, API Gateway URL or an ARN (Amazon Resource Name) of a SQS queue. These identifiers are assigned dynamically, and they can't be known ahead of time (before deploying the stack).
  • Stacktape allows you to easily create a dynamic reference 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 paremeter 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:
resources:
myDatabase:
type: relational-database
properties: ...database properties...
myFunction:
type: function
properties:
packageConfig:
filePath: path/to/my/lambda-requiring-db-endpoint.ts
environment:
- name: DB_ENDPOINT
value: $ResourceParam('myDatabase', 'endpoint')
- name: DB_PORT
value: $ResourceParam('myDatabase', 'port')

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
    • 2. parameter name - parameter of the AWS Cloudformation resource to reference. To see a list of all referenceable cloudformation parameters, refer to Referenceable parameters docs

Listing child resources

Let's say we want to override the description of the AWS Lambda resource (cloudformation type AWS::Lambda::Function) in the following configuration

resources:
myFunction:
type: function
properties:
packageConfig:
filePath: path/to/my/lambda.ts


We can use stack-info command to get a detailed list of all resources and their child resources.

stacktape stack-info --detailed --stage my-stage --region eu-west-1

The (truncated) output will look like this. The logical name of the Lambda function resource is highlighted.

resources:
myFunction:
resourceType: function
cloudformationChildResources:
LambdaFunction:
cloudformationResourceType: AWS::Lambda::Function
status: DEPLOYED
referenceableParams:
- Name
- Arn
LogGroup:
cloudformationResourceType: AWS::Logs::LogGroup
status: DEPLOYED
referenceableParams:
- Name
- Arn
logicalName: DeletePostLambdaLogGroup


Now we can use the lambda function's logical name to get its parameter.

cloudformationResources:
MySnsTopic:
type: AWS::SNS::Topic
resources:
myBucket:
type: bucket
processData:
type: function
properties:
packageConfig:
filePath: path/to/my/lambda.ts
destinations:
onFailure: $CfResourceParam('MySnsTopic', 'Arn')
environment:
name: BUCKET_NAME
value: $CfResourceParam('MyBucketBucket', 'Name')

Cloudformation parameters