Help us understand the problem. What is going on with this article?

Terraformを使ってLambdaLayerをデプロイしてみる。

More than 1 year has passed since last update.

Terraformを使って、Lambdalayer(Python3.6)のデプロイを行いました。

出来上がったのがこちら

ファイル構成

├── main.tf
└── requirements
    └── commonlib.txt

main.tf

provider "aws" {
    region = "ap-northeast-1"
}
resource "null_resource" "pip_install" {
    provisioner "local-exec" {
        command = "pip2 install -r requirements/commonlib.txt -t ./python"
    }
}

data "archive_file" "libs" {
    depends_on = ["null_resource.pip_install"]
    type        = "zip"
    source_dir = "./python"
    output_path = "./common-lib.zip"
}

resource "aws_lambda_layer_version" "lambda_layer" {
    filename = "${data.archive_file.libs.output_path}"
    layer_name = "common-lib"

    compatible_runtimes = ["python3.6"]
}

commonlib.txt

boto3

解説

provisioner "local-exec" を用いて、事前にローカル環境に"pip2 install -r requirements/commonlib.txt -t ./python"をコマンドの実行を行いcommonlib.txtに書かれた内容をインストールします。

ただしprovisioner "local-exec" はリソース内に記載しないと動作しません。
そこでresource "null_resource"を用いて空のリソース内でローカルコマンドの実行をします。

次にdata "archive_file"にてLambdaLayerにアップロードするため対象ディレクトリをZIP圧縮します。
その際depends_on = ["null_resource.pip_install"]を用いて、PIP Installコマンドの実行後にZIP圧縮が行われるように調整します。

resource "aws_lambda_layer_version"にて対象ZIPファイルがアップロードされ、LambdaLayerが作成されます。

まとめ

Terraformを用いると、1コマンドterraform applyにて、pip install から、LabmdaLayerの作成までが行えます。

jey0taka
お餅の妖怪
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした