CloudFormation

リスト17-1 Resourcesセクション(例)
# Resources Section.
Resources:
  VPC:
    Type:"AWS::EC2::VPC"
    Properties:
      CidrBlock: "172.16.0.0/16"

リスト17-2 Parametersセクション(例)
 # Parameters Section.
Parameters:
   VPCCidr:
     Type: String
     Default:"172.16.0.0/16"
# Resources Section.
Resources:
  VPC:
    Type:"AWS::EC2::VPC"
    Properties:
      CidrBlock: !Ref VPCCidr

リスト17-3 Mappingsセクション(例)
# Parameters Section.
Parameters:
  Environment:
    Type: String
    Default:"development"
    AllowedValues:
      - development
      - production
# Mappings Section.
Mappings:
  Cidr:
    development:
      vpc: "172.16.0.0/16"
      subnet: "172.16.1.0/24"
    production:
      vpc: "172.17.0.0/16"
      subnet: "172.17.1.0/24"
# Resources Section.
Resources:
  VPC:
    Type:"AWS::EC2::VPC"
    Properties:
      CidrBlock: !FindInMap [Cidr, !Ref Environment, vpc]
  Subnet:
    Type:"AWS::EC2::Subnet"
    Properties:
      AvailabilityZone: !Sub ${AWS::Region}a
      VpcId: !Ref VPC
      CidrBlock: !FindInMap [Cidr, !Ref Environment, subnet]

リスト17-4 Conditionsセクション(例)
# Parameters Section.
Parameters:
   NeedSubnet:
     Type: String
     Default: true
     AllowedValues:
       - true
       - false
# Conditions Section.
Conditions:
  CreateSubnet: !Equals [!Ref NeedSubnet, 'true']
# Resources Section.
Resources:
...途中略... 
    Subnet:
      Type:"AWS::EC2::Subnet"
      Condition: CreateSubnet
      Properties:
        AvailabilityZone: "ap-northeast-1a"
        VpcId: !Ref VPC
        CidrBlock: "172.16.1.0/24"

リスト17-5 Outputsセクション(例)
# Resources Section.
Resources:
   VPC:
     Type:"AWS::EC2::VPC"
     Properties:
      CidrBlock: "172.16.0.0/16"
...途中略...
 # Outputs Section.
Outputs:
    VPC:
      Value: !Ref VPC
      Export:
        Name: vpc-id
    Subnet:
      Value: !Ref Subnet
AWSTemplateFormatVersion: 2010-09-09
Resources: 
  MyVPC2:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.8.0/21
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: MyVPC2fromCF

■Settings.jsonのyaml.schemasには以下を設定してください

    "yaml.schemas": {
        "https://d33vqc0rt9ld30.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json": [
            "*.cf.yaml",
            "*.cf.yml",
            "*.cfn/*.yaml",
            "*.cfn/*.yml",
            "cloud*formation/*.yaml",
            "cloud*formation/*.yml"
        ]
    },

■Settings.jsonのyaml.customTagsには以下を設定してください

"yaml.customTags": [
    "!Ref",
    "!Sub scalar",
    "!Sub sequence",
    "!Join sequence",
    "!FindInMap sequence",
    "!GetAtt scalar",
    "!GetAtt sequence",
    "!Base64 mapping",
    "!GetAZs",
    "!Select scalar",
    "!Select sequence",
    "!Split sequence",
    "!ImportValue",
    "!Condition",
    "!Equals sequence",
    "!And",
    "!If",
    "!Not",
    "!Or"
  ],

■他に何も設定が入っておらず、本件のみを設定したい場合はSettings.jsonに以下をコピー&ペーストして保存してください

{"yaml.schemas": {
    "https://d33vqc0rt9ld30.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json": [
        "*.cf.yaml",
        "*.cf.yml",
        "*.cfn/*.yaml",
        "*.cfn/*.yml",
        "cloud*formation/*.yaml",
        "cloud*formation/*.yml"
    ]
},


"yaml.customTags": [
    "!Ref",
    "!Sub scalar",
    "!Sub sequence",
    "!Join sequence",
    "!FindInMap sequence",
    "!GetAtt scalar",
    "!GetAtt sequence",
    "!Base64 mapping",
    "!GetAZs",
    "!Select scalar",
    "!Select sequence",
    "!Split sequence",
    "!ImportValue",
    "!Condition",
    "!Equals sequence",
    "!And",
    "!If",
    "!Not",
    "!Or"
    ]
}

■ 4:13 settings.json が開けない場合の対応方法(Windowsユーザーの方への補足)

①VSCodeの左下の歯車→設定

②設定の検索欄に「yaml」と入力

③「settings.jsonで編集」をクリック

④”yaml.schemas”と”yaml.customTags”を追記