ハンズオン(簡易版): CFnテンプレート入門(EC2::SecurityGroupIngress)

事前作業1.2. CFnリソースファイルの作成 (EC2::SecurityGroup 名前指定: SecurityGroup0Named)

手順の目的 [why]

リソース(SecurityGroup0Named)のCloudFormationリソースファイルを作成します。

設定値の指定

設定値の指定

手順に必要な設定値を変数に格納をします。

1. CloudFormationリソース名

CloudFormationリソース名を指定します。

変数の設定:

TEMPLATE_CFN_RESOURCE_NAME='SecurityGroup0Named'

2. リソースファイル用ディレクトリ

リソースファイル用ディレクトリを指定します。

変数の設定:

DIR_TEMPLATE_CFN_RESOURCE="${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources"

ディレクトリが存在することを確認します。

コマンド:

ls -d ${DIR_TEMPLATE_CFN_RESOURCE}

結果(例:存在する場合):

${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources

存在しない場合は作成します。

コマンド:

mkdir -p ${DIR_TEMPLATE_CFN_RESOURCE}

結果(例):

(出力なし)

3. リソースファイル名

リソースファイル名を指定します。

変数の設定:

FILE_TEMPLATE_CFN_RESOURCE="${DIR_TEMPLATE_CFN_RESOURCE}/${TEMPLATE_CFN_RESOURCE_NAME}.txt" \
  && echo ${FILE_TEMPLATE_CFN_RESOURCE}

結果(例):

${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources/SecurityGroup0Named.txt

4. VPCのリソース名

VPCのリソース名の指定します。

変数の設定:

TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC='Vpc0StackName'

5. セキュリティグループ名

セキュリティグループ名を指定します。

変数の設定:

EC2_SECURITY_GROUP_NAME='handson-cli-cfn-ec2-SecurityGroupIngress-0-sg'

6. セキュリティグループの説明

セキュリティグループの説明を指定します。

変数の設定:

EC2_SECURITY_GROUP_DESCRIPTION='Security Group 0 For handson-cli-cfn-ec2-SecurityGroupIngress.'

設定値の確認

各変数に正しい設定値が格納されていることを確認しながら保存します。

変数の確認:

cat << END

  # 1. TEMPLATE_CFN_RESOURCE_NAME:"SecurityGroup0Named"
       TEMPLATE_CFN_RESOURCE_NAME="${TEMPLATE_CFN_RESOURCE_NAME}"
  # 2. DIR_TEMPLATE_CFN_RESOURCE:"${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources"
       DIR_TEMPLATE_CFN_RESOURCE="${DIR_TEMPLATE_CFN_RESOURCE}"
  # 3. FILE_TEMPLATE_CFN_RESOURCE:"${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources/SecurityGroup0Named.txt"
       FILE_TEMPLATE_CFN_RESOURCE="${FILE_TEMPLATE_CFN_RESOURCE}"
  # 4. TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC:"Vpc0StackName"
       TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC="${TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC}"
  # 5. EC2_SECURITY_GROUP_NAME:"handson-cli-cfn-ec2-SecurityGroupIngress-0-sg"
       EC2_SECURITY_GROUP_NAME="${EC2_SECURITY_GROUP_NAME}"
  # 6. EC2_SECURITY_GROUP_DESCRIPTION:"Security Group 0 For handson-cli-cfn-ec2-SecurityGroupIngress."
       EC2_SECURITY_GROUP_DESCRIPTION="${EC2_SECURITY_GROUP_DESCRIPTION}"

END

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

処理の実行

CFnリソースファイルを作成します。

変数の確認:

cat << END

  # FILE_TEMPLATE_CFN_RESOURCE:"${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources/SecurityGroup0Named.txt"
    FILE_TEMPLATE_CFN_RESOURCE="${FILE_TEMPLATE_CFN_RESOURCE}"
  # TEMPLATE_CFN_RESOURCE_NAME:"SecurityGroup0Named"
    TEMPLATE_CFN_RESOURCE_NAME="${TEMPLATE_CFN_RESOURCE_NAME}"
  # TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC:"Vpc0StackName"
    TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC="${TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC}"
  # EC2_SECURITY_GROUP_DESCRIPTION:"Security Group 0 For handson-cli-cfn-ec2-SecurityGroupIngress."
    EC2_SECURITY_GROUP_DESCRIPTION="${EC2_SECURITY_GROUP_DESCRIPTION}"
  # EC2_SECURITY_GROUP_NAME:"handson-cli-cfn-ec2-SecurityGroupIngress-0-sg"
    EC2_SECURITY_GROUP_NAME="${EC2_SECURITY_GROUP_NAME}"

END

コマンド:

cat << EOF > ${FILE_TEMPLATE_CFN_RESOURCE}
  ${TEMPLATE_CFN_RESOURCE_NAME}:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: ${EC2_SECURITY_GROUP_NAME}
      GroupDescription: ${EC2_SECURITY_GROUP_DESCRIPTION}
      VpcId: !Ref ${TEMPLATE_CFN_RESOURCE_NAME_EC2_VPC}

EOF

cat ${FILE_TEMPLATE_CFN_RESOURCE}

結果(例):

SecurityGroup0Named:
  Type: AWS::EC2::SecurityGroup
  Properties:
    GroupName: handson-cli-cfn-ec2-SecurityGroupIngress-0-sg
    GroupDescription: Security Group 0 For handson-cli-cfn-ec2-SecurityGroupIngress.
    VpcId: !Ref Vpc0StackName

完了確認

「リソースファイル"${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources/SecurityGroup0Named.txt"が存在する。」ことを確認します。

コマンド:

ls ${FILE_TEMPLATE_CFN_RESOURCE}

結果(例):

${HOME}/environment/conf-handson-cli-cfn-ec2-SecurityGroupIngress/resources/SecurityGroup0Named.txt

手順の完了