Skip to content

Latest commit

 

History

History
114 lines (98 loc) · 2.43 KB

Tips.md

File metadata and controls

114 lines (98 loc) · 2.43 KB

Tips

Groovy全般

変数の参照

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

"Ref:VPC" >> [Ref: "VPC"]
{  "Ref": "VPC" }

FindInMap

"FindInMap:AddressMap:IpRange:VPC" >> ["Fn::FindInMap": ["AddressMap", "IpRange", "VPC"]]
{  "Fn::FindInMap": [ "AddressMap", "IpRange", "VPC"] }

GetAtt

"GetAtt:CodeDeployTrustRole:Arn" >> ["Fn::GetAtt": ["CodeDeployTrustRole", "Arn"]]
{  "Fn::GetAtt": [ "CodeDeployTrustRole", "Arn"] }

EC2:Instance

UserData

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

次のように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"
            ]
        ]
    }
}