我正在尝试在 CircleCI
上使用Terraform部署lambda函数resource "aws_lambda_function" "demo_function" {
function_name = "my-dummy-function"
handler = "index.handler"
role = "${var.IAM_LAMBDA_ARN}"
filename = "${var.API_DIR}"
source_code_hash = "${base64sha256(data.local_file.dist_file.content)}"
runtime = "nodejs8.10"
}
data "local_file" "dist_file" {
filename = "${var.API_DIR}"
}
错误
on lambda/main.tf line 10, in data "local_file" "dist_file":
10: data "local_file" "dist_file" {
变量都很好。 本地部署也可以正常工作。 也尝试过不同版本的Terraform(0.11.xx和0.12.0)
.circleci / config.yml
version: 2
jobs:
build:
working_directory: ~/tmp
docker:
- image: circleci/node:8
- image: hashicorp/terraform
steps:
- checkout
- restore-cache:
keys:
- v1-dependencies-{{ checksum "backend/package.json" }}
- vi-dependencies-
- run:
name: Installing modules
command: cd backend/ && npm ci
- save-cache:
paths:
- ./backend/node_modules
key: v1-dependencies-{{ checksum "backend/package.json" }}
- run:
name: Building backend code
command: cd backend/ && npm run build
- persist_to_workspace:
root: backend/dist
paths:
- terraform_demo-api.zip
deploy:
working_directory: ~/tmp
docker:
- image: alpine:3.8
steps:
- run:
name: Setting up
command: apk update && apk add ca-certificates openssl wget && update-ca-certificates
- run:
name: Installing
command: |
wget https://releases.hashicorp.com/terraform/0.12.0/terraform_0.12.0_linux_amd64.zip
apk add --update git curl openssh make python py-pip groff less unzip
unzip terraform_0.12.0_linux_amd64.zip -d /bin
rm -f terraform_0.12.0_linux_amd64.zip
pip install --quiet --upgrade pip && \
pip install --quiet awscli==1.14.5
- checkout
- attach_workspace:
at: infrastructure/lambda
- restore-cache:
keys:
- v1-infrastructure-{{ checksum "infrastructure/.terraform/terraform.tfstate" }}
- run:
name: Initialising terraform
command: cd infrastructure/ && terraform init -reconfigure -force-copy -backend=true -backend-config "bucket=geet-tf-state-bucket" -backend-config "key=terraform.tfstate" -backend-config "region=us-west-2"
- run: cd infrastructure/lambda/ && cat terraform_demo-api.zip >> api.zip
- run:
name: Executing plan
command: cd infrastructure/ && terraform plan -var="UI_BUCKET_NAME=$UI_BUCKET_NAME" -var="API_DIR=$API_DIR" -var="AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID" -var="AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY" -out .terraform/service.tfplan
- run:
name: Applying infrastructure
command: cd infrastructure/ && terraform apply -auto-approve .terraform/service.tfplan
- save-cache:
paths:
- ./infrastructure/.terraform
key: v1-infrastructure-{{ checksum "infrastructure/.terraform/terraform.tfstate" }}
workflows:
version: 2
build_and_deploy:
jobs:
- build
- deploy:
requires:
- build
如果有人会说文件路径可能不同或者我可以使用$ {path.module},我尝试从同一目录访问本地文件(例如output.tf),但仍然找不到文件问题。