defキーワードで変数を定義し、DSL内で参照できる。 CloudFormationのMappingsを使うのと同様だが、テンプレートをビルドした時点で変数がインライン展開される。
def vpcId = "Ref:VPC"
resources {
subnet id: "SubnetA", VpcId: vpcId, CidrBlock: "10.0.0.0/24", AvailabilityZone: "ap-northeast-1a"
subnet id: "SubnetC", VpcId: vpcId, CidrBlock: "10.0.1.0/24", AvailabilityZone: "ap-northeast-1c"
}
"Ref:VPC" >> [Ref: "VPC"]
{ "Ref": "VPC" }
"FindInMap:AddressMap:IpRange:VPC" >> ["Fn::FindInMap": ["AddressMap", "IpRange", "VPC"]]
{ "Fn::FindInMap": [ "AddressMap", "IpRange", "VPC"] }
"GetAtt:CodeDeployTrustRole:Arn" >> ["Fn::GetAtt": ["CodeDeployTrustRole", "Arn"]]
{ "Fn::GetAtt": [ "CodeDeployTrustRole", "Arn"] }
UserDataのスクリプトは内部展開を行うため、複数行テキストとしてDSLに定義できる。
def userData = """\
#! /bin/sh
yum -y update
"""
resources {
instance id: "Web", ImageId: "ami-a003a8a1", UserData: userData
}
"Web": {
"Type": "AWS::EC2::Instance",
"Properties": {
"ImageId": "ami-a003a8a1",
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#! /bin/sh\\n",
"\\n",
"yum -y update\\n"
]
]
}
}
}
}
次のようにUserData内で[Ref:XXX]の書式を埋め込むことで、リソースの参照(Ref)を展開できる。
def userData = """\
#! /bin/bash -v
# Install packages
/opt/aws/bin/cfn-init -s [Ref:AWS::StackId] -r WebServer --region [Ref:AWS::Region]
"""
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#! /bin/bash -v\\n",
"\\n",
"# Install packages\\n",
"/opt/aws/bin/cfn-init -s ",
{
"Ref": "AWS::StackId"
},
" -r WebServer --region ",
{
"Ref": "AWS::Region"
},
"\\n"
]
]
}
}