Open10

CloudFormation、関数の使い方個人的メモ

enumura1enumura1

◆!Ref
・入力されたパラメータ値の参照
・リソースの参照
・リソースのパラメータに必要な値が Ref による 戻り値で書けるかどうか 、書けない場合は GetAtt による 属性で書けるかどうか 、調査が必要
https://dev.classmethod.jp/articles/cloudformation-tips-focused-on-refs/
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html#aws-resource-ec2-vpc-return-values

◆!GetAtt
・Arn、RoleIdの参照

https://dev.classmethod.jp/articles/cloudformation-tips-focused-on-refs/
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html

◆Outputs
・別スタックで値を参照する時(クロススタック参照)
 └参照元:Outputsセクションに任意のパラメータを記述。
      対象のリソースはValue: !Refで参照する。
  参照先:!ImportValue 任意のパラメータ。
・cloudformaitonのコンソールに値を出力
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html
https://dev.classmethod.jp/articles/cloudformation-tips-focused-on-refs/

enumura1enumura1

cloudformationの値の引き回し

クロススタック参照(Outputs+ImportValue)、CloudFormationのネスト機能

  • クロススタック参照(Outputs+ImportValue):上記の「◆Outputs」
  • CloudFormationのネスト機能
    上位のテンプレートからネストしている下位のテンプレートへの値の引き回しができる
    https://qiita.com/tyoshitake/items/ff08855d629ba4097125
enumura1enumura1

◆Conditionsセクション

リソースを作成する、しないを環境ごとに制御したいときに使用する。
!Refで参照したhoge1がdevと同じであればCreateDevResourcesはtrue、違うならfalse

Conditions:
  CreateDevResources: !Equals [!Ref hoge1, dev]

◆!Equals
[]内の第一引数と第二2引数が同じであればtrue、違う場合はfalseを返す

!Equals [!Ref hoge1, dev]

https://zenn.dev/ano/articles/dc9fbf2ca5af41
https://blog.css-net.co.jp/entry/2022/04/20/105925

enumura1enumura1
  • CloudFormationのネスト機能を使った値の引き回しをやってみた。

下記のlambdaのソースをzip化しS3バケットに配置。

lambda_function.py
import os

def lambda_handler(event, context):
    param_1_from_master = os.environ['PARAM_1_FROM_MASTER']
    param_2_from_master = os.environ['PARAM_2_FROM_MASTER']
    param_3_from_master = os.environ['PARAM_3_FROM_MASTER']
    
    print(f"Value from Master Param 1: {param_1_from_master}")
    print(f"Value from Master Param 2: {param_2_from_master}")
    print(f"Value from Master Param 3: {param_3_from_master}")
    
    return "Success"

下記のネストされたテンプレートのソースをS3バケットに配置。

NestedTemplate.yaml
AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  Param1FromMaster:
    Type: String

  Param2FromMaster:
    Type: String

  Param3FromMaster:
    Type: String

Resources:
  LambdaExecutionRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: LambdaExecutionPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: arn:aws:logs:*:*:*

  MyLambdaFunction:
    Type: "AWS::Lambda::Function"
    Properties:
      Handler: lambda_function.lambda_handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Code:
        S3Bucket: バケット名
        S3Key: S3キー
      Runtime: python3.8
      Environment:
        Variables:
          PARAM_1_FROM_MASTER: !Ref Param1FromMaster
          PARAM_2_FROM_MASTER: !Ref Param2FromMaster
          PARAM_3_FROM_MASTER: !Ref Param3FromMaster

下記の親テンプレートのソースをS3バケットに配置。

MasterTemplate.yaml
AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  MasterParam1:
    Type: String
    Default: "Hello from Master Param 1"

  MasterParam2:
    Type: String
    Default: "Hello from Master Param 2"

  MasterParam3:
    Type: String
    Default: "Hello from Master Param 3"

Resources:
  NestedStack:
    Type: "AWS::CloudFormation::Stack"
    Properties:
      TemplateURL: ネストした下位のテンプレートのオブジェクトURL
      Parameters:
        Param1FromMaster: !Ref MasterParam1
        Param2FromMaster: !Ref MasterParam2
        Param3FromMaster: !Ref MasterParam3

これでcfnでデプロイすると環境変数が設定されたLambdaが作成される。