For increased flexibility, you can add expressions to cloud template code in Cloud Assembly.
How expressions work
Cloud Assembly expressions use the ${expression} construct, as shown in the following examples.
The following code samples are pruned to show only the important lines. The entire, unedited cloud template appears at the end.
Examples
At deployment time, allow the user to paste in the encrypted key needed for remote access:
inputs:
sshKey:
type: string
maxLength: 500
resources:
frontend:
type: Cloud.Machine
properties:
remoteAccess:
authentication: publicPrivateKey
sshKey: '${input.sshKey}'
For deploying to VMware Cloud on AWS, set the folder name to the required name of Workload:
inputs:
environment:
type: string
enum:
- AWS
- vSphere
- Azure
- VMC
- GCP
default: vSphere
resources:
frontend:
type: Cloud.Machine
properties:
folderName: '${input.environment == "VMC" ? "Workload" : ""}'
At deployment time, tag the machine with an all-lowercase env tag that matches the selected environment:
inputs:
environment:
type: string
enum:
- AWS
- vSphere
- Azure
- VMC
- GCP
default: vSphere
resources:
frontend:
type: Cloud.Machine
properties:
constraints:
- tag: '${"env:" + to_lower(input.environment)}'
Set the number of machines in the front-end cluster to one (small) or two (large). Note that the large cluster is set by process of elimination:
inputs:
envsize:
type: string
enum:
- Small
- Large
resources:
frontend:
type: Cloud.Machine
properties:
count: '${input.envsize == "Small" ? 1 : 2}'
Attach machines to the same Default network by binding to the property found in the network resource:
resources:
frontend:
type: Cloud.Machine
properties:
networks:
- network: '${resource.Cloud_Network_1.name}'
apitier:
type: Cloud.Machine
properties:
networks:
- network: '${resource.Cloud_Network_1.name}'
Cloud_Network_1:
type: Cloud.Network
properties:
name: Default
networkType: existing
Encrypt access credentials submitted to the API:
resources:
apitier:
type: Cloud.Machine
properties:
cloudConfig: |
#cloud-config
runcmd:
- export apikey=${base64_encode(input.username:input.password)}
- curl -i -H 'Accept:application/json' -H 'Authorization:Basic :$apikey' http://example.com
Discover the address of the API machine:
resources:
frontend:
type: Cloud.Machine
properties:
cloudConfig: |
runcmd:
- echo ${resource.apitier.networks[0].address}
apitier:
type: Cloud.Machine
properties:
networks:
- network: '${resource.Cloud_Network_1.name}'
Complete cloud template
inputs:
environment:
type: string
enum:
- AWS
- vSphere
- Azure
- VMC
- GCP
default: vSphere
sshKey:
type: string
maxLength: 500
envsize:
type: string
enum:
- Small
- Large
resources:
frontend:
type: Cloud.Machine
properties:
folderName: '${input.environment == "VMC" ? "Workload" : ""}'
image: ubuntu
flavor: medium
count: '${input.envsize == "Small" ? 1 : 2}'
remoteAccess:
authentication: publicPrivateKey
sshKey: '${input.sshKey}'
cloudConfig: |
packages:
- nginx
runcmd:
- echo ${resource.apitier.networks[0].address}
constraints:
- tag: '${"env:" + to_lower(input.environment)}'
networks:
- network: '${resource.Cloud_Network_1.name}'
apitier:
type: Cloud.Machine
properties:
folderName: '${input.environment == "VMC" ? "Workload" : ""}'
image: ubuntu
flavor: small
cloudConfig: |
#cloud-config
runcmd:
- export apikey=${base64_encode(input.username:input.password)}
- curl -i -H 'Accept:application/json' -H 'Authorization:Basic :$apikey' http://example.com
remoteAccess:
authentication: publicPrivateKey
sshKey: '${input.sshKey}'
constraints:
- tag: '${"env:" + to_lower(input.environment)}'
networks:
- network: '${resource.Cloud_Network_1.name}'
Cloud_Network_1:
type: Cloud.Network
properties:
name: Default
networkType: existing
constraints:
- tag: '${"env:" + to_lower(input.environment)}'