あしあと

自分自身のログ(足跡)となります。ソフトウェアエンジニアです。ブログはテック系の内容が少し多めです。

CloudFormationでLambdaを最小限でデプロイする方法

追記
調べていたら、他にも方法がありそう。。なので、随時更新します。

タイトルの通りの内容です。
同等の記事が見つからなかったので書いてみました。
2018-07-07時点の情報になります。

必要なコマンド

バージョンは、

$ aws --version
aws-cli/1.15.52 Python/3.6.1 Darwin/17.6.0 botocore/1.10.5

フォルダ構成

以下のフォルダ構成です

$ tree
.
├── hello.py
└── template.json

ソースコード

hello.py
def handler(event, context):
    return "Hello"
template.json
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Lambda sample environment template",
  "Resources": {
    "MyLambdaFunction": {
      "Type" : "AWS::Lambda::Function",
      "Properties" : {
        "Code" : "hello.py",
        "Handler": "hello.handler",
        "Runtime" : "python2.7",
        "Role" : "arn:aws:iam::xxxxxx:role/xxxxx"
      }
    }
  }
}

Roleは自身のIAMからLambdaを使えるロールを選択してください。

デプロイ方法

以下の2つのコマンドでデプロイ。

aws cloudformation package --template-file template.json --s3-bucket test --output-template-file output.yml
aws cloudformation deploy --template-file output.yml --stack-name test

参考資料