LoginSignup
9
1

More than 5 years have passed since last update.

AWS Lambda (Python) からCloudFormationのスタック作成・削除を行う

Last updated at Posted at 2018-07-26

業務時間だけ使うAWSリソースがあり、それをCloudFormationを使って管理しているという場合、Lambdaからスタックの作製・削除ができると便利です。

削除するのは簡単なのですが、作成するときは少し手こずったのでそれを書いておきます。

作成

TemplateURLにS3バケット内のファイルを指定する場合は次のようにします。
※httpsとスキームが付きますが、Webホスティングの設定は必要ありません。

https://s3-ap-northeast-1.amazonaws.com/(S3バケット名)/ファイル名

ということで作成するときはこんな感じになります。

    cf = boto3.client('cloudformation')
    res = cf.create_stack(
        StackName=(スタック名),
        TemplateURL='https://s3-ap-northeast-1.amazonaws.com/(S3バケット名)/ファイル名',
        Parameters=[
            {
                'ParameterKey': 'パラメータ名1',
                'ParameterValue': '設定値1'
            },
            {
                'ParameterKey': 'パラメータ名2',
                'ParameterValue': '設定値2'
            },
        ],
        Capabilities=[
            'CAPABILITY_NAMED_IAM',
        ],
    )

削除

削除はスタック名を指定するだけなのでとても簡単。

    cf = boto3.client('cloudformation')
    res = cf.delete_stack(
        StackName=(スタック名)
    )
9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1