|
| 1 | +# Exports |
| 2 | + |
| 3 | +Exports allow resources to publish values (e.g. IDs, ARNs, names) so that |
| 4 | +subsequent resources in the stack can reference them. |
| 5 | + |
| 6 | +## Defining exports |
| 7 | + |
| 8 | +Add an `exports` field to a resource in `stackql_manifest.yml`. The exports |
| 9 | +query in the resource's `.iql` file must return columns that match the |
| 10 | +export names. |
| 11 | + |
| 12 | +### Simple exports |
| 13 | + |
| 14 | +```yaml |
| 15 | +resources: |
| 16 | + - name: example_vpc |
| 17 | + props: |
| 18 | + - name: cidr_block |
| 19 | + value: "10.0.0.0/16" |
| 20 | + exports: |
| 21 | + - vpc_id |
| 22 | + - vpc_cidr_block |
| 23 | +``` |
| 24 | +
|
| 25 | +The `exports` anchor in the `.iql` file must return these columns: |
| 26 | + |
| 27 | +```sql |
| 28 | +/*+ exports */ |
| 29 | +SELECT vpc_id, cidr_block AS vpc_cidr_block |
| 30 | +FROM awscc.ec2.vpcs |
| 31 | +WHERE region = '{{ region }}' AND vpc_id = '{{ vpc_id }}'; |
| 32 | +``` |
| 33 | + |
| 34 | +### Aliased exports |
| 35 | + |
| 36 | +Use the mapping format to rename columns on export: |
| 37 | + |
| 38 | +```yaml |
| 39 | +exports: |
| 40 | + - arn: aws_iam_cross_account_role_arn |
| 41 | + - role_name: aws_iam_role_name |
| 42 | +``` |
| 43 | + |
| 44 | +Here the exports query returns columns `arn` and `role_name`, but they are |
| 45 | +stored in the context under the alias names `aws_iam_cross_account_role_arn` |
| 46 | +and `aws_iam_role_name`. |
| 47 | + |
| 48 | +## Referencing exported values |
| 49 | + |
| 50 | +Exported values are injected into the global template context and can be |
| 51 | +referenced by any subsequent resource using `{{ variable_name }}`. |
| 52 | + |
| 53 | +### Unscoped references |
| 54 | + |
| 55 | +The simplest way to reference an export is by its name (or alias): |
| 56 | + |
| 57 | +```yaml |
| 58 | +- name: example_subnet |
| 59 | + props: |
| 60 | + - name: vpc_id |
| 61 | + value: "{{ vpc_id }}" |
| 62 | +``` |
| 63 | + |
| 64 | +Unscoped names follow **last-writer-wins** semantics: if two resources both |
| 65 | +export a variable called `vpc_id`, subsequent resources will see the value |
| 66 | +from whichever resource was processed last. |
| 67 | + |
| 68 | +### Resource-scoped references |
| 69 | + |
| 70 | +Every export is also available under a **resource-scoped** name of the form |
| 71 | +`resource_name.variable_name`. This name is **immutable** -- once set, it |
| 72 | +cannot be overwritten by a later resource: |
| 73 | + |
| 74 | +```yaml |
| 75 | +- name: example_subnet |
| 76 | + props: |
| 77 | + - name: vpc_id |
| 78 | + value: "{{ example_vpc.vpc_id }}" |
| 79 | +``` |
| 80 | + |
| 81 | +Resource-scoped names are useful when: |
| 82 | + |
| 83 | +- Multiple resources export variables with the same name and you need to |
| 84 | + reference a specific one unambiguously. |
| 85 | +- You want to make it clear which resource a value originates from for |
| 86 | + readability and maintainability. |
| 87 | + |
| 88 | +### Example |
| 89 | + |
| 90 | +```yaml |
| 91 | +resources: |
| 92 | + - name: aws_cross_account_role |
| 93 | + file: aws/iam/roles.iql |
| 94 | + props: |
| 95 | + - name: role_name |
| 96 | + value: "{{ stack_name }}-{{ stack_env }}-role" |
| 97 | + # ... |
| 98 | + exports: |
| 99 | + - role_name: aws_iam_cross_account_role_name |
| 100 | + - arn: aws_iam_cross_account_role_arn |
| 101 | +
|
| 102 | + - name: databricks_account/credentials |
| 103 | + props: |
| 104 | + - name: credentials_name |
| 105 | + value: "{{ stack_name }}-{{ stack_env }}-credentials" |
| 106 | + - name: aws_credentials |
| 107 | + value: |
| 108 | + sts_role: |
| 109 | + # Unscoped -- works because only one resource exports this name |
| 110 | + role_arn: "{{ aws_iam_cross_account_role_arn }}" |
| 111 | + # Resource-scoped -- always unambiguous |
| 112 | + # role_arn: "{{ aws_cross_account_role.aws_iam_cross_account_role_arn }}" |
| 113 | +``` |
| 114 | + |
| 115 | +## Protected exports |
| 116 | + |
| 117 | +Sensitive values can be masked in log output by listing them under |
| 118 | +`protected`: |
| 119 | + |
| 120 | +```yaml |
| 121 | +- name: secret_resource |
| 122 | + props: [] |
| 123 | + exports: |
| 124 | + - api_key |
| 125 | + protected: |
| 126 | + - api_key |
| 127 | +``` |
| 128 | + |
| 129 | +The actual value is still stored in the context and usable by templates; |
| 130 | +only the log messages are masked. |
| 131 | + |
| 132 | +## Stack-level exports |
| 133 | + |
| 134 | +The top-level `exports` field in the manifest lists variables that are |
| 135 | +written to a JSON output file (when `--output` is specified): |
| 136 | + |
| 137 | +```yaml |
| 138 | +exports: |
| 139 | + - vpc_id |
| 140 | + - subnet_id |
| 141 | +``` |
| 142 | + |
| 143 | +The output file always includes `stack_name`, `stack_env`, and |
| 144 | +`elapsed_time` automatically. |
0 commit comments